Visual Studio Code does not render ipywidgets correctly - visual-studio-code

I am having issue using Visual Studio Code. the code seem to have issue with ipywidgets.
the code is below (very simple interactive chart)
from ipywidgets import interact
import ipywidgets as widgets
import numpy as np
import plotly.graph_objects as go
#interact
def foo(beta=(-10,10,1)):
myx = np.arange(0,100,1)
myy = myx *beta
print ('debug:',myy)
fig = go.FigureWidget()
fig.add_scatter(x= myx, y=myy)
fig.show()
the same code works just as expected in Jupyter Notebook.
I dig further and it seems:
"widgets require us to download supporting files from a 3rd party website. Error loading plotlywidget: ^4.14.3
https://github.com/microsoft/vscode-jupyter/wiki/IPyWidget-Support-in-VS-Code-Python
This is where I got stuck.. What's the next then to fix this issue?
any guidance much appreciated
thanks

Some of the issues I've seen reported seem to have started after the release of ipywidgets 8.
I don't understand exactly what's broken (or if it's broken), but downgrading to version 7.7.1 fixed all issues I was having.
If you want to try it as well, you can run: pip install -U ipywidgets==7.7.1

There two things you can explore.
Check modules:
Run the jupyter --version command inside your conda or virtual environment.
You should see something like this:
IPython : 8.4.0
ipykernel : 6.9.1
ipywidgets : 7.7.1
jupyter_client : 7.2.2
jupyter_core : 4.10.0
jupyter_server : 1.21.0
jupyterlab : 3.4.8
nbclient : 0.6.4
nbconvert : 6.5.0
nbformat : 5.4.0
notebook : 6.4.12
qtconsole : not installed
traitlets : 5.4.0
If there are any of those modules not installed (Apart from qtconsole) try installing them (with pip install "module") and restarting the kernel.
Check Jupyter nbextension.
As mentioned here after running pip install ipywidgets try:
jupyter nbextension enable --py widgetsnbextension
Note: If you are using virtual environments (including conda environments) the recommended way to activate the extension is to run
jupyter nbextension enable --py --sys-prefix widgetsnbextension
I hope this helps.

Related

Problems with ipywidgets when importing vaex

I am trying to import vaex after pip installation but the following problem appears an is not solved even after I have done a pip install for the specific 7.6.0 version of ipywidgets in the jupyter notebook where I am (naively typed !pip -upgrade ipywidgets and !pip install ipywidgets==7.6.0).
ContextualVersionConflict: (ipywidgets 7.5.1 (/Users/federiconutarelli/opt/anaconda3/lib/python3.8/site-packages), Requirement.parse('ipywidgets<8,>=7.6.0'), {'ipympl', 'ipyleaflet'})
Is there a way to solve the issue here from jupyter?

kernel failed to start using conda environment with Jupyter in Visual Studio Code

When using a Jupyter notebook file in Visual Studio code with the Jupyter extension I receive the error The kernel failed to start due to the missing module 'ipykernel_launcher'. Consider installing this module. View Jupyter [log](command:jupyter.viewOutput) for further details.
This notebook works correctly from the JupyterLab web application when I select the same conda environment that was selected in Visual Studio Code.
pip list shows that ipykernel version 5.3.4 is installed, but I don't know how to install ipykernel_launcher. I tried reinstalling pyzmq and it didn't help.
Any ideas why this isn't working?
Had the same problem. My solution is --
First uninstall all jupyter related modules:
python3 -m pip uninstall -y jupyter jupyter_core jupyter-client jupyter-console jupyterlab_pygments notebook qtconsole nbconvert nbformat jupyterlab-widgets nbclient ipykernel ipynb
(from: https://stackoverflow.com/a/52912244/1516331)
And then reinstall ipykernel. I'm using VScode so when I run a cell, VSCode asked me to installipykernel. The following should work the same alternatively:
conda install -c conda-forge --update-deps --force-reinstall ipykernel -y

jupyter ImportError: cannot import name 'constants' from partially initialized module 'zmq.backend.cython'

I recently upgraded jupyter, but now I get
ImportError: cannot import name 'constants' from partially initialized module 'zmq.backend.cython'
when I run for instance jupyter-notebook or jupyter notebook
and pip install --upgrade pyzmq did not solve the issue
any idea?
Thanks
ps: I am on Mac OS 10.15.5 , with jupyter --v:
jupyter core : 4.6.3
jupyter-notebook : 6.0.3
qtconsole : 4.5.5
ipython : 8.0.0.dev
ipykernel : not installed
jupyter client : not installed
jupyter lab : 1.2.3
nbconvert : 5.6.1
ipywidgets : not installed
nbformat : 5.0.4
traitlets : 4.3.3
I had the same issue and below fixed my problem:
pip uninstall pyzmq
pip install pyzmq
This actually worked. If you are launching jupyter-notebook from a venv and you get this error message, just pip unistall pyzmg and pip install pyzmg. You should be fine.

Conda virtual environment for IPython

I'm fairly new to the Python scene. My problem is that when I launch a jupyter notebook from an Anaconda Powershell with my DataScience virtual environment activated, the notebook does not have my virtual environment in it's PATH, and therefore cannot find some packages (like plotly and progress). The same is true when I launch VS Code from Anaconda Navigator with DataScience activated. When I run import plotly in an interactive window, I get ModuleNotFoundError: No module named ‘plotly’. But when I run this line in the terminal within VS Code, it runs without error.
So I have run the following commands in various shell/terminal sessions:
import sys
print(sys.path)
In a VS Code terminal I get:
['', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\lib', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\lib\\site-packages']
In an interactive window in VS Code I get:
['C:\\Users\\adiad\\AppData\\Local\\Temp\\04e2b30c-4fc3-4aa9-9567-3aba17081a73', 'C:\\Users\\adiad\\Anaconda3\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\lib', 'C:\\Users\\adiad\\Anaconda3', '', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\adiad\\.ipython']
In a jupyter notebook running in my browser I get:
['C:\\Users\\adiad\\Anaconda3\\envs\\test', 'C:\\Users\\adiad\\Anaconda3\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\lib', 'C:\\Users\\adiad\\Anaconda3', '', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\adiad\\.ipython']
The IPython session don't appear to reference my virtual environment. So my question is: what do I need to do make IPython run with same environment as my terminal?
I found the following SO question which seems to answer my question, but I find it hard to believe that everyone is following this practice.
How to start an ipython shell(not notebook) within a conda or virtualenv
Here's my configuration:
conda version : 4.7.12
conda-build version : 3.18.8
python version : 3.7.3.final.0
virtual packages :
base environment : C:\Users\adiad\Anaconda3 (writable)
channel URLs : https://conda.anaconda.org/conda-forge/win-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\Users\adiad\Anaconda3\pkgs
C:\Users\adiad\.conda\pkgs
C:\Users\adiad\AppData\Local\conda\conda\pkgs
envs directories : C:\Users\adiad\Anaconda3\envs
C:\Users\adiad\.conda\envs
C:\Users\adiad\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.3 Windows/10 Windows/10.0.18362
After doing further digging, my problem ought to be filed under, "knowing enough to be dangerous." My problem was ultimately caused by the fact that the jupyter package hadn't yet been installed in my new environment. So whenever I attempted to launch an IPython session of some kind, either in VS Code or in a browser, the application would look in my environment and see that the IPython packages weren't installed. It would then look to other conda environments and use the "nearest" equivalent, which was the base environment. Hence, most of the packages would load, but not all.
The fix to my problem was:
conda install jupyter
Another simple fix:
Launch CMD.exe prompt on Anaconda Navigator
Install: conda install jupyter
And
Conda install plotly

How to add "share notebook as gist" button to Jupyter notebook server?

I upgraded from Ipython Notebook Server to Jupyter Server using anaconda:
(ioos)usgs#gam:~/.jupyter/custom$ conda list jupyter
jupyter 1.0.0 py27_0 defaults
jupyter-client 4.1.1 <pip>
jupyter-console 4.0.3 <pip>
jupyter-core 4.0.6 <pip>
jupyter_client 4.1.1 py27_0 defaults
jupyter_console 4.0.3 py27_0 defaults
jupyter_core 4.0.6 py27_0 defaults
and my "Share Notebook as Gist" button went away.
I imagine that the configuration of extensions changed with the new version.
I can see ~/.local/share/jupyter/nbextensions
with gist.js and a directory called mathjax in it.
Does anyone know how to properly configure the "Share Notebook as Gist" button with this Jupyter version?
You still need to activate the nbextension. The custom.js now lives now in:
$(jupyter --config-dir)/custom
I also have the file:
$HOME/.jupyter/nbconfig/notebook.json
with,
{
"load_extensions": {
"livereveal/main": true,
"gist/gist": true
}
}
that I do remember if I or if jupyter migrate created.
PS: make sure to download the latest gist.js.
I've fixed it using these steps:
Download gist.js:
$ wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/gist.js
Install the extension as a user:
$ jupyter nbextension install --user gist.js
Note: the command accepts the file name (with .js)
To enable it, put into $(jupyter --config-dir)/nbconfig/notebook.json:
{
"load_extensions": {
"gist": true
}
}
Though the instructions here suggest that python snippets should be used instead.
To install, simply run:
pip install jupyter-notebook-gist
jupyter serverextension enable --py jupyter_notebook_gist
jupyter nbextension install --py jupyter_notebook_gist
jupyter nbextension enable --py jupyter_notebook_gist
jupyter nbextension enable --py widgetsnbextension
you also need to configure jupyter. for details visit here