Skip to content

Unable to switch theme when using panel with fastapi #8493

@ludwigHoon

Description

@ludwigHoon

ALL software version info

(this library, plus any other relevant software, e.g. bokeh, python, notebook, OS, browser, etc should be added within the dropdown below.)

Software Version Info ```plaintext python 3.14.3 bokeh 3.8.2 bokeh_fastapi 0.1.6 panel 1.8.9 uvicorn 0.41.0 ```

Description of expected behavior and the observed behavior

When visiting the site at 127.0.0.1:8000/panel, I should be able to toggle the dark mode, but it is not changing. When visiting 127.0.0.1:8000/panel?theme=dark, no error is created, but the dark mode is not toggled.

Complete, minimal, self-contained example code that reproduces the issue

import hvplot.pandas
import numpy as np
import panel as pn
from fastapi import FastAPI
from panel.io.fastapi import add_application

import pandas as pd

xs = np.linspace(0, np.pi)

freq = pn.widgets.FloatSlider(name="Frequency", start=0, end=10, value=2)
phase = pn.widgets.FloatSlider(name="Phase", start=0, end=np.pi)

def sine(freq, phase):
    return pd.DataFrame(dict(y=np.sin(xs*freq+phase)), index=xs)

def cosine(freq, phase):
    return pd.DataFrame(dict(y=np.cos(xs*freq+phase)), index=xs)

dfi_sine = hvplot.bind(sine, freq, phase).interactive()
dfi_cosine = hvplot.bind(cosine, freq, phase).interactive()

plot_opts = dict(responsive=True, min_height=400)

# Instantiate the template with widgets displayed in the sidebar
template = pn.template.FastGridTemplate(#.EditableTemplate(
    #editable=True,
    title='EditableTemplate',
    sidebar=[freq, phase],
)

template.main[:2, :5] = dfi_sine.hvplot(title='Sine Plot', **plot_opts).output()
template.main[:2, 7:] = dfi_cosine.hvplot(title='Cosine Plot', **plot_opts).output()

app = FastAPI()

@add_application('/panel', app=app, title='My Panel App')
def create_panel_app():
    slider = pn.widgets.IntSlider(name='Slider', start=0, end=10, value=3)
    return template #slider.rx() * '⭐'

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app)

Stack traceback and/or browser JavaScript console output

Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x3063ab0e0>>, <Task finished name='Task-65' coro=<ServerSession.with_document_locked() done, defined at /Users/ludwig/Documents/c2/.pixi/envs/default/lib/python3.14/site-packages/bokeh/server/session.py:77> exception=RuntimeError("Unexpected ASGI message 'websocket.send', after sending 'websocket.close' or response already completed.")>)
Traceback (most recent call last):

Screenshots or screencasts of the bug in action

  • I may be interested in making a pull request to address this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions