Embedding bokeh app in django by including the primary key in the url

Hi everyone,

I am trying to embed a bokeh application inside a django template by using the django_embed example as a reference point.
I would like the url path to the django application, however, to include a primary key which I thought should be easy enough to implement but I’m running into trouble.

Here’s what I’m doing.
In the urlpatterns I rewrite the path to sea_surface as follows:

    path("other/path/<int:pk>/sea_surface/", views.sea_surface)

I also update bokeh_apps variable:

    autoload("other/path/<int:pk>/sea_surface", views.sea_surface_handler)

But this gives me a TypeError: unexpected keyword argument ‘pk’ on the sea_surface function which is defined in the views.py as follows:

    def sea_surface(request: HttpRequest) -> HttpResponse:
        script = server_document(request.build_absolute_uri())
        return render(request, "embed.html", dict(script=script))

I am completely new to django so this is perhaps quite trivial but I cannot get it to work, so would appreciate a bit of help.

I’m not a Django expert, but if this is like some other systems, then when you specify:

path("other/path/<int:pk>/sea_surface/", views.sea_surface)

That means that Django will extract that part <int:pk>of the URL, convert it to an int, and pass it as a variable named pk to the handler. Which means that handler necessarily must be able to accept having a parameter pk passed to it. But your current sea_surface handler does not:

def sea_surface(request: HttpRequest) -> HttpResponse: 

As an immediate “solution”, maybe:

def sea_surface(request: HttpRequest, pk: int) -> HttpResponse: 

But presumably you would want your handler to do something with this pk value (or else why pull it out of the URL in the first place).

Right, but the thing I don’t understand is why doing so results in a autoload.js 404 error. If I enter something like in the browser, I get
Not Found: /other/path/23/sea_surface/autoload.js

The script tag generated by server_document looks fine to me as it does include the pk value.
So I don’t understand why embedding the application doesn’t work when adding pk values to the url path.

As for the motivation for doing so, the bokeh applications will be run by signed-in users and will be unique to each user, so I want each application to have their own url address, that is my reasoning for using pk in the first place.

There is only one base URL for a Bokeh application. That is why inserting pk in to the Bokeh URL results in a 404. Bokeh does not know anything about the Django configuration at all, its routes are not affected by anything on the Django side. The call to server_document needs to use the one, unique, standard Bokeh app URL. If you need to pass additional information to a Bokeh app session, the current best way to do that is via HTTP request arguments. You can pass a Python dict to server_document via the arguments parameter. That will add HTTP requests arguments that can be accessed in the app code as described here:


in some upcoming releases it will be possible to send additional information in a JWT-like token as well.

Thank you Bryan for your reply. It certainly does help and I understand now that the issue I’m facing is more complicated than I anticipated. Now I am wondering what the best way to approach this issue in more general terms is, but I am creating a new topic to ask for help with that.