Tqdm progress bar bold description in jupyterlab

Hi all,

I am trying to display a progress bar with a bold title:

from tqdm.notebook import tqdm
import time
for i in tqdm(range(5), desc='\033[1mBold Desc\033[0m'): 
    time.sleep(.2)

Unfortunaltey, the bold ansi control character are not recognised.
It works in jupyter notebook, and in jupyter lab if I do

from tqdm import tqdm

without the notebook submodule, but in this case the progress bar is rendered in an unpleasant “console-like” style.
I thought it might be related to a specific jupyter-lab version, hence I decided to try on “try jupyter”. Here, my code yields and ImportError, complaining IProgress is not found and I should install ipywidgets.
Also, how can I get the jupyter-lab version running on “try jupyter”?

You need to actually have an environment where things are set up to work properly and the same backbone code as you were provided in answer there to ’ TQDM progress bar, change text format’ will work to at least render the progress bar as a nice widget in JupyterLab. (Note that essentially the same post was made here without linking.)

You got a notification you need ipywidgets installed and working. If you have such an environment than this code works to render an widgets-based progress bar in JupyterLab with ipywidgets installed and working with ipykernel:

from tqdm.notebook import tqdm
import time
for i in tqdm(range(5), desc='𝐁𝐨𝐥𝐝 𝐃𝐞𝐬𝐜𝐫'): 
    time.sleep(.4)

Details & How To demonstrate in JupyterLab (with ipykernel):

Go here and press the top ‘launch binder’ badge. Or click here to launch directly.
(Or you can use this more official offering of current JupyterLab via MyBinder from Jupyter developer Jeremy Toulup; it comes, apparently by default because curiously it isn’t listed in the requirements.txt configuration file, with ipywidgets v 8.1.5 at this time.)

When the remote-served Jupyter session comes up, open a new notebook and run the following in a cell:

%pip install tqdm

Run that cell and then restart the kernel.

Now with the kernel restarted, run the code block from your post but increase the sleep time slightly. You’ll see it use ipywidgets to render the progress bar.

(Note: it is easier to test anything involving ipywidgets where you know the environment is properly set up before launching Jupyter. Where I’ve sent you to try it, I already know & verified in both cases it gets installed and works when the session comes up.)

However, you are back to the title not being rendered bold because the implementation is different.
One work around is described here and here is it implemented:

from tqdm.notebook import tqdm
import time
for i in tqdm(range(5), desc='𝐁𝐨𝐥𝐝 𝐃𝐞𝐬𝐜𝐫 (not bold)'): 
    time.sleep(.4)



As for using JupyterLab from the ‘Try Jupyter’ Page, i.e., JupyterLite:

(You mean you want to know what version it is using? Or something else? Assuming something else is meant…)

Unless you need to work in JupyterLite, you are making things harder for yourself trying in JupyterLite. On the ‘Try Jupyter’ page it is clearly emphasized with prominent warning signs that JupyterLite is experimental. And if you further follow the link there to the JupyterLite page the emphasized ‘Status’ page warns yet again with prominent DANGER symbols. (In fact, pertinent to the approaches here to make a mock loop, up until recently the Pyodide kernel was known for not supporting from time import sleep, see the bottom note here in the old coverage for Voici of ‘How does xeus-python lite compare to the Pyodide kernel?’.)
With that important cautionary note out of the way…

However, at present both of the code blocks you’ll find below demonstrating tqdm with an widget bar work with JupyterLite JupyterLab where ipywidgets is first installed.

You will get an error about IProgress if ipywidgets isn’t first installed and you try to run the code though.
And in fact clearing the kernel doesn’t help.
(At this time for the Pyodide kernel you dynamically install in the namespace of the running notebook, i.e., installing once in a separate notebook won’t affect a new notebook you open in JupyterLite with the Pyodide kernel. In other words each new notebook won’t have the ipywidgets installed, unlike how it works in a proper ipykernel.)

Demonstration if using with JupyterLite JupyterLab:

Go to the ‘Try Jupyter’ page. (I suggest doing that last step in an incognito mode or in a private browser window so you get fresh JupyterLite for testing.) When that page comes up, double-click on the notebook listed entitled ‘Lorenz.ipynb’ to open it. When it comes up, let the kernel start up and then go to not busy. Now run the first cell to install ipywidgets.

Alternatively, start a new notebook with the ‘Python (Pyodide)’ as kernel choice, wait until the kernel status indicator is clear, and then run in the first cell the following to install ipywidgets in that notebook namespace:

%pip install -q ipywidgets

When the kernel status indicator again shows clear after installing with that command in either situation, run one of the following in a new cell:

from tqdm.notebook import tqdm

import asyncio
for i in tqdm(range(5), desc='𝐁𝐨𝐥𝐝 𝐃𝐞𝐬𝐜𝐫 (not bold)'): 
    await asyncio.sleep(0.4)

-and-

from tqdm.notebook import tqdm
tqdm.monitor_interval = 0
import time
for i in tqdm(range(5), desc='𝐁𝐨𝐥𝐝 𝐃𝐞𝐬𝐜𝐫 (not bold)'): 
    time.sleep(.4)

(Note if you remove the tqdm.monitor_interval = 0 line, at this time you get the warning: TqdmMonitorWarning: tqdm:disabling monitor support (monitor_interval = 0) due to: can't start new thread.)

And while it is better to do the install at first and then try, I found that at this time you can get away with running the following in a new cell in a new notebook in JupyterLite to do both steps at once:

%pip install -q ipywidgets
from tqdm.notebook import tqdm
tqdm.monitor_interval = 0
import asyncio
for i in tqdm(range(5), desc='𝐁𝐨𝐥𝐝 𝐃𝐞𝐬𝐜𝐫 (not bold)'): 
    await asyncio.sleep(0.4)

@fomightez thank you so much for your help.
Everything clear.
So… I understand there’s no solution besides the workaround of copying/pasting rendered text from an external source?

If it is actually using the description to pass the text to the widget, then that is my understanding. I didn’t dig too far after finding the similar post. For example, I didn’t find in the code base for tqdm how it is passing that text on and rendering it to the left of the widget.

There’s always the possibility of customizing the tqdm.notebook code.
Or possibly printing nothing to the description and possibly using an HBox to place HTML widget with text to the left of the progressbar, vague remiscent of Hbox use here.
But I don’t know how much that will require given it needs to update.
The supplied solution is requires less experimenting and is much less brittle.

1 Like