71

I was trying to print a plotly plot in Visual Studio Code and caught this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-e07b5feb5ded> in <module>
     30 
     31 fig.update_layout(height=nrows*500)
---> 32 fig.show()

C:\Python38\lib\site-packages\plotly\basedatatypes.py in show(self, *args, **kwargs)
   3147         import plotly.io as pio
   3148 
-> 3149         return pio.show(self, *args, **kwargs)
   3150 
   3151     def to_json(self, *args, **kwargs):

C:\Python38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    383 
    384         if not nbformat or LooseVersion(nbformat.__version__) < LooseVersion("4.2.0"):
--> 385             raise ValueError(
    386                 "Mime type rendering requires nbformat>=4.2.0 but it is not installed"
    387             )

ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed

The code I used:


import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px

df = df[df['Data']>0]
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df[(df['Id'] ==1)|(df['Id'] ==6)]

dfp = pd.pivot_table(df,
                     values='Data',
                     index=['Timestamp'],
                     columns=['Id'],
               )
nrows = len(dfp.columns) 

fig = make_subplots(rows=nrows,
                    cols=1,
                    subplot_titles=['Id '+str(c) for c in dfp.columns])

# add traces
x = 1
for i, col in enumerate(dfp.columns):
    fig.add_trace(go.Scatter(x=dfp.index, y=dfp[col].values,
                             name = 'Id '+str(col),
                             mode = 'lines',
                             ),
                  row=i+1,
                  col=1)

fig.update_layout(height=nrows*500)
fig.show()

I tried pip install nbformat in the console following this feed on GitHub and this question on stackoverflow but it did not work.

However, it seems the code could run with the last 2 rows removed:

fig.update_layout(height=nrows*500)
fig.show()
nilsinelabore
  • 4,143
  • 17
  • 65
  • 122

8 Answers8

83

Method 1

reinstall ipykernel via

pip install ipykernel

Method 2

pip install --upgrade nbformat
J00N
  • 871
  • 5
  • 4
48
!pip install nbformat 
  1. Install this.
  2. Restart your Kernel.
  3. Dam sure it will work!
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Aravind R
  • 835
  • 1
  • 7
  • 14
7

For those that use conda, this worked for me:

Verify the name of your conda environment: conda info --envs

Supposing it's "myenv", proceed:

conda activate myenv
conda install nbformat

Then restart the kernel.

Matt Payne
  • 99
  • 1
  • 8
4

I just ran into this problem too. In my case, it turned out I had only installed ipykernel, but it's best to install the jupyter metapackage.

Reference: https://code.visualstudio.com/docs/datascience/jupyter-notebooks

Jamy Mahabier
  • 400
  • 5
  • 16
2

Configurations

  • Mac OS
  • VS code
  • python 3.x

Method

  • Enter the following code: pip3 install --upgrade nbformat
  • Restart the kernel in the VS code prompt
  • Run your code again

It should work now.

cryptomamy
  • 41
  • 6
0

Restarting ipykernel works for me.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '23 at 05:25
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/34011837) – Simas Joneliunas Mar 16 '23 at 13:45
0

run below command

step 1: write in the box

!pip install nbformat

step 2: restart the kernel

wooo, your issue is fixed!!!

  • This is a duplicate of this answer: https://stackoverflow.com/a/69952185/2648551. Please read other answers carefully before posting something. – colidyre May 22 '23 at 11:38
0

if your jupyter notebook is being managed by conda, install nbformat with;

conda install -c conda-forge nbformat

then restart your kernel.

else use the pip package manager to install or upgrade your nbformat

pip install --upgrade nbformat

don't forget to restart the kernel as well.