Getting zoom current zoom setting back into python

Following the suggestion here:

https://github.com/bokeh/bokeh/issues/6782#issuecomment-646842658

I’m posting my question here :slight_smile:

The question was:

Ok, I’m a bit of a noob. I’m trying to get the current zoom range after an interactive zoom in a notebook.

I start with this:

import numpy as np

from bokeh.plotting import figure, show
from bokeh.io import output_notebook, push_notebook
from bokeh.models import ColumnDataSource, Range1d
from bokeh.layouts import column, row

import ipywidgets as widgets

output_notebook()

Then I create a plot:

# Magnitude plot
source1 = ColumnDataSource()
x = np.linspace(0,1,100)
y = x**2
xr  = Range1d()
source1.data = dict(x=x, y=y)
p1 = figure(plot_width=600,plot_height=400, x_range = xr)
p1.line('x', 'y', source=source1)
p1.xaxis.axis_label =  "Test"
target = show(p1, notebook_handle=True)

Then I zoom around a bit, and in the next cell I check what the value of the x-range is:

p1.x_range.start

Regardless of what I zoom to, the value of p1.x_range.start is always 0. Am I missing something here?

From the reply of Bryan van der Ven, it seems like I need to do something different than show:

show with notebook handles is for one-way Python->JS updates only. If you want to have bi-directional updates back to Python, you would need to embed a Bokeh server app in the notebook. Happy to discuss but the appropriate venue is the project Discourse:

Any suggestions?

Thanks!
Gary

Here is an example notebook with a Bokeh server app embedded inside:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

Basically, you will need to create a function app(doc) that has all the Bokeh bits self-contained inside including any callbacks you want called when values (e.g like a range start or end) change.

Thank Bryan for the link

I’m only just now starting to grasp how bokeh works.

I typically do my development on a Jupyterhub server (say for arguments sake “https://my.jupyterhub.com”). And then your code did not work out of box at all (displayed nothing).

I tried it on a local notebook server, and indeed, this worked fine. (although I got a popup from my macos firewall asking if I was prepared to allow incoming connections?)

A bit of digging revealed that notebook_url seems to be optional parameter of the show command, and so i tried notebook_url="https://my.jupyterhub.com". But this again gave nothing.

Does anyone know if it is possible to run an embedded Bokeh app on a notebook that is running on a remote jupyterhub?

Thanks!
Gary

I definitely know it is possible, but I have never set up JupyterHub personally, so I am far from an expert. However, AFAIK the information you need is here:

https://docs.bokeh.org/en/latest/docs/user_guide/jupyter.html#jupyterhub

ok, thanks, i’ll give it a go!

btw, doc link seems a bit outdated, it seems nbserverproxy is now depricated and replaced with:

I guess this is what the connection would then look like:

But, then I guess I might have to ask our IT to open up some extra holes in our firewall for incoming connections to our JH server…I’ll have to take a look.

Since you mention a firewall I’ll go ahead and also mention that the Bokeh server operates over a websocket connection, so websocket upgrades will also need to be possible through any network configuration present.

it seems nbserverproxy is now depricated and replaced with:

Was not aware, can you open an issue? (Or ideally a PR to update this file here)

sure, i’ll give it a go. Issue tracker seems complicated…I’ll plan to edit the file directly.

Let me confirm it works though :slight_smile: