Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+1 vote

Hello, I have a question about working with the cadabra in jupyter notebook. Often I need to run very long loops, and I would like to make some kind of progress bar. I thought cadabra allowed me to use python, but when I try to use ipywidgets or tqdm for example, the progress bar is displayed after the loop has finished. When I change the kernel to python, everything works. For example:

from tqdm import tqdm
for i in tqdm(range(10000)):
    time.sleep(.1)
    pass

It's not a very important question, but it would make my work easier.

in General questions by (1.4k points)

1 Answer

0 votes

Edit: sorry, I was wrong, the example below doesn't work either. Let me investigate.

I don't know why this minimal example does not work, but what does work is the tqdm.notebook version, which is meant for use in Jupyter notebooks:

from tqdm.notebook import tqdm
import time
for i in tqdm(range(10000)):
    time.sleep(.1)
    pass

So import tqdm from tqdm.notebook, not from tqdm.

Looks better too, IMHO.

by (80.3k points)
edited by

it is interesting that the same thing happens with the print function:

import time
for i in range(10):
    print(i)
    time.sleep(1)

But if you replace it with display, everything works

import time
for i in range(10):
    display(i)
    time.sleep(1)

The Cadabra server buffers standard output, because displaying text in the Cadabra notebook is expensive (since everything goes through LaTeX). If it buffers, it can run all lines simultaneously through LaTeX in one shot.

In the context of the Jupyter kernel this makes less sense, but the Jupyter kernel for Cadabra is really a very thin layer over the Cadabra server.

The right way forward is to give the Cadabra Jupyter kernel a comm channel, but that requires extra work (and my priority is improvements to the native Cadabra notebook).

...