51

I'm trying to get a progress bar going in Jupyter notebooks. This is a new computer and what I normally do doesn't seem to work:

from tqdm import tqdm_notebook
example_iter = [1,2,3,4,5]
for rec in tqdm_notebook(example_iter):
    time.sleep(.1)

Produces the following text output and doesn't show any progress bar

HBox(children=(IntProgress(value=0, max=5), HTML(value='')))

Similarly, this code:

from ipywidgets import FloatProgress
from IPython.display import display
f = FloatProgress(min=0, max=1)
display(f)
for i in [1,2,3,4,5]:
    time.sleep(.1)

produces this text output:

FloatProgress(value=0.0, max=1.0)

Is there a setting I'm missing to get Jupyter to display these progress bars?

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
J.Doe
  • 749
  • 1
  • 5
  • 9

6 Answers6

78

The answer is in this GitHub issue.

The key is to ensure that you have the ipywidgets notebook extension enabled using the following command:

jupyter nbextension enable --py widgetsnbextension

For the old JupyterLab 2.0 you'll also need to install the JupyterLab extension:

jupyter labextension install @jupyter-widgets/jupyterlab-manager

For the old JupyterLab 2.0 installing the JupyterLab extension with the command above requires that you have Node.js installed. The installer from the Node.js website includes npm, which is also required for the command to run properly.

When using JupyterLab 3.0, the extension will be auto-installed together with ipywidgets when you install it with pip or conda. Node.js is no longer required for JupyterLab 3.0.

krassowski
  • 13,598
  • 4
  • 60
  • 92
Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
  • 2
    I tried this with a recent version of Juypterlab. The second command takes 10 minutes to run and doesn't fix the issue. – Zephaniah Grunschlag Jan 27 '20 at 19:13
  • Ok, it works (partially) if you re-start the kernel, close the Jupyterlab window, and come back. It only worked partially for me because the bar doesn't draw, but I can at least see my messages come up. – Zephaniah Grunschlag Jan 27 '20 at 19:20
  • @ZephaniahGrunschlag You may wish to add your experience with this fix on the GitHub thread I linked in my answer, or post a new question with your specific problem here on Stack Overflow, and link to this question for context. Given that it worked for a number of people, I think it's hardly fair for you to downvote my answer because it doesn't work in your specific case half a year after I posted this answer. – Mihai Chelaru Jan 27 '20 at 19:33
  • 3
    Worth mentioning that node,js is needed. I'm not using conda, therefore I've issued on a mac ' brew install node'. – Oren Mar 24 '20 at 18:47
  • 1
    I had also install npm. Otherwise the installation of the lab-manager failed. – HWM-Rocker Apr 23 '20 at 07:28
  • 3
    wish this becomes auto installed and enabled with `jupyter lab` – Thamme Gowda Jun 29 '20 at 19:55
  • 1
    In case you need to reinstall nodejs, you my want to uninstall the conda version (which is stuck to 6.13.1 at the moment) – Ziofil Oct 25 '20 at 16:32
  • 4
    Unfortunately, this solution did not work for me. With node.js v12.16.1 and the ipywidgets extension installed, I still just get the plain old `HBOX(children=...)` static print out. I believe I already have the `@jupyter-widgets/jupyterlab-manager` installed, and trying to install it again causes ~20 page traceback of a recursion error. – rocksNwaves Nov 02 '20 at 23:26
  • Running `jupyter nbextension enable --py widgetsnbextension` (after running `pip install ipywidgets`) produces for me the error `ModuleNotFoundError: No module named 'widgetsnbextension'` – sh37211 Jan 16 '23 at 07:07
  • ModuleNotFoundError: No module named 'widgetsnbextension' with Jupyter 3.5.3 – Soerendip Mar 06 '23 at 19:04
10

An important consideration here is to have node version >=10.0.0 for this to work. To check your node version use:

node -v

Also, you may have a version of node >=10 installed, but not selected. To check the list of installed node versions, you can use the node version manager nvm using:

nvm ls

In the example below, the selected version is 9.11.2:

->      v9.11.2
        v10.4.0
        v12.5.0

In order to fix this, I will have to run:

nvm use 12.5.0

Now, I can run the two commands mentioned by @Mihai:

jupyter nbextension enable --py widgetsnbextension
jupyter labextension install @jupyter-widgets/jupyterlab-manager

It should work now after refreshing the Jupyter browser tab.

Ali Hassaine
  • 579
  • 5
  • 19
  • note: Node.js is not needed for JupyterLab 3.0+; the extension can be installed with pip, and should auto-activate. – krassowski Apr 07 '21 at 18:54
6

Quick hack, if you don't want to solve it properly:

Run the command line version of tqdm, i.e. replace from tqdm import tqdm_notebook with from tqdm import tqdm and run for i in tqdm(range(10000)): pass.

This produced acceptable output for me.

tyrex
  • 8,208
  • 12
  • 43
  • 50
3

Read all of it before executing a command:

I followed all the instructions here, multiple times, nothing worked.

In my last try, I did:

Create new environment and install jupyterlab

From https://github.com/nodesource/distributions/blob/master/README.md#debinstall:

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejs

then:

conda install -c conda-forge nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager
conda install -c conda-forge ipywidgets

Still it didnt work. Then as advised here, I did:

jupyter labextension install js

Restarted jupyter lab, tried the code:

import ipywidgets as widgets
widgets.IntSlider()

And it finally worked. So I guess what was missing was to install js through jupyter labextension.

Felipe Mello
  • 395
  • 2
  • 8
1

In case you don't have node installed you can follow the instructions here: https://github.com/nodesource/distributions/blob/master/README.md#debinstall

curl -sL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejsapt-get install -y nodejs

But sometime it is better to install via conda:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
 ./Miniconda3-latest-Linux-x86_64.sh

And then:

conda install -c conda-forge nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager

Reference: https://ipywidgets.readthedocs.io/en/latest/user_install.html

Gianmario Spacagna
  • 1,270
  • 14
  • 12
-4

I found the solution provided by Bartosz Mikulski on his blog to be very simple and easy to execute: How to display a progress bar in Jupyter Notebook

import time, sys
from IPython.display import clear_output

def update_progress(progress):
    bar_length = 20
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
    if progress < 0:
        progress = 0
    if progress >= 1:
        progress = 1

block = int(round(bar_length * progress))

clear_output(wait = True)
    text = "Progress: [{0}] {1:.1f}%".format( "#" * block + "-" * (bar_length - block), progress * 100)
    print(text)

You can also refer to this Stack Overflow answer by ChristopheD and "3 Tips to Improving Your Data Science Workflow" on the Towards Data Science blog.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574