svg figures hidden in jupyterlab after some time - python

I recently found I could make all my matplotlib figures with SVG by default in my jupyterlab notebooks with
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_formats = ['svg']
However, if I refresh the page, the figures disappear, leaving behind
<Figure size 864x576 with 1 Axes>
This effect does not occur without changing the inline backend. My plotly figures also still show up after a refresh. I would prefer not to have to re-run the cells. Looking at the actual ipynb file, the SVG is right there in the actual file. How can I get this figure to show without re-running the cell?

The behaviour looks in line with the security model of Jupyter.
Untrusted HTML is always sanitized
Untrusted Javascript is never executed
HTML and Javascript in Markdown cells are never trusted
Outputs generated by the user are trusted
Any other HTML or Javascript (in Markdown cells, output generated by others) is never trusted
The central question of trust is “Did the current user do this?”
Because SVG can have <script> tags, there's an attack surface. Hence, you have two options to display the SVG:
Re-run the cell that generates an SVG (last point above).
Explicitly trust the notebook, jupyter trust /path/to/notebook.ipynb.
Notes:
Plotly saves image/png version in the output of the cell. It's displayed when the notebook is loaded. But the loaded (not reran) cell also has interactivity. Not sure how that exemption works.
As a side note, current JupyterLab's behaviour varies with different forms of SVG display (see issue#10464), but that's not the issue here.

Related

Issues with too many interactive plotly figures

I am using Jupyter notebook on my laptop (the version coming with Anaconda) to perform some sensitivity analysis.
I use plotly to display the results and I like the interactive features that it has.
However, when I am trying to display more than 7/8 interactive plots on the same notebook, some plots disappears and the output cells of those plots go crazy (see picture attached).
Issue with plotly
A solution I found was to disable the interactive feature at least for some of the plots, changing the diplay mode in config as:
config = {'staticPlot': True}
fig.show(config=config)
This method works, however, I like the feature and I was wondering if there was a solution that does not imply disabling the interactive view.
I read about this post where they say it might be a memory issue (even though their graphs are going blank while mine are behaving crazy):
Plotly: How to prevent graphs from going blank when there are too many interactive plots?
However I did not manage to find/change the jupyter configuration file, maybe because I installed it via Anaconda?
I was also wondering if someone experienced exactly the same or there might be a simpler solution to this issue.
Thanks a lot in advance
I believe that in the second link the config file should be generated if not existing.
You can also try changing to gl rendering:
https://plotly.com/python/webgl-vs-svg/

How to display entire output of a code cell when the code is imported from a Python script?

I am plotting some figures using matplotlib inside a notebook in JupyterLab. The code which is plotting these figures is imported from a .py file present in the same directory as of the notebook.
Assuming that figures are of appropriate size, how can I display them entirely i.e. where scrolling is not needed or disabled to display the output and I can get one-shot overview of the output given it is not populating the display beyond the screen size?
The issue is resolved if I copy the code from .py file and run it in the notebook directly:
I have tried Toddneal's answer from Making a Jupyter notebook output cell fullscreen, but scroll bar still exists in the output cell. I also referred to How to avoid output into scrollable frames in jupyter notebook? and tried:
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
as the first code cell in the notebook, but gives out the error: Javascript Error: Can't find variable: IPython.
The output cell is already expanded, so the issue is with the height of the output cell. There are bunch of cells beyond the output cells (could that be possibly affecting the height of output cell?). The extension autoscroll might not work because it is a Jupyter Notebook extension:
Due to major differences between the Jupyter Notebook and JupyterLab, the extensions in this repository will not work in JupyterLab.
As krassowki suggested in a comment, the scrolling is disabled by right-clicking on the output cell and choosing "Disable Scrolling for Outputs."

Importing and displaying a plotly graph saved as an html file into Jupyter Notebook

I am new to programming, so forgive the question if it seems simple.
I have been using Python and its libraries to make plots. I am attempting to import an interactive graph I plotted using the plotly graphing library and display it in the Jupyter notebook.
The graph is saved as an html file. I need the graph to remain as an html file to retain the graph's features and interactivity (zooming in/out, changing the scales, hiding certain data, etc).
So far, I have written out this bit of code:
import plotly
import os
os.chdir(r'C:\Users\alsha\Documents\CE-CERT - SIGI\CMC Data')
from IPython.display import HTML
HTML(filename="./CopperMountainCollegeEntireInterval.html")
I do not know what code to write to display the actual graph, I would appreciate any help I receive.
I am probably a year to late, but the easiest way would be to import IFrame from IPython.display and use it like
from IPython.display import IFrame
IFrame(src='path_to_html', width=500, height=500)
To complete Luca R answer (his solution works fine): In my case though, I noticed that to use IFrame, I needed to place the html file in the same directory as my notebook (or in a subdirectory under the notebook) and then use a relative path. It is probably due to common browser security settings (see more from https://community.plotly.com/t/displaying-html-file-generated-by-plotly-offline-in-a-jupyter-notebook/19586)

Bokeh plots just bring up a blank window

So I've been trying to learn Bokeh recently and everything was going well but suddenly whenever I try and make a Bokeh plot the browser just shows a blank page. I get no error codes just the blank page. This is with programs that I was successfully using to create plots just a couple days ago. I even tried loading a html plot file I'd made a few weeks ago that had worked on co-workers computers and got the same result. I even tried one of the basic example code and got the same blank page.
from bokeh.plotting import figure, output_file, show
p = figure(title="line", plot_width=300, plot_height=300)
p.line(x=[1, 2, 3, 4, 5], y=[6, 7, 2, 4, 5])
show(p)
Never thought to try it but do standard Bokeh plots work if you are not online? Does it callout to an external server to generate the plots and maybe now some IT changes at my work are preventing the plots from being generated?
Thanks for any help!
It does not call out to an external server, but it does require the browser to load a JavaScript library, BokehJS. By default (and by popular demand) BokehJS is loaded remotely from a CDN (specifically, from https://cdn.bokeh.org). Accordingly, viewing a Bokeh plot that is configured to use CDN resource requires an active and working network connection.
But it's possible to use "inline" resources, which means the BokehJS library is included directly in the HTML output that Bokeh (the python library) generates. The easiest way to to do this is to set the environment variable:
BOKEH_RESOURCES=inline
There are other ways to specify resources, though, too. For more details see the documentation.
As an aside, in a situation like this it is helpful to consult your browser's JavaScript console. If the CDN resources can't be loaded, you will see the error there.

Bokeh plots distorting nbviewer slides view

When using the web based iPython Notebook/Jupyter capability, i created a notebook with markdown cells of text and code cells to display the code and plots for matplotlib and bokeh. I then download that as .ipynb open with sublime, copy and paste to git, then access it on nbviewer through my git account. When looking at it here:
http://nbviewer.ipython.org/github/angisgrate/test/blob/master/pyohio3.ipynb
in notebook view, it works fine. the markdown, code, and plot steps are all there.
When switching to slides view, the intent of the creation needed for the presentation, this code blocks occurs first, blocking out the first 10 markdown steps and all the matplotlib steps, rendering this weird code without the plots:
http://nbviewer.ipython.org/format/slides/github/angisgrate/test/blob/master/pyohio3.ipynb
How can i fix this asap?? I've looked through and there was a similar problem in 2014 with slides, but it yielded an actual "Error" that i'm not seeing, just this contorted view
I can't say for certain. It's possible in the nbviewer slides view, the execution of JavaScript is suppressed (this happens on GitHub notebook previews, for instance). All of the rendering in Bokeh actually happens from the JavaScript library BokehJS, so if this is the case, then Bokeh will not function, and there is nothing really that can be done. This is probably a question bset directed towards the nbviewer team/community to find out the specifics of how the slides view behaves (and is intended to behave) with respect to executing embedded JavaScript.
Edit: Looks like this is a known/discussed issue. More information here: Issue #484: Jupyter>>nbviewer slides Fail, notebook view fine by angisgrate in jupyter/nbviewer on GitHub

Categories