0

I am attempting to export a Jupyter Notebook to HTML (as the first step) and ultimately PDF (it's the PDF I want, HTML is just an intermediary step).

I do so using the following conversion script: Script

Unfortunately some of my charts aren't rendering (either in HTML or PDF).

I've followed the guidance at this link. I have a local copy require.js (this one)in the same location as my script, and linked to it in my script.

Here is a test script, for this script the chart is rendering in html, but not pdf:

%%HTML
<script src="require.js"></script>

import pandas as pd
import plotly.express as px
import plotly.io as pio
pio.renderers.default='notebook'


(Markdown cell): Test Chart below


# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age'])
# Plot the number of images per class
px.histogram(df, x="Name", y='Age', color="Name", title="Test Chart")


(Markdown cell): Test Chart above

My real script is a quite long and complex script but includes all of the code above, in the same order (albeit the pandas dataframe has different content).

Similarly I tried the following script, which also renders in html but not pdf (note there's no need for my chart to be interactive).

import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=False)
mesh = go.Mesh3d(x=[0,1,0],y=[0,0,1],z=[0,0,0],i=[0],j=[1],k=[2],flatshading=False,color='#333333')
py.iplot([mesh])

Does anybody have any ideas on what I could try next? I want my chart to render in pdf (html isn't a requirement, but in terms of suppressing prompts/warnings & exporting to a pdf with A4 pages, the conversion script I'm using has worked the best for those requirements).

I've also created a 3rd test script, which uses my real data and real chart, and this successfully renders in html but not pdf. There must be something in my real script that prevents rendering in html - this will be painstakingly difficult to isolate, and since my current solutions don't render in pdf anyway, I thought it best to find a solution that works in pdf first for my test script, and then interrogate my real script afterwards.

  • Is the issue only in the 3D plots? if so, have you checked what filetype they are saved as in the html export? I believe there should be a file with all the outputs line from Jupyter – Pepsi-Joe Jan 16 '22 at 21:57
  • Yes, the issue is only with plots being produced from plotly express, plots produced by plot.scatter are rendering fine and I have just switched my charts around to use matplotlib plots rather than plotly charts. I also looked in the html page code, but couldn't see an obvious file type anywhere. – DaarioNaharis Jan 17 '22 at 11:40

1 Answers1

0

My solution was to use matplotlib instead of plotly.express

Basically this...

import matplotlib.pyplot as plt
plt.bar(df['Name'], df['Age'], color='green')
plt.show

Instead of this...

import plotly.express as px
px.histogram(df, x="Name", y='Age', color="Name", title="Test Chart")