How do I serve static assets like images when running my apps via panel.serve?

I’m using panel.serve to serve multiple apps.

Some of these apps need to display images. For various reasons it would be easier for me to serve them as a part of the application than serving them separately.

HOW DO I DO THAT?


According to https://docs.bokeh.org/en/latest/docs/user_guide/server.html#directory-format you can put static assets in a static folder and they will be served with bokeh serve.

Inspired by that I’ve tried setting up the following.

image

import panel as pn

def create_app():
    return pn.Spacer(background="blue", sizing_mode="stretch_both", name="App2")

APP_ROUTES = {
    "app1": create_app,
    "app2": create_app,
}

pn.serve(APP_ROUTES, port=14033)

It gives me

image

image

but no image is served at

and not at the following either.

$ python app.py
Launching server at http://localhost:14033
WARNING:tornado.access:404 GET /app1/static/spinner_trading_analytics.gif (127.0.0.1) 1.00ms
WARNING:tornado.access:404 GET /favicon.ico (127.0.0.1) 1.00ms
WARNING:tornado.access:404 GET /app1/static/spinner_trading_analytics_light (127.0.0.1) 0.00ms
WARNING:tornado.access:404 GET /static/spinner_trading_analytics_light (127.0.0.1) 1.00ms
WARNING:tornado.access:404 GET /spinner_trading_analytics_light (127.0.0.1) 1.00ms
WARNING:tornado.access:404 GET /app1/spinner_trading_analytics_light (127.0.0.1) 0.00ms
WARNING:tornado.access:404 GET /app1/static/spinner_trading_analytics_light (127.0.0.1) 0.00ms
WARNING:tornado.access:404 GET /spinner_trading_analytics_light.gif (127.0.0.1) 0.00ms
WARNING:tornado.access:404 GET /static/spinner_trading_analytics_light.gif (127.0.0.1) 2.00ms
WARNING:tornado.access:404 GET /app1/static/spinner_trading_analytics_light.gif (127.0.0.1) 0.00ms
WARNING:tornado.access:404 GET /app1/spinner_trading_analytics_light.gif (127.0.0.1) 0.00ms

I really don’t know how to solve that but I noticed that the bokeh docs you mention are about how to serve an app in a directory format. You should maybe try to run panel serve yourappdirectory. The bokeh docs say that a main.py file is the minimum requirement, not sure it’s needed here as the Panel docs mention panel serve allows directories as positional arguments (https://panel.holoviz.org/user_guide/Deploy_and_Export.html).

Let us know whether you manage to fix that and how, that’s interesting :slight_smile:

Thanks @maximlt.

I believe I cannot use the directory format as I would like to serve multiple apps and not just one app.

At least I would not know how to serve multiple apps via the directory format. So if somebody knows how. Please let me know. Thanks.

I wonder if the answer to this post and the other two I have about panel.serve is that I should really be running Panel inside Django for my use case.

This is an old thread, but it came up when I was searching for an answer to this topic so I figured I would post the solution that I found.

I’m not sure when this was added, but I found these docs to be very helpful:

https://panel.holoviz.org/user_guide/Deploy_and_Export.html#static-file-hosting

Basically there is a command line option to panel serve that lets you specify additional static dirs:

panel serve some_script.py --static-dirs assets=./assets
1 Like