I want to incorporate a loading indicator in my app at a custom location, as per the documentation, I tried to use pn.state.busy to determine busyness state. However, when I open the app from a different session, I get the following error
RuntimeError: Models must be owned by only a single document, ImportedStyleSheet(id='p1172', ...) is already in a doc
I understand that this is because pn.state object is shared across session, I am not sure how to avoid this, minimal reproducer, taken from the Bootstrap Template example here -
import hvplot.pandas
import numpy as np
import panel as pn
import pandas as pd
import param
class SampleApp(pn.viewable.Viewer):
loading = pn.indicators.LoadingSpinner(value=True, size=25, styles={'padding-top': '8px'})
def busy_indicator(self, busy):
return pn.WidgetBox(self.loading, styles={'padding-top': '7px', 'border': 'none'}) \
if busy else None
def get_template(self):
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.BootstrapTemplate(
title='BootstrapTemplate',
sidebar=[freq, phase],
)
# Append a layout to the main area, to demonstrate the list-like API
template.main.append(
pn.Row(
pn.bind(self.busy_indicator, pn.state.param.busy), # Binding the busy state param to show a loading indicator
pn.Card(dfi_sine.hvplot(**plot_opts).output(), title='Sine'),
pn.Card(dfi_cosine.hvplot(**plot_opts).output(), title='Cosine'),
)
)
return template
def __init__(self):
self.template = self.get_template()
if __name__.startswith("bokeh"):
SampleApp().template.servable()
I am running the above script using -
panel serve sample_app.py
The above error occurs when I open 2 parallel instances of the app. I also tried using pn.state.sync_busy instead of pn.bind, but it results in the same issue. I even tried binding the busy_indicator function to bootstrap template’s busy indicator, but I get the same error, what should be the apporpriate way to handle this?