dash-labs
dash-labs copied to clipboard
"No module named 'pages'" when importing module, to use with werkzeug DispatcherMiddleware.
When I import my Dash application as a module, to be dispatched by werkzeug DispatcherMiddleware, I get the following error:
Traceback (most recent call last):
File "dashboards.py", line 5, in <module>
from app_one.app import app as one_app
File "/home/vmatos/experiments/minimal_multipage_dispatcher/app_one/app.py", line 10, in <module>
app = Dash(__name__, plugins=[dl.plugins.pages], requests_pathname_prefix=requests_pathname_prefix)
File "/home/vmatos/experiments/minimal_multipage_dispatcher/venv/lib/python3.8/site-packages/dash/dash.py", line 458, in __init__
plugin.plug(self)
File "/home/vmatos/experiments/minimal_multipage_dispatcher/venv/lib/python3.8/site-packages/dash_labs/plugins/pages.py", line 321, in plug
_import_layouts_from_pages(pages_folder)
File "/home/vmatos/experiments/minimal_multipage_dispatcher/venv/lib/python3.8/site-packages/dash_labs/plugins/pages.py", line 295, in _import_layouts_from_pages
page_module = importlib.import_module(f"pages.{page_filename}")
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'pages'
You can find a minimal replication of the issue in the repository: https://github.com/vmatos/minimal_multipage_dispatcher
Would it be possible to achieve this use case, of having multiple Dash app with the multi-page plugin?
Hi @vmatos
Thanks for providing the repo with the example, I'll see what I can do to make it work with dash-labs, but I expect that it may not work since it's plug-in.
I'm currently working on a PR to move the pages plug-in to dash. It would be very helpful if you could take the pre-release for a spin and see if you get the same results.
You can find the current pre-release tarball based on my PR here: https://github.com/AnnMarieW/dash-flask-login/tree/dash2.4
pip install dash-2.4.0rc3.tar.gz
change the app instantiation to: app = Dash(__name__, use_pages=True)
change dl.plugins.page_container, to dash.page_container,
Thank you @AnnMarieW
I have tested with the rc3 you have provided. The ModuleNotFoundError remains. You can find in branch: https://github.com/vmatos/minimal_multipage_dispatcher/tree/dash-2.4.0rc3
Also in branch: https://github.com/vmatos/minimal_multipage_dispatcher/tree/pythonpath I try to work around the module importing issue. But I find myself with each Dash instantiation deleting the previous pages registries.
@vmatos
But I find myself with each Dash instantiation deleting the previous pages registries.
Could you try using unique names in the first attribute of dash.register_page(), for example in app1:
dash.register_page("another_home1", layout=html.Div("App 1!"), path="/")
Oh, But I did in the other branch.
dash.register_page(__name__+".another_home", layout=html.Div("App 1!"), path='/')
dash.register_page(
__name__+".very_important", layout=html.Div("Don't miss it! 1"), path="/important", order=0
)
In branch https://github.com/vmatos/minimal_multipage_dispatcher/tree/pythonpath-2.4.0rc3 I tried the same hacking of pythonpath to be able to import the modules. On the other hand we dive deep in many other issues that result from differences introduced in this dash version.
- Dash complains about different Flask, than the one already existing.
Exception: Server already exists - Flask blueprint name relies on the
routes_pathname_prefix.ValueError: The name '_dash_assets' is already registered for a different blueprint. Use 'name=' to provide a unique name. - Then it complains that the path are duplicate
Exception: modules ['app_one.app.very_important', 'app_two.app.very_important'] have duplicate paths
I looked into this a little more, and I expect that pages is not compatible with this method. I'll confirm this with the Plotly team and see if they can suggest a work-around.
It may be necessary to use the current pattern for multi-page apps described in the docs Multi-page Apps and URL Support rather than using pages
@AnnMarieW https://github.com/conda-forge/dash-labs-feedstock/pull/8