norok2
norok2

Reputation: 26896

Bokeh + interactive widgets + PythonAnywhere

I was unable to find a minimal working example for an interactive web app using bokeh and bokeh widgets that runs on PythonAnywhere.

Ideally, I would like to have a simple plot of a relatively complicated function (which I do not know analytically, but I have SymPy compute that for me) which should be replotted when a parameter changes.

All code that I have found so far does not do that, e.g. https://github.com/bokeh/bokeh/tree/master/examples, or refers to obsolete versions of bokeh.

Most of the documentation deals with running a bokeh-server, but there is no indication on how to have this running with WSGI (which is how PythonAnywhere handles the requests). For this reasone I have tried embedding a Bokeh plot within a Flask app. However, as far as I understand, in order to have interactive Bokeh widgets (which should trigger some computation in Python) do require a bokeh-server. I am not particularly attached to using either Flask or Bokeh, if I can achive a similar result with some other simpler tools. Unfortunately, a Jupyter notebook with interactive widgets does not seems to be an option in PythonAnywhere.

I have installed bokeh 0.12 on Python 3.5.

I have managed to run a simple bokeh plot within a flask app, but I am unable to use the Bokeh widgets.

Upvotes: 4

Views: 2958

Answers (2)

bigreddot
bigreddot

Reputation: 34568

As of Bokeh 0.12.5 you can embed Bokeh server applications directly in Jupyter notebooks. This is the best and most robust way to have interactive Bokeh plots and widgets (backed by real python code) in a notebook.

You can study an example of this in this demo notebook:

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

A screencast of that notebook in action is below:

enter image description here

Upvotes: 1

conrad
conrad

Reputation: 1913

Here is a working example of Jupyter notebook with interactive widgets on pythonanywhere:

%pylab inline
import matplotlib.pyplot as plt
from ipywidgets import interact

def plot_power_function(k):
    xs = range(50)
    dynamic_ys = [x ** k for x in xs]
    plt.plot(xs, dynamic_ys)

interact(plot_power_function, k=[1, 5, 0.5])

PythonAnywhere does have the ipywidgets module pre-installed. But if you are not seeing the interactive widgets, make sure that you have run jupyter nbextension enable --py widgetsnbextension from a bash console to get it enabled for your notebooks. You will have to restart the jupyter server after enabling this extension (by killing the relevant jupyter processes from the consoles running processes list on the pythonanywhere dashboard).

Upvotes: 5

Related Questions