Export jupyter notebook with matplotlib images without inline - python

I would like to export a jupyter notebook to html or ipynb to display online (e.g. Github). My notebook produces some images with matplotlib, and I would like a static version of these images to be included. I've seen several examples of this, and they all seem to use the inline option:
%matplotlib inline
I've tested my code with this option and the images are saved fine (either in the exported html or using an online viewer). However, when I use the ipympl or notebook backends (to keep some interactive features) the images are not saved. Is it possible to include them in the exported html or ipynb?

Related

Hiding and showing sections in Jupyter notebook

I am using jupyter notebook on my PC. As the length of my codes is increasing, I want to hide and display some sections of the python code based on the heading markings in Jupyter Notebook like the google colab does.
Is there any python package to install in the environment specifically for this functionality? Similar to the screenshots below.
Hidden Codes based on the headings
[1]: https://i.stack.imgur.com/kQSMG.png
Expanded Python Codes
[2]: https://i.stack.imgur.com/20HNw.png

Exporting Jupyter notebook with plotly to html not displaying correctly when offline

I am using Jupyter lab, everything works fine within jupyter lab even when I am offline. However whenever I try to export the report to HTML. the plotly plots are not rendered. If I turn on my internet connection the plots are rendered, just fine.
Here is a sample code:
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame(np.random.randn(100,4), columns='A B C D'.split())
px.scatter(df, x='A',y='B')
I have tried following the troubleshooting guide for plotly shown here. Additionally I tried installing on a fresh environment.
If i use the following:
import plotly.io as pio
pio.renderers.default = "jupyterlab"
The offline HTML includes static plot, however I would very like to have the interactivity enabled.
I have noticed that the files differs in size, the static pages are only around 700 Kb whereas when I try to save them as interactive they are about 4 Mb.
Is this not possible in Jupyter lab ? or am I missing something
If you want to be able to have interactivity while being offline, you need to add the plotly.js to the output html.
You can achieve that like this:
import plotly.io as pio
pio.renderers.default='notebook'
Actually, this should be done by default on JupyterLab (you can tell be the increased file size. As in your case it will be >4MB). So if that doesn't work, I suspect a bug. I think I've experienced something similar. Here's my browser console output when using your example and exporting it to html:
For some reason, the included plotly.js seems to depend on require.js which is not included in the html export for some reason. Instead, your page will try to load it from a CDN which fails when you are offline (as seen in the screenshot).
Now, what you can do is to manually include a local version of require.js. Get a copy here. Then, in your Notebook add the following at the top:
%%HTML
<script src="require.js"></script>
Then, export your notebook to html. Make sure, it is in the same folder as the require.js file you downloaded before and open it in the browser.
There should be no more error message in the console and your chart should appear and work interactively:
/e: If you want to share your notebook, this might be sub optimal as it requires you to also distribute the require.js script. You can also directly include the whole script in your notebook. Just put the %%js cell magic at the top of a code cell and paste the content of the require.js file you downloaded below that.
As you are trying to export it to HTML, don't forget jupyter's way (.html). Also with the "Open with" button on jupyter, you can see the maximum file size that it can handle. And most likely the storage wouldn't be the issue.

How to create custom report in PDF using matplotlib and python

i am working on a project where i have to present the Chart /graph created using matplotlib with python3 into a PDF format. The PDF must carry the data, custom titles along with the chart/graph. PDF can be multiple page report as well. I know that we can store the matplotlib charts in PDF. But i am looking for any solution if we can achieve Data, chart and custom text in PDF format.
Even though the question is not very clear. If I have to do what I understand from your question, I will use Jupyter Notebook and save it as PDF. In this notebook, I will have:
Exploratory analysis of the data (What data scientists call EDA)
Discussion and other mathematical formulas at they may apply to your case
The plots
You can save jupyter notebooks to PDF using nbconvert module in Python.
If you don't have it installed on your computer, do so with this command:
pip install nbconvert
To save your notebook as a PDF, go to the folder containing your Jupyter notebook file and run the following command:
jupyter nbconvert --to pdf MyNotebook.ipynb

Save Jupyter Notebook with Plotly Express widgets displaying

I have a Jupyter notebook (python) where I used plotly express to plot in the notebook for analysis purposes.
I want to share this notebook with non-coders, and have the interactive visuals be available still - but it does not seem to work.
I tried following recommendations made here but even after saving widgets state and using nbconvert, when I open the new HTML file, the visuals are not available.
A sample line of plotting can be seen below:
import plotly_express as px
fig = px.scatter(
df,
x='size',
y='size_y',
color='clients',
hover_data=['id'],
marginal_y="histogram",
marginal_x="histogram"
)
fig.show()
After running plotly.offline.init_notebook_mode() in a cell,
you can export the notebook with full interactivity via the file menu:
File --> Export Notebook as... --> Export Notebook to HTML.
I was having similar issues but with JupyterLab. Followed the instructions here: https://plot.ly/python/renderers/ .
import plotly.io as pio
pio.renderers.keys()
I had to add the following snippet to my script:
import plotly.io as pio
pio.renderers.default = 'jupyterlab'
Exporting as HTML after that worked for me. You can read "Overriding the default renderer".
I suppose you would need
pio.renderers.default = 'notebook'
You can specify the default renderers from plotly with:
import plotly.io as pio
pio.renderers.default = 'pdf'
or when displaying the images with:
fig.show(renderer="pdf")
The 2 choices for you are:
'notebook': work well with jupyter notebook;
'pdf': perfect when using nbconvert to convert to HTML or LATEX
You can also join the 2 with "notebook+pdf" so you have iterative plots when running the notebook and static images when converting with nbconvert.
I just had a problem that resulted in this error message.
nbconvert/filters/widgetsdatatypefilter.py:69: UserWarning: Your element with mimetype(s) dict_keys(['application/vnd.plotly.v1+json']) is not able to be represented.
warn("Your element with mimetype(s) {mimetypes}"
Google brought me here, but I could not find a solution from the answers above.
I'm sharing the setup which caused my problem and the solution.
I was running the notebook in VS Code and then on the command line using: jupyter nbconvert
To solve it, I had to start jupyter lab and then run it and save it before running nbconvert.

Data security with mpld3

I often plot data that I do not want to leave my personal computer. I notice that when using mpld3 to generate plots in the jupyter notebook, https://mpld3.github.io is accessed. I think its just pulling plotting scripts, but is there any risk of any of my plotted data being sent off of my computer when using mpld3? Is there an "offline mode" I could use mpld3 with?
Try to pass local=True either to mpld3.enable_notebook() or mpld3.display(). As FAQ claims:
Setting this to True will copy the mpld3 and d3 JavaScript libraries to the notebook directory, and will use the appropriate path within IPython (/files/*.js) to load the libraries

Categories