Persistent plotly plots in jupyter notebooks - jupyter-notebook

My jupyter notebooks that have plotly plots do not retain the plots between sessions.
This is running on a Singularity container based on the official jupyter/datascience-notebook docker image with plotly pip installed on top.
I am using the new renderer framework with the notebook renderer.
My notebooks are trusted.
My plots show up during the session without issues.
They persist across refreshes and reloads of the same notebook, even if I restart the kernel.
They disappear either when I restart the jupyter server or sometimes when I reboot the client machine and come back with a new browser session.
The output cells persist with the correct dimensions, but they are blank.
I can see that a whole bunch of js is embedded in the notebook but it does not render in the browser.
At this point, even if I nbconvert to html, they still do not show up.
Tried with Chromium and Firefox.
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.renderers.default='notebook'
then later I plot a bunch of things like:
go.Figure(go.Scattergl(x = var1, y= var2, mode='markers', marker_size=1))
and
go.Figure(go.Histogram2dContour(x = var1, y= var2))
My understanding is that I am set up to retain these figures in offline (non-running) notebooks; the js generated for the plots and the entirety of plotly.js library appears to be embedded in each notebook adding up to 10s of MBs, but they are not rendered.
Due to this issue I end up having to re-run (sometimes expensive) notebooks when all I need is to take a look at a previous plot.
As a recent matplotlib/seaborn convert I absolutely love the interactivity but this is quickly becoming a showstopper at this point.
I feel like I'm missing something.
Any advice is appreciated.

For me, the following solution worked :
in the environment you're using : install orca :
conda install -c plotly plotly-orca
at the beginning of your notebook : override default renderer as 'notebook' :
import plotly.io as pio
pio.renderers.default='notebook'
plotly graphs were persistent between sessions (ie : after restart of kernels) with these modifications.

Related

Autoformat code in a jupyter notebook with a keystroke

Is there a widget or keyboard shortcut to apply code formatting a jupyter notebook?
If I have a function like:
def f(x):
y = x*2
z = y*2
return z
I'd like to be able to autotab this function over to:
def f(x):
y = x*2
z = y *2
return z
automatically.
There's jupyter-black - a simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using Black
For JupyterLab, there's also JupyterLab_Black.
For JupyterLab, there's jupyterlab_code_formatter - a JupyterLab plugin to facilitate invocation of code formatters. You can configure the keyboard shortcut for that.
Keep in mind that soon (present is Fall 2022) the document-centric notebook experience, most people now associate with the classic notebook interface, will be based on what is underlying JupyterLab, see here. And thus investing great time working out things in the old classic notebook may result in you needing to change approaches soon as a lot of the stuff that works only with the classic Jupyter notebook interface will need updating to use while things already working in current JupyterLab will either work or have a lot less friction getting updated.
There is Jupyter Nbextensions Configurator which contains several extensions for your Jupiter Notebook needs:
autopep8;
Code prettify.
You'll find instructions on how to set them up in links.
Both of extensions have their settable toolbar buttons and are applicable to a whole document.

Octave in Jupyter Notebook - Opening 3D plot in an external window for interactive use

Is it possible using Octave in Jupyter Notebook to open plots in an external window so that they can be manipulated (zooming in/out, rotating)?
I hoped there would be some switch so that I could use them inline (as it is by default) but if need be, I could open them in an external window to manipulate it the way like in GNU Octave GUI. After checking what I need, I would switch back to inline.
I looked for some hints. I found this %%octave -g in octavemagic: Octave inside IPython. Unforutunately, it doesn't work for me when using Octave kernel.
I found something about inline plotting in An Octave kernel for Jupyter (Configuration section) but it doesn't look like it could help anything in regard to switching on/off it from the notebook.
For example, I would like the following code to invoke an external window.
y=-10:0.4:10+eps;
[xx,yy]=meshgrid(x,y);
zz= sin(sqrt(xx.^2+yy.^2))./(sqrt(xx.^2+yy.^2));
surf(xx,yy,zz)
view(-35,45)
It looks like that I have finally found the answer here:
https://github.com/Calysto/octave_kernel/issues/98
https://github.com/Calysto/octave_kernel/issues/6
https://am111.readthedocs.io/en/latest/jmatlab_use.html
%plot gnuplot - switch to using external window
%plot inline - switch to using inline plotting

