☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

How to Bokeh serve + Django

Community Supportserver
yvechang
yvechang

I am trying to study the django_embed example. I also followed the instructions of ejprinz. However, all of the following environments got different kinds of the following errors

  1. bokeh-1.4.0 + panel-0.8.3 + Django 3.2 + django-param 0.1.10
    => File “C:\Users\yvechang\webapps\virtualenv\django_embed\lib\site-packages\django\apps\config.py”, line 38, in init
    raise ImproperlyConfigured(
    django.core.exceptions.ImproperlyConfigured: The app label ‘bokeh.server.django’ is not a valid Python identifier.

  2. bokeh-1.4.0 + panel-0.8.3 + Django 2.2 + django-param 0.1.10
    =>File “”, line 991, in find_and_load
    from bokeh.server.django import autoload, directory, document, static_extensions
    ImportError: cannot import name ‘static_extensions’ from ‘bokeh.server.django’ (C:\Users\yvechang\webapps\virtualenv\django_embed\lib\site-packages\bokeh\server\django_init
    .py)
    File “”, line 975, in _find_and_load_unlocked

⌁   Link to post
Bryan
Bryan

@yvechang For starters, if you are going to run with old major versions of Bokeh you definitely need to look at the example code that is specific and appropriate for that version. New features are continually added, and example code is updated to use them. It’s common and expected that the latest examples don’t work with old versions of Bokeh, because the latest versions of examples use features that don’t exist in old Bokeh versions.[1]

Here is the code for that same example, that was published with version 1.4

bokeh/examples/howto/server_embed/django_embed at 1.4.0 · bokeh/bokeh · GitHub


  1. In this case, support for package relative imports like static_extensions was a feature only added in Bokeh 2.0 ↩︎

⌁   Link to post
yvechang
yvechang

Hi Bryan,
I had replaced all files to version 1.4. However, the followinf error occurred:
…
raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured: The app label ‘bokeh.server.django’ is not a valid Python identifier.
…

If I changed the line 41 in apps.py into

name = 'bokeh.server.django'
    label = 'bokeh_server_django'

Then, the following error occurred:
No installed app with label ‘bokeh.server.django’. Did you mean ‘bokeh_server_django’?

Does I missed updating something else?

⌁   Link to post
Bryan
Bryan

@yvechang I actually am not really going to be able to help. I haven’t used Django personally in more than 10 years, so I am really no kind of expert at all on what those Django errors signify. The only thing that comes to mind is that maybe your current Django version is also too new. Hopefully someone else in the forum may have more relevant experience.

⌁   Link to post
Bryan
Bryan

@yvechang if you very specifically need the Bokeh server integration with Django, then you might be advised to look elsewhere for now. That corner of Bokeh is just not currently maintained, really. Perhaps it should be removed entirely.

However, since I don’t know your actual requirements, I should mention another possibility for completeness. If you just need to embed standalone Bokeh content (which can be plots and even widgets with CustomJS callbacks) then all the APIs for standalone embedding will work fine with Django:

In fact you could even use the “standard” Bokeh server app embedding functions server_document and server_session with Django, as long as the Bokeh server was running separately. It’s only the very specific and narrow feature for “direct” Django / Bokeh server integration that is currently in bad shape.

⌁   Link to post
yvechang
yvechang

My current project is developing a web-base monitor system of a factory. Some views are like the following figures.


The data of these figures comes from a postgresql database, and the data will be updated dynamically. The objective is updating these figures with newest information automatically or periodically. I think all these figures can be showed with bokeh plots or figures. The only bokeh’s way l knew is callback function. If I want the these figures updated periodically, I have to using bokeh serve. In a web system, users could choose any figure. I don’t know how to use bokeh components to developing my web system.

⌁   Link to post
yvechang
yvechang
  1. I didn’t see anything after run “python ajax_source.py”.
  2. Does multiple python files can run simultaneously? If the answer is “yes”, then the output html file can be loaded simultaneously.
  3. If the answer of question #2 is “yes”, Django seems not necessary any more?
⌁   Link to post
Bryan
Bryan

@yvechang I guess it seemed Django was a pre-existing requirement. If that is not the case, if you don’t need Django for some external reason, then you could just create a “normal” Bokeh server app (i.e. run with the bokeh serve command) as described here:

That kind of Bokeh server app usage (without Django) is very well known, established, and supported for many years now.

  1. I didn’t see anything after run “python ajax_source.py”.

You mean a browser did no open up the page automatically? If not you will have to open up the ajax_source.html output file manually. We just use the built-in standard Python webbrowser module to try to open a browser for you, but if that does not work it’s not really a Bokeh issue, and we can’t do anything about it.

⌁   Link to post
Bryan
Bryan

Right, the toy example in the repo does not set any CORS headers on the /data endpoint, and some browsers apparently don’t like that. Any real API endpoint that you would point AjaxDataSource at would presumably not have this issue. If you’d like to make a PR to update the example to set the Access-Control-Allow-Origin header in the response that would certainly be appreciated.

⌁   Link to post
ejprinz
ejprinz

@yvechang. First, the point of my post was to get a bug fix going for bokeh so it can work with Django 3.2. Until this is done, we are using Django==3.1.8, bokeh==2.3.1, panel==0.11.1, and channels<3.
We are using the NGINX server to handle https:// and steer http:// to https://. We use Hypercorn==0.11.2 to serve Django as follows:
…venv/bin/hypercorn
–bind localhost:8001 .asgi:application
–debug
–graceful-timeout=3
–websocket-ping-interval=1

So this is the setup, in the Django app we do some more things but I don’t know all the details and we don’t have a simple stripped down example. We did get it working though.

⌁   Link to post
ejprinz
ejprinz

@yvechang I think you may have an issue in your //settings.py file. The Bokeh relevant pieces are as follows. Also make sure you really have Django==3.1.8 in your venv, maybe check with pip freeze | grep Django. We are not using django_embed.

'''
Django settings for <project_name> project.

Generated by 'django-admin startproject' using Django 3.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
'''

import ...
import logging
import logging.config
from datetime import datetime

# extra for Panel support
from bokeh.settings import bokehjsdir, settings

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

... 

# Bokeh
ASGI_APPLICATION = '<project_name>.routing.application'

# Application definition
# for Panel support apps channels and bokeh.server.django were added

INSTALLED_APPS = [
        ...,
        'channels',
        'bokeh.server.django',
        ]

MIDDLEWARE = [
        ...
        ]

ROOT_URLCONF = '<project_name>.urls'


# CSRF Information Needed so Daphne/Django trusts CSRF cookies from NGINX
CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS

# added <Bokeh app> templates to TEMPLATES for Panel support
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'sliders', 'templates'),
            ...
                 ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

...

# for Panel added bokehjsdir to static files dir
STATICFILES_DIRS = [
        (os.path.join(BASE_DIR, 'static')),
        bokehjsdir(),
        ]
⌁   Link to post

End of archived discussion · Replies are disabled