Plotly Graphs not showing in Jupyterlab despite installed extensions - python

I have a weird problem. Jupyterlab doesn't show plotly graphs, but in Jupyter notebook, everything works fine.
I've searched everywhere, many said plotly-extension should be installed. I have installed plotly extension. The notebook I'm working on is trusted. I don't know what should I do.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(y=[2, 1, 4, 3]))
fig.add_trace(go.Bar(y=[1, 4, 3, 2]))
fig.update_layout(title = 'Hello Figure')
fig.show()
This is what jupyterlab shows me

Just had the same problem and, as I am using Anaconda, in the "Server" environment, apart from installing Plotly, I installed the following JupyterLab extension(s):
jupyterlab-plotly
jupyter labextension install jupyterlab-plotly#4.14.1
plotlywidget [optional]
jupyter labextension install #jupyter-widgets/jupyterlab-manager plotlywidget#4.14.1
In order to install the extensions nodejs is required.
For a more detailed answer check my answer here.

Consider adding the following lines of code in your notebook:
from plotly.offline import init_notebook_mode
init_notebook_mode(connected=True)
plotly.offline.init_notebook_mode(connected=True)

Related

ModuleNotFoundError: No module named 'nbformat'

I would like to run python in a Quarto document. I followed the docs about installing and using python in Quarto, but the error stays. Here is some reproducible code:
---
title: "matplotlib demo"
format:
html:
code-fold: true
jupyter: python3
---
For a demonstration of a line plot on a polar axis, see #fig-polar.
```{python}
#| label: fig-polar
#| fig-cap: "A line plot on a polar axis"
import numpy as np
import matplotlib.pyplot as plt
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
subplot_kw = {'projection': 'polar'}
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
```
Error output:
Starting python3 kernel...Traceback (most recent call last):
File "/Applications/RStudio.app/Contents/Resources/app/quarto/share/jupyter/jupyter.py", line 21, in <module>
from notebook import notebook_execute, RestartKernel
File "/Applications/RStudio.app/Contents/Resources/app/quarto/share/jupyter/notebook.py", line 16, in <module>
import nbformat
ModuleNotFoundError: No module named 'nbformat'
I also checked with Quarto if Jupyter is installed in the terminal like this:
quarto check jupyter
Output:
[✓] Checking Python 3 installation....OK
Version: 3.7.11 (Conda)
Path: /Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python
Jupyter: 4.12.0
Kernels: julia-1.8, python3
[✓] Checking Jupyter engine render....OK
Which seems to be OK. So I was wondering if anyone knows how to fix this error?
Edit: output conda info --envs
Output of conda info:
# conda environments:
#
/Users/quinten/.julia/conda/3
/Users/quinten/Library/r-miniconda
/Users/quinten/Library/r-miniconda/envs/r-reticulate
/Users/quinten/Library/rminiconda/general
/Users/quinten/opt/anaconda3
base * /Users/quinten/opt/miniconda3
Edit: conda install Jupyter
The condo install Jupyter was installed (thanks to #shafee), now when I check with quarto if Jupyter exists, I get the following error:
quarto check jupyter
[✓] Checking Python 3 installation....OK
Version: 3.7.11 (Conda)
Path: /Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python
Jupyter: 4.11.1
Kernels: julia-1.8, python3
(/) Checking Jupyter engine render....Unable to load extension: pydevd_plugins.extensions.types.pydevd_plugin_pandas_types
Unable to load extension: pydevd_plugins.extensions.types.pydevd_plugin_pandas_types
[✓] Checking Jupyter engine render....OK
This error usually occurs when jupyter is not installed in the environment that is being used and from the output of quarto checks, its seems that Quarto is referring to the r-reticulate environment and possibly jupyter is not installed in that environment. If thats the case, you simply need to install jupyter in the r-reticulate environment.
Option 01
One option could be to activate the r-reticulate environment and then install the jupyter there.
So first, activate the environment.
conda activate r-reticulate
and then install jupyter there,
conda install jupyter
Option 02
If conda could not find that environment, use the specific path to that environment to install jupyter in that environment,
/Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python -m pip install jupyter
Option 03
If the r-reticulate environment is created by the {reticulate} R package, then an easy option could be using reticulate::conda_install to install a package.
reticulate::conda_install(envname = "r-reticulate", "jupyter")
Also, if you are wondering, why jupyter command runs even though it did not exist in the r-reticulate environment, its because you probably installed jupyter in the base environment and quarto is detecting that jupyter installation while checking.

No module named 'keras_tuner'

I'm executing this code on Kaلgle, and install autokeras library on it,
!pip install autokeras
import matplotlib.pyplot as plt
import pandas as pd
import datetime
%matplotlib inline
#Control the default size of figures in this Jupyter notebook
%pylab inline
pylab.rcParams['figure.figsize'] = (14, 9) # Change the size of plots
#cib = pd.read_csv("../input/balmhils1/balmhils1015.csv")
# load data from csv
cib_f = pd.read_csv("../input/jkse1234/JKSE.csv") # load data from csv
f, ax = plt.subplots(figsize=(11,8))
plt.xlabel("Number of trading day")
plt.ylabel("Close price")
but I got this error.
No module named 'keras_tuner'
Upgrade the keras_tuner
!pip install keras-tuner --upgrade
I had a similar issue using PyCharm. When I installed the keras-tuner package in the Anaconda 3 prompt, I got the message that everything is already installed. The problem was, that the keras-tuner was installed in my base environment and not in the environment (virtual) which I use in PyCharm. You simply need to do the following.
check out your environments in the anaconda prompt using: conda env list
you will probably see the * on the base environment
now change to your working environment for example conda activate tf_cpu -> tf_cpu needs to be changed by your envs name (see on your list)
install your package such as pip install keras_tuner

Jupyter notebook does not reflect library version change in virtualenvironment

I am new to jupyter notebooks and virtualenvironment. I think that I am incurring in a really trivial problem.
I am trying to import matplotlib in a Jupyter notebook and the import works fine. However, it uses the wrong version of the library (installed also systemwide) and I don't know how to force it to use the one in the virtualenvironment.
In my virtualenvironment I want to use a most recent version of matplotlib and therefore I did
pip install --upgrade matplotlib in my virtualenvironment. The upgrade worked fine.
Now if I do pip show matplotlib in my virtualenvironment I get:
Name: matplotlib
Version: 2.2.5
...
Instead, if I do the same command in my home, I get:
Name: matplotlib
Version: 1.5.1
However, if in my jupyter notebook I do
import matplotlib
print ("matplotlib version:",matplotlib.__version__)
I get:
('matplotlib version:', '1.5.1')
Could you please help me understanding what I do wrong?
I found a solution to my problem. First of all, I read this long post that I suggest to everyone using conda or pip.
Then I understood that the shell environment is determined when the Jupyter notebook is launched, while the Python executable is determined by the kernel, and the two do not necessarily match.
I understood this by putting at the beginning of my jupyter notebook the following:
paths = !type -a python
for path in set(paths):
path = path.split()[-1]
print(path)
!{path} -c "import sys; print(sys.path)"
print()
!type python
import sys
sys.executable
!pip show matplotlib
It showed me that I was still using an older version of the matplotlib library.
Therefore I added
!{sys.executable} -m pip install --upgrade matplotlib
Restarted my jupyter kernel and this solved my problem.
I hope this solution will help someone else in the same situation.

ModuleNotFoundError: No module named 'seaborn' in Python IDE

It checks the lib folder where my seaborn stuff is, but still error._.
Hi,
I have looked at other posts, but most seemed to be dealing with Jupyter notebooks, which I'm not. I was wondering how to get to use Seaborn in the basic Python IDE or in PyCharm. I read about filepath collisions stuff, but not too clear on that front.
I'm using Python 3.6 right now.
Thanks for any help!
When dealing with version ambiguity, remember that pip is a python module. Once you're confident that python is the python installation that your IDE is running, run
python --version
python -m pip install seaborn
>pip3 may be pointing to an old or different python installation.
import pip
pip.main(['install','seaborn'])
From: https://stackoverflow.com/a/49391839
If you're doing in jupyter notebook Try doing this:
!conda install -c anaconda seaborn -y
Try running it in the terminal, it will work. But while running the command your pwd should be in the virtual environment in activated form
$ sudo apt-get install -y python3-seaborn
Try running this in a command line 'pip install seaborn'
https://seaborn.pydata.org/installing.html#installing
In PyCharm IDE, we can import the downloaded libraries, and that's what I did. Still, I have no clue on how to resolve this issue on Python IDE, but as of now, it's working on PyCharm for me.
Conda install seaborn
worked in my case.
If you are using jupyter notebook following command will solve the issue
!pip install seaborn
Jupyter Code-Cell:
%%bash
pip install seaborn
Since you are using python 3, try to open with idle3 from terminal
Tried importing seaborn in Pycharm with the proper configuration thing, and it works. I still don't know why the regular Python IDE doesn't work even though one of the sys.path folder it checks contains the seaborn folder, but oh well.
Thanks for all the replies!
Maybe you should try “python —version “to check if you’re using the right version of python in cmd. Sometimes it happens that there are multiple versions installed and it sometimes picks the wrong one.
Also it can happens that python hasn’t the rights to use the module. Then you should create your file and run it with “python [path of file]”
Just found a new method to install all important libraries.
Open command prompt:
Pip install pyforest
All the most important libraries got installed.
My fix (Same import error but on Jupyter Notebook). The import sequence:
import numpy as np
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
Importing Matplotlib before seaborn can lead to an import error (I have no idea why).
The Seaborn official document also shows this:
https://seaborn.pydata.org/installing.html
If you're using Jupyter notebook try to run this_ _
** pip install seaborn --user**

Jupyter notebook will not import matplotlib although it appears to be installed

in:
import matplotlib.pyplot as plt
%matplotlib inline
df.plot()
Gives:
ImportError: matplotlib is required for plotting.
I installed matplotlib through pip, and showing it gives:
Location: c:\users\miscs\appdata\local\programs\python\python36\lib\site-packages
in Jupyter, sys.executable outputs:
'C:\\Users\\miscs\\AppData\\Local\\Programs\\Python\\Python36\\python.exe'
Why is this error occurring and how can I fix it?
Just ran into the same error, resolved it thus:
I did pip install matplotlib in my virtualenv, and then I ran import matplotlib which succeeded, but the df.plot() step still gave me the ImportError: matplotlib is required for plotting. error.
Saved my Jupyter notebook, killed and restarted the Jupyter notebook server, and reopened the notebook. Didn't face the error again.
I guess matplotlib needs to be installed and available before the notebook is started.

Categories