Atom and Hydrogen: Output and console

I'm a Python beginner and recently came across the Atom editor and the package Hydrogen, that implements the Jupyter notebook. I did so after realizing that running the notebook in Chrome consumed way too many resources and also seemed to be a bit slower.
However, the Atom editor and Hydrogen always output prints within a little frame in the code (see image). Unfortunately, it doesn't use the full window width. I also don't see any console/terminal for installing pip libraries.
Is there a way to have the output in a console below the code, just as in Jupyter, and to have a terminal?
this comes most certainly too late, but in case someone else searches for this:
hit control+shift+P / cmd+shift+P and type "toggle output area".

Display IFrame in IJulia Jupyter Notebook

Is there a way to display an IFrame in an IJulia Jupyter notebook?
I am trying to use Plotly.jl with Jupyter. When plot is called it returns a URL linking to the generated graph.
Using an IFrame should allow this to work out.
Plotly.plot(["z"=>rand(6,6)],["style"=>["type"=>"heatmap"]])
https://plot.ly/~astrieanna/0 # Returned URL
This is not an answer the the posted question, but it does solve the issue with plotly.
Plotly.jl is about to undergo massive changes. One of these will be relying on the PlotlyJS.jl package for all plot generation and display.
If you want to use plotly from julia in a jupyter notebook you should use PlotlyJS.jl: https://github.com/spencerlyon2/PlotlyJS.jl
Docs here: http://spencerlyon.com/PlotlyJS.jl/
IJuliaPortrayals has an IFrame constructor.
IFrame(600, 200, "http://julialang.org")
At In [15]:
Bring back 2003-era pages with IFrame!

iPython Notebook svg Figures by Default

I just started using ipython, and I'm creating figures such as:
fig, axes = plt.subplots()
xs = range(0,100)
axes.plot(xs, [x*x for x in xs], 'r')
I know that the figures can be rendered as svgs, see here. Unfortunately, the figures are always rendered as a rasterized image. The rasterized images become very ugly when I'm using the notebook's zoom feature. Is there a way to change this behavior, such that figures are displayed as svg by default?
The magic I was looking for:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
Alternatively you might still want to show png but save a figure into a file:
plt.savefig(fig_filename, format='svg')
You can change the default figure format in the ipython profile configuration files. What I did was create a configuration profile especially for the notebook server, using:
ipython profile create nbserver
At the command line. This creates a whole bunch of files under ~/.ipython/profile_nbserver which have example lines for almost every setting you could want to change (it might be somewhere such as ~/.config/ipython instead depending on your OS, not sure about where it would be under windows). You need to look in the file ipython_notebook_config.py. You should then add the the line:
c.InlineBackend.figure_formats = ['svg']
Note that this only applied to IPython 3.x, and that you can also specify additional formats as per #HarrySchreiner's comment. For IPython 2.x, you should set c.InlineBackEnd.figure_format='svg'. To use this profile you should start the notebook with
ipython notebook --profile=nbserver
If this is too much trouble then don't give a profile name when running create, and modify the default profile instead.
Also, you may want to have the line
c.IPKernelApp.matplotlib = 'inline'
so that each notebook will automatically start with the matplotlib inline backend used.
Originally I also wanted to use the svg backend instead of png to enable zooming etc. However, I found that certain plots, such as pcolor with a large number of points can just kill my browser when using the svg backend. So I find it easier to use png, and just use the xlim and ylim commands to zoom in manually if I need to.
Also, you should definitely tweak the line c.InlineBackend.rc to set more reasonable defaults for the figure size and the fonts used.
edit
Current recommended best practice is not to use pylab, but to explicitly import matplotlib and numpy instead, so I modified my answer to stop encouraging this. See this post for the reasons why:
http://carreau.github.io/posts/10-No-PyLab-Thanks.html
Also, if svg rendering is too slow for particular plot elements (such as pcolor or plot_surface), you can pass the option rasterized=True to these plot commands. This means that those particular parts of the plot will have fast pixel based rendering, but all the other plot elements will be nicely vectorized.

Resources