How to get ipywidgets working in Jupyter Lab?

后端 未结 8 1071
北海茫月
北海茫月 2020-12-07 17:39

In Jupyter Notebook, ipywidgets work fine, however they seem to not work in Jupyter Lab (which is supposedly better than Notebook).

I followed these directions.

相关标签:
8条回答
  • If you're on linux and you'd rather avoid conda entirely, and use virtual envs (venvs) to keep python happy, AND you happen to be using an 'older'/LTS Debian based OS, which may not have upto date nodejs: Ie, Ubuntu 16.04 LTS, which doesn't have a node but rather nodejs (node belongs to another package, and the 'legacy nodejs' version is too old), then read on.

    This is a little more complicated to setup, but much easier to maintain long-term than conda is. (you can always just mk a new venv for a new project, without breaking your old projects).

    Main points are:

    • use PPA's to get fresh versions of the things you need
    • Use virtualenvwrapper so you can:
      • use up-to-date python3
      • avoid messing up your 'pip install''s
      • avoid getting the system package manager confused
      • also easily work with people with different versions of python
      • follow python Best Practise
      • easily have different venvs which might have old or incompatible python and pip packages.
    • Use Nodejs binary distributions
      • need node.js version "10.x" for jupyterlab widgets
      • allows using the system's package manager to keep nodejs fresh
      • will be maintained and available as long as LTS's are

    So, all actual steps (these were tested to work on Linux Mint 18.3 Sylvia, which is basically compatible with ubuntu xenial aka Ubuntu 16.04 LTS. Differences will arise mostly in nodejs, read the readme in the github link above to solve for other OS):

    Get an admin to do (or do yourself if you can sudo):

    sudo apt update
    sudo apt install software-properties-common
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    sudo apt install -y python3.8 python3.8-dev python3.8-distutils python3-pip python3-venv
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    Then, as your own user, you can complete the rest of the steps:

    pip3 install --user virtualenv virtualenvwrapper
    mkdir ~/.envs
    

    You'll then want to add the following to the end of your .bashrc :

    export PATH=~/.local/bin:$PATH
    export WORKON_HOME=~/.envs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    source ~/.local/bin/virtualenvwrapper.sh
    

    At this point, make a new shell, and you will be able to run the rest of the setup, actually installing jupyterlab:

    mkvirtualenv -p python3.8 jupenv
    pip install jupyter matplotlib pandas ipympl tqdm
    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    

    Now you're done.

    To open/use jupyter, you want (because of the venv I've called jupenv above, you can name it as you like in that mkvirtualenv line):

    workon jupenv
    jupyter lab
    

    Otherwise, I had no end of hell trying to get nodejs to work with outdated ubuntu packages. Sometimes it would work, for a few restarts, and then fail. Other times it would just keep giving me the same missing widgets, or sometimes little lines of junk js code.

    Virtualenvs are well worth using, especially when you start using python seriously, and working with others who may use different versions / different sets of pip packages. VirtualEnvWrapper makes this pretty painless. The basic point is that everything you 'pip install', even jupyter, ends up being kept cleanly separate (and separate from the system packages), which keeps everything working very nicely.

    There are some basic DO's and DON'T's:

    • DON'T run pip install ... lines without being in a venv
    • DON'T use pip3 in place of pip within a venv.
    • DO just use python and not python3 to run within.
    • DON'T use conda...!
    • DO know that you can have all your virtualenvs updated at once with:
      • allvirtualenv pip install -U pip

    As for the nodejs binary distribution packages: These are highly recommended where they support your particular OS. They'll be very up-to-date and should present the minimum of trouble.

    0 讨论(0)
  • 2020-12-07 18:24

    JupyterLab now prefers a model where arbitrary javascript is no longer allowed to be embedded in a cell's output, which is how many interactive Jupyter Notebook modules used to work. They now ask that modules with interactivity create a JupyterLab extension. IPyWidgets has an extension that can be activated by running this on your command line (which assumes you already have NodeJS installed):

    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    
    0 讨论(0)
提交回复
热议问题