Plotly + iPython Notebook - Plots Disappear on Reopen - python

When I create a notebook with plotly plots, save and reopen the notebook, the plots fail to render upon reopening - there are just blank blocks where the plots should be. Is this expected behavior? If not, is there a known fix?

Check to make sure your notebook is marked as "trusted", either in the top-right corner of the notebook, or in the File menu.
My notebooks are trusted by default, but I managed to reproduce your observed behaviour by temporarily removing my ~/Library/Jupyter/nbsignatures.db file, which forces Jupyter to run the notebook as untrusted. Clicking the tile in the top-right corner to trust the notebook fixed the issue for me.
This page in the jupyter docs, Security in notebook documents explains further:
The security problem we need to solve is that no code should execute just because a user has opened a notebook that they did not write. Like any other program, once a user decides to execute code in a notebook, it is considered trusted, and should be allowed to do anything.

I also meet this annoying issue. I find no way to reveal them on the notebook, but I find a compromise way to display them on an html page that is File -> Print Preview.

The reason as documented here:
Note: Default renderers persist for the duration of a single session,
but they do not persist across sessions. If you are working in an
IPython kernel, this means that default renderers will persist for the
life of the kernel, but they will not persist across kernel restarts.
As indicated in the troubleshooting documentation, "JupyterLab Problems" section, You have two options to solve the problem:
calling fig.show("notebook") instead of just fig.show().
If this problem is recurrent, you can use this:
import plotly.io as pio
pio.renderers.default='notebook'
Credits for here

Related

Cells unreadable/un-editable in Jupyter dashboards extension

I am having an issue in the jupyter dashboards extension:
https://jupyter-dashboards-layout.readthedocs.io/en/latest/getting-started.html#
When I move to grid view, all cells get stacked on top of each other making it impossible to read or even edit. Additionally, it will not let me switch back to notebook view without exiting and reopening the notebook.
This question seems like it has been asked:
Cell stacking on each other in Jupyter dashboard view
but I don't think it's actually been answered. Has anybody else come across this issue?

Is there a way to autohide/autocollapse code cells after first execution in jupyter notebook?

I want to be able to autohide code cells after they have run once in a jupyter notebook.
I am calling a function that prints some output. For example
print("Hello World")
The problem I am trying to solve is that after the output is printed, the code block stays there. I can use ctrl k + ctrl o to hide the code block after execution however there are many code blocks like that and I want it to be automatic. I tried using some javascript (https://habr.com/en/post/439570/) however that didn't work as expected. I am aware that I can use NBconvert to hide all the code cells while generating a pdf however I want only some cells to be hidden and that to in the raw notebook.
I looked into how we can add metadata in the cell to hide it with python here: https://jupyterbook.org/content/metadata.html
However, I want to do it locally on some cells, and I want to prevent file changed, do you want to overwrite your notebook prompts.
Is this possible?
For Vscode, refer to the official documentation https://code.visualstudio.com/docs/datascience/jupyter-notebooks, however I couldn't find anything related to autohide/collapse when using Vscode. They might release this functionality in their next release.

How to get ipywidgets interact to work in HTML?

I have a Jupyter notebook with the following cell:
from ipywidgets import widgets, interact
slider = widgets.IntSlider()
def print_val(v):
print(v)
interact(print_val,v=slider)
This works fine in the notebook - when I change the slider the printed output changes. But when I use nbconvert to convert the notebook into HTML, the slider still renders fine in the output HTML, but has no effect on the printed output.
Any idea how to get this to work? Maybe ipywidgets is not the right library for this kind of stuff?
Thanks!
I think you miss what nbconvert does. This utility is needed for converting notebooks to other formats, such as PDF, LATEX, etc. It is used for sharing and presentation. One loses the interactivity option once the notebook is converted.
The interactivity of the Jupyter notebook is provided by the Jupyter server that is started when you invoke jupyter notebook from command line.
So when you save notebook as a web page or convert it to HTML, you don't have the server back end part that processes all the changes. It is a static web page. Because the slider is a standard element it is rendered correctly, but nothing happens when you slide it: there is nothing "attached" to it to handle the events.
At the end, in order to be able to interact with Jupyter notebook, you need the some kind of Jupyter server to be running. It can be a local server if you need it only for yourself, or you can use Google Colab (or Jupyter Hub) if you want to collaborate with others.
Also, there are several solutions that allow one to have some interactivity on the graphs, like Bokeh and Plotly. They also require you to run some kind of server.
Of course, all that is relevant only if you need Python to process your data. If your needs are simple as showing the slider value you better off with a simple Javascript, that can handle the slider changes right in the browser, without the need to run any server at the back end.
An interesting option, although not a direct solution: voila.

What happened to the Python interactive window?

I am working on MacOS v10.14.6 and using the Python interactive window of VScode v1.38.1.
I write code in a .py file and use #%% to create cells and shift+enter to run them in the interactive window.
Today the layout of the interactive window changed, but I didn't do any update and I did not change my settings.
It now shows icons on the top left (as on the screenshot below) instead of showing them on the top right, as it was the case before (as shown in the tutorial).
To me it looks like that doing shifts+enter now launchs a jupyter notebook similar to the one now supported by vscode, because the icons are similar. See the ones on this tutorial here.
The really annoying thing is that now my plots have a dark background, instead of a white one as it was the case before.
I checked and the option python.dataScience.ignoreVscodeTheme is still set on True.
Do you have an idea what happened ?
Thanks for the info Louis. So you are still seeing the interactive window there, not the new notebook experience, we just made some tweaks to the icons and icon locations to match the interactive window up better with the new experience. However while much of that is expected we did break the ignoreVscodeTheme setting. We have an issue filed on that there and it's on our immediate list to fix. Sorry about that, and thanks for reporting.
https://github.com/microsoft/vscode-python/issues/7847

How to stop bokeh from opening a new tab in Jupyter Notebook?

First of all, before this is tagged as duplicate, I have read the other solutions and unfortunately none of them worked for me.
My problem is that I want to display a bokeh plot in Juypter Notebook (and only in Jupyter Notebook), not in a new tab/window.
In the official documentation here I am told that I only need to change
output_file
to
output_notebook
Even though the plot is now displayed inline if I do that, bokeh won't stop also opening a new tab and needlessly displaying the plot there.
Since I'm gonna create lots of plots in my project, it'd be very nice to not always have to close this new tab and return to notebook, but just have it stop creating new tabs, just as it would work with e.g. matplotlib.
What confuses me is that if I load up the official tutorial and enter code there, for example
import numpy as np
x = np.linspace(0, 10, 100)
y = np.exp(x)
p = figure()
p.line(x, y)
show(p)
there is no new tab opened. If I now run the same code locally on my machine's Juypter Notebook, it does open up a new tab.
I've been trying for a while now to fix this, any help would be very much appreciated.
Thanks in advance, Vincent
You need to call output_notebook at the top of your notebook, but only call output_notebook. If you call output_file at all, that activates a persistent mode that saves output to files, and causes show to open new tabs with those files. You would need to call reset_output to clear that persistent mode.
As a convenience, since several users asked for it, if no output mode is supplied, show behaves as though output_file was called as a default. The reason a tab is not opened from the Binder tutorial is because it is not technically possible for code running remotely on Binder server to open a tab on your local browser (which is a very good thing).
Adding an explicit example to #bigreddot's answer:
You might have ran bokeh.io.output_file() somewhere in your notebook, to save noteable graphs. However, now you only want to experiment with some plots quickly to inspect the data.
Simply reset your settings to stop saving to file in any cell in your notebook like so:
import bokeh.io
# this is here only for completeness to clarify where
# the methods are nested (you probably already imported this earlier)
bokeh.io.reset_output()
bokeh.io.output_notebook()
You can activate saving to file again later to keep the interesting graphs.
You can import:
from bokeh.plotting import output_notebook
And call output_notebook before your figures declaration, then you just show the figure using show. See the doc.

Categories

Resources