plotly / dash-labs

Work-in-progress technical previews of potential future Dash features.
MIT License
139 stars 39 forks source link

"No module named 'pages'" when importing module, to use with werkzeug DispatcherMiddleware. #91

Open vmatos opened 2 years ago

vmatos commented 2 years ago

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?

AnnMarieW commented 2 years ago

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,

vmatos commented 2 years ago

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.

AnnMarieW commented 2 years ago

@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="/")
vmatos commented 2 years ago

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.

AnnMarieW commented 2 years ago

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

IcToxi commented 2 years ago

@AnnMarieW https://github.com/conda-forge/dash-labs-feedstock/pull/8