I have created a panel dashboard and I want to integrate it with a Flask app. I have had some success in doing so, but I encountered a problem. When a user logs out and reloads the panel dashboard, it does not appear and instead shows an error message.
RuntimeError: Models must be owned by only a single document, ImportedStyleSheet(id=’p1007′, …) is already in a doc
500 GET /bkapp/autoload.js?bokeh-autoload-element=ff952a09-a902-4d77-91fb-1d058116d66c&bokeh-app-path=/bkapp&bokeh-absolute-url=http://localhost:5007/bkapp (::1) 549.87ms
while i run the code which as
`
# Bokeh application handler
def modify_doc(doc):
dashboard = JiraDashboard() # Instantiate your Panel dashboard or components
pn_dashboard = pn.Column( # Create a Panel layout
pn.Row(dashboard.param, dashboard.update_charts), # Add parameters and update controls
dashboard.worklog_chart() # Display the worklog chart
)
doc.add_root(pn_dashboard.get_root())
# Function to find an available port
def find_available_port(port):
while True:
try:
with socket.create_connection(('localhost', port), timeout=1):
port += 1
except socket.error:
return port
# Initialize Bokeh server with an available port
bk_server_port = find_available_port(5006)
bk_server = None
bk_worker_thread = None
def start_bokeh_server():
global bk_server, bk_worker_thread
bk_server = Server({'/bkapp': modify_doc}, io_loop=IOLoop(), port=bk_server_port, allow_websocket_origin=["localhost:5000","127.0.0.1:5000"])
# Bokeh server worker
def bk_worker():
bk_server.start()
bk_server.io_loop.start()
bk_worker_thread = Thread(target=bk_worker)
bk_worker_thread.daemon = True
@app.route('/dashboard')
def dashboard():
if 'google_token' not in session:
return redirect(url_for('login'))
# Ensure that the Bokeh app is started on a separate thread
if not bk_worker_thread.is_alive():
bk_worker_thread.start()
script = server_document(url=f'http://localhost:{bk_server_port}/bkapp')
return render_template_string(f'''
<!doctype html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h1>Panel Dashboard</h1>
{script}
<a href="/logout"><button>Logout</button></a>
</body>
</html>
''')
def reload_dashboard():
global bk_server, bk_worker_thread
if bk_server:
bk_server.io_loop.add_callback(bk_server.io_loop.stop)
bk_worker_thread.join()
start_bokeh_server()`
I try to close the broken session when the user logs out. I use the session_id for the broken session but did not see any positive impact.
Abhinandan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.