4

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.

6
  • Bokeh server apps require the use of websockets. Does PythonAnywhere provide for forwarding websocket connections? If not, this may not be technically feasible. Also, almost every single example at the link you gave show apps that "update something when a parameter is changed" so the comment about them "not doing that" is confusing. Lastly, Bokeh server apps can use a Jinja template, so if your app is a single-page app you can most easily just do it all entirely as a bokeh server app (no flask) Commented Aug 11, 2016 at 15:07
  • I do understand that bokeh is perfectly capable of that. Probably what I am missing is a way of hooking the bokeh server to WSGI. As for the comment on the code found so far, I obviously mean that either does not easily work with PythonAnywhere or it is not able of replotting the function (unless I go through an HTTP request, and giving up on using the beutiful bokeh widgets). Commented Aug 11, 2016 at 15:17
  • I'm afraid I don't understand what "hooking the bokeh server to WSGI" means. If that precludes websocket connections, this this will simply not work. Bokeh server must be able to open and maintain websocket connections to clients. As an aside, this recent mailing list thread might be useful/relevant: groups.google.com/a/continuum.io/forum/?pli=1#!topic/bokeh/… Commented Aug 11, 2016 at 16:27
  • 1
    I am relatively new to PythonAnywhere, let alone web development in Python, but from my little understanding, I do not really have control over the actual serving script, but as far as I know, websockets should not be blocked. The example you sent is actually pretty interesting and I might give it a try over the week-end... if successful I will write a minimal working example on the matter. Commented Aug 12, 2016 at 9:56
  • 2
    pythonanywhere webapps do not support web socket connections Commented Aug 12, 2016 at 12:02

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. Unless I am missing something obvious, I see no easy way of configure PythonAnywhere to serve a Jupyter notebook. Could that be because my plan (free) does not seem to support Jupyter notebooks?
The question specifically asks about Bokeh, not MPL.
Additionally, I wonder whether with Jupyter is possible to limit the generic user interaction to the widget I expose.
You will need a paying plan for juptyer notebooks right now. but you won't be able to show running notebooks to the world (because then people can do malicious stuff such as wiping your account).
1

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

1 Comment

Thanks for sharing this! Pretty nice! Unfortunately, this does not solve the issue of the PythonAnywhere support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.