Running Panel apps inside Django
Panel generally runs on the Bokeh server which itself runs on Tornado. However, it is also often useful to embed a Panel app in large web application, such as a Django web server. Using Panel with Django requires a bit more work than for notebooks and Bokeh servers.
To run this example app yourself, you will first need to install django (e.g. conda install "django=2"
).
Additionally, you should also install the channels
library (using pip install channels==2
or conda install channels=2 -c conda-forge
). This makes it possible to run bokeh without launching a separate Tornado server.
Note that these examples can also be run with django 3 or django 4 (which will require channels 3), by installing the additional bokeh_django
library. In this case replace all instances of bokeh.server.django
with bokeh_django
in the example below.
Configuration
Before we start adding a bokeh app to our Django server we have to set up some of the basic plumbing. In the examples/apps/django/project
folder we will add some basic configurations.
First of all we need to set up a Asynchronous Server Gateway Interface (ASGI) instead of the usual WSGI setup. For this purpose we add examples/apps/django/project/asgi.py
:
Next we need to ensure the routing is configured correctly to handle a bokeh server in examples/apps/django/project/routing.py
:
Lastly we need to add some configuration to examples/apps/django/project/settings.py
. As a first step we need to add both channels
and bokeh.server.django
to the INSTALLED_APPS
:
Secondly we need to declare the bokehjs_path
as part of the STATICFILES_DIRS
:
Now we need to add any templates we have:
and lastly add the app(s) and static_extensions()
to the urlpatterns
in the urls.py
file:
Now it's time to configure an actual app and add it to our Django server.
Sliders app
Based on a standard Django app template, this app shows how to integrate Panel with a Django view
The sliders app is in examples/apps/django/sliders
. We will cover the following additions/modifications to the Django app template:
sliders/sinewave.py
: a parameterized object (representing your pre-existing code)sliders/pn_app.py
: creates an app function from the SineWave classsliders/apps.py
: how a Django app can import and use Bokeh serversliders/views.py
andtemplates/base.html
: getting the Bokeh app into a Django view
To start with, in sliders/sinewave.py
we create a parameterized object to serve as a placeholder for your own, existing code:
However the app itself is defined we need to configure an entry point, which is a function that accepts a bokeh Document and adds the application to it. In case of the slider app it looks like this:
Next we create a views.py
file which returns a view the Django server can render:
The base.html
template should be in the TEMPLATES
DIRS
directory we declared in the settings.py
file above. A very basic template might look like this but can be as complex as you need:
Next we declare a urls.py
file to declare the urlpattern where to serve the sliders app to Django:
You should be able to run this app yourself by changing to the examples/apps/django
directory and then running: python manage.py runserver
; then visit http://localhost:8000/sliders in your browser to try the app.
Multiple apps
This is the most basic configuration for a bokeh server. It is of course possible to add multiple apps in the same way and then registering them with Django in the way described in the configuration section above. To see a multi-app Django server have a look at examples/apps/django_multi_apps
and launch it with python manage.py runserver
as before.