Following the instructions here for passing a Flask server to a Dash app, I am trying to set up a multipage app as described here.
My Flask app and the separate dash scripts are
Flask starting page
import sys, os
import dash
from flask import Flask
from dash import Dash, dcc, html
server = Flask(__name__)
server.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@server.route('/dash_test')
def test_app():
return app.index()
app = Dash(__name__, server = server, requests_pathname_prefix='/myapp/', use_pages=True)
app.layout = html.Div([
html.H1('Multi-page app with Dash Pages'),
html.Div(
[
html.Div(
dcc.Link(
f"{page['name']} - {page['path']}", href=page["relative_path"]
)
)
for page in dash.page_registry.values()
]
),
dash.page_container
])
if __name__ == '__main__':
main()
pages/archive.py
import dash
from dash import html, dcc
dash.register_page(__name__, relative_path="/myapp/")
layout = html.Div(children=[
html.H1(children='This is our Archive page'),
html.Div(children='''
This is our Archive page content.
'''),
])
I am getting the errors below:
[Tue Mar 07 09:31:00.494813 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] mod_wsgi (pid=1744228): Failed to exec Python script file '/var/www/wsgi-scripts/myapp.wsgi'.
[Tue Mar 07 09:31:00.494865 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] mod_wsgi (pid=1744228): Exception occurred processing WSGI script '/var/www/wsgi-scripts/myapp.wsgi'.
[Tue Mar 07 09:31:00.495206 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] Traceback (most recent call last):
[Tue Mar 07 09:31:00.495255 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/var/www/wsgi-scripts/myapp.wsgi", line 4, in <module>
[Tue Mar 07 09:31:00.495264 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] from hello import app as application
[Tue Mar 07 09:31:00.495273 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/var/www/wsgi-scripts/hello.py", line 14, in <module>
[Tue Mar 07 09:31:00.495279 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] app = Dash(__name__, server = server, requests_pathname_prefix='/myapp/', use_pages=True)
[Tue Mar 07 09:31:00.495288 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/opt/miniconda3/envs/py38/lib/python3.8/site-packages/dash/dash.py", line 494, in __init__
[Tue Mar 07 09:31:00.495294 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] self.init_app()
[Tue Mar 07 09:31:00.495303 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/opt/miniconda3/envs/py38/lib/python3.8/site-packages/dash/dash.py", line 578, in init_app
[Tue Mar 07 09:31:00.495314 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] self.enable_pages()
[Tue Mar 07 09:31:00.495323 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/opt/miniconda3/envs/py38/lib/python3.8/site-packages/dash/dash.py", line 2045, in enable_pages
[Tue Mar 07 09:31:00.495329 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] self._import_layouts_from_pages()
[Tue Mar 07 09:31:00.495338 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/opt/miniconda3/envs/py38/lib/python3.8/site-packages/dash/dash.py", line 2017, in _import_layouts_from_pages
[Tue Mar 07 09:31:00.495344 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] spec.loader.exec_module(page_module)
[Tue Mar 07 09:31:00.495353 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/var/www/wsgi-scripts/pages/archive.py", line 4, in <module>
[Tue Mar 07 09:31:00.495359 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] dash.register_page(__name__, relative_path="/myapp/")
[Tue Mar 07 09:31:00.495368 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] File "/opt/miniconda3/envs/py38/lib/python3.8/site-packages/dash/_pages.py", line 275, in register_page
[Tue Mar 07 09:31:00.495374 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] if context_value.get().get("ignore_register_page"):
[Tue Mar 07 09:31:00.495391 2023] [wsgi:error] [pid 1744228:tid 140322294171392] [remote 10.5.10.40:55944] LookupError: <ContextVar name='callback_context' at 0x7f9f34aca9a0>
I have modified the code as suggested and still receive the same error.
I have found very little about the cause of LookupError: <ContextVar name=‘callback_context’ or /site-packages/dash/_pages.py", line 275, in register_page
Also, I was able pass a Flask server to a dash app when not using register_page and seperate files.
My initial thought is I have the wrong relative_path, requests_pathname_prefix, url_base_pathname and requests_pathname_prefix options. Trying different permutations of them hasn’t worked yet.