Page not found for Multipage app when running the code as a module

Dash version 2.2.0 and dash-labs is installed

I am creating a multipage app with the latest dash version and trying to execute the scripts as python package. I get an error of page not found even when the pages folder exists with the relevant .py files. However, when I run the file (app.py) as a script (from Pycharm’s Run button option in the IDE), the app works fine.

I am using the example from Adams github repo: Dash-by-Plotly/Dash_More_Advanced_Shit/Multipage_app at master · Coding-with-Adam/Dash-by-Plotly · GitHub

The folder hierarchy is:

├───folder1
│   ├───folder2
│   │   └───pages
│   │       app.py   

And I run the script as python -m folder1.folder2.app

Is there anything extra step to run the script as a module?

Note: I was already using dash all this time and running it as a module which worked fine. Now, I want to upgrade the app to multipage app.

Thanks!

Did you try moving into “folder2” and running the app from there?

Yes, moving into folder2 and then executing the code as a module works!

However, in my original project, I need to run my code from the root directory (which is equivalent to folder1 having several sub-folders). There would be problem in calling other modules if I move to the folder containing app.py for execution.

Any solution regarding how can I execute the script from folder1?

I think that your issue can be considered as a bug in the pages plugin. What do you say, @AnnMarieW ? :slight_smile:

1 Like

I believe you can work around the issue by adding the following snippet at the top of app.py,

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

I get ModuleNotFoundError as follows:

File “D:\ …\lib\site-packages\dash_labs\plugins\pages.py”, line 292, in _import_layouts_from_pages
page_module = importlib.import_module(f"pages.{page_filename}")

image

At the same time, I would like to point out that in Pycharm, I register_page and page_registry are not found as shown below from the editor snip. Note that still the program runs successfully (of course executed as a script or as a module from the same path). Any information on this?

image

image

Hi @hundredmiles and @Emil

Yes, this is a bug in the pages plugin. I fixed it in the pull request to add\pages to dash, but it looks like it may be a while until that PR is reviewed. I’ll do a PR to fix it in dash-labs as well.

1 Like

Thanks @AnnMarieW for the information. Will wait for the fix. Also, thanks @Emil for your insights.

@AnnMarieW This bug you mentioned 2 posts up (in 2022) is solved I guess?
I am running into some issues with pages not being found, but the likelihood is high that I made a coding mistake. I am looking for clues in the forum right now.

Hi @plotplot and welcome to the Dash community :slight_smile:

Yes, this issue has been solved. As you noted, this thread is quite old.

Can you make a minimal example that replicates the issue? You can find lots of examples here:

Thank you very much @AnnMarieW .
I started new thread with example here: