Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 468
pfincent Avatar asked Jul 25 '18 07:07

pfincent


2 Answers

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).

like image 58
bigreddot Avatar answered Nov 06 '22 05:11

bigreddot


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.

like image 44
martin-martin Avatar answered Nov 06 '22 07:11

martin-martin