Skip to content

Dash Pages bug fixes and enhancements for dash_pages param and layout module imports #2392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ab8de66
Fix bug where custom pages_folder wouldn't force use_pages=True
ned2 Jan 12, 2023
818d704
add two new pages folder tests and fix bug revealed
ned2 Jan 13, 2023
f6990c5
add more test_pages_folder_path_config and refactor to use parameteri…
ned2 Jan 14, 2023
0464477
fix bug where pages_folder can't take absolute paths and add tests
ned2 Jan 14, 2023
2070c4f
refactor pages_folder_config and add more tests
ned2 Jan 14, 2023
89d88b7
make pages_folder_config work with pathlib.Path inputs
ned2 Jan 14, 2023
fa1bf85
make pages_folder support pathlib.Path values
ned2 Jan 15, 2023
4e09114
add new test for pages_folder with absolute path
ned2 Jan 15, 2023
c833e99
add integration test for pages_folder with absolute path
ned2 Jan 15, 2023
d79c683
clear PAGES_REGISTRY after each pages integration test to avoid clash…
ned2 Jan 15, 2023
3426f62
move pages test dedicated folder
ned2 Jan 16, 2023
1e67a65
fix bug where pages_folder with underscores or upper case chars had w…
ned2 Jan 17, 2023
948a993
make Dash.pages_folder read-only
ned2 Jan 17, 2023
4f4d8fd
replace raised bare Exception with exception.PageError
ned2 Jan 17, 2023
fdeadde
add more infer_pages_path tests and improve variable naming
ned2 Jan 17, 2023
02c5ca5
fix borked long module names for pages files when pages_folder is abs…
ned2 Jan 22, 2023
2eedb00
Support page_folder 1 level below proj root and add clear_page_regist…
ned2 Jan 22, 2023
51404ed
Fix flaky tests broken by dirty _pages.CONFIG
ned2 Jan 22, 2023
21515af
Properly fix flaky test
ned2 Jan 23, 2023
900db46
Refactor test_pages_folder path tests to single parameterised test
ned2 Jan 23, 2023
1b3b6a4
add infer_path test case for nested directory paths
ned2 Jan 24, 2023
74252a8
Fix imports of modules by Dash Pages feature
ned2 Jan 27, 2023
7f9ed57
refactor _infer_module_name
ned2 Jan 27, 2023
03271db
add tests for _infer_module_name helper functions
ned2 Jan 27, 2023
11dbf86
Change default use_param=None and value of False now disables Pages e…
ned2 Mar 12, 2023
7d6d380
remove unused import
ned2 Mar 12, 2023
6fcf53b
Merge branch 'dev' into dash-pages-path-fixes
alexcjohnson Mar 15, 2023
04bbf0d
changelog for pages improvements
alexcjohnson Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix imports of modules by Dash Pages feature
  • Loading branch information
ned2 committed Mar 12, 2023
commit 74252a8416efb3ea9fb2d954d6d65336814936e4
39 changes: 31 additions & 8 deletions dash/_pages.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from os import listdir
from os.path import isfile, join
import collections
from urllib.parse import parse_qs
from fnmatch import fnmatch
import os
import re
import sys
from fnmatch import fnmatch
from pathlib import Path
from os.path import isfile, join
from urllib.parse import parse_qs

import flask

Expand Down Expand Up @@ -34,7 +34,7 @@ def _infer_image(module):

if os.path.exists(assets_folder):
files_in_assets = [
f for f in listdir(assets_folder) if isfile(join(assets_folder, f))
f for f in os.listdir(assets_folder) if isfile(join(assets_folder, f))
]
app_file = None
logo_file = None
Expand Down Expand Up @@ -66,9 +66,9 @@ def _module_name_to_page_name(module_name):
def _infer_path(module_name, template):
if template is None:
if CONFIG.pages_folder:
pages_folder = str(Path(CONFIG.pages_folder).name)
pages_module = str(Path(CONFIG.pages_folder).name)
path = (
module_name.split(pages_folder)[-1]
module_name.split(pages_module)[-1]
.replace("_", "-")
.replace(".", "/")
.lower()
Expand All @@ -83,6 +83,29 @@ def _infer_path(module_name, template):
return path


def _module_name_is_package(module_name):
return (
module_name in sys.modules
and Path(sys.modules[module_name].__file__).name == "__init__.py"
)


def _infer_module_name(page_path):
page_path = page_path.replace("\\", "/")
_, _, filename = page_path.partition(CONFIG.pages_folder.replace("\\", "/") + "/")
module = filename.replace(".py", "").replace("/", ".")
proj_root = flask.helpers.get_root_path(CONFIG.name)
parent_module = CONFIG.pages_folder
if CONFIG.pages_folder.startswith(proj_root):
parent_module = parent_module[len(proj_root) :]
parent_module = parent_module.lstrip("/").replace("/", ".")
module_name = f"{parent_module}.{module}"
if _module_name_is_package(CONFIG.name):
# Only prefix with CONFIG.name when its an imported package name
module_name = f"{CONFIG.name}.{module_name}"
return module_name


def _parse_query_string(search):
if search and len(search) > 0 and search[0] == "?":
search = search[1:]
Expand Down
27 changes: 7 additions & 20 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

from . import _pages
from ._pages import (
_infer_module_name,
_parse_path_variables,
_parse_query_string,
)
Expand Down Expand Up @@ -1987,9 +1988,7 @@ def verify_url_part(served_part, url_part, part_name):
self.server.run(host=host, port=port, debug=debug, **flask_run_options)

def _import_layouts_from_pages(self):
walk_dir = self.config.pages_folder

for (root, dirs, files) in os.walk(walk_dir):
for root, dirs, files in os.walk(self.config.pages_folder):
dirs[:] = [
d for d in dirs if not d.startswith(".") and not d.startswith("_")
]
Expand All @@ -2000,29 +1999,17 @@ def _import_layouts_from_pages(self):
or not file.endswith(".py")
):
continue
with open(os.path.join(root, file), encoding="utf-8") as f:
page_path = os.path.join(root, file)
with open(page_path, encoding="utf-8") as f:
content = f.read()
if "register_page" not in content:
continue

page_filename = os.path.join(root, file).replace("\\", "/")
_, _, page_filename = page_filename.partition(
walk_dir.replace("\\", "/") + "/"
)
page_filename = page_filename.replace(".py", "").replace("/", ".")

proj_root = flask.helpers.get_root_path(self.config.name)
parent_module = self.config.pages_folder
if self.config.pages_folder.startswith(proj_root):
parent_module = parent_module[len(proj_root) :]
parent_module = parent_module.lstrip("/").replace("/", ".")
module_name = f"{parent_module}.{page_filename}"

spec = importlib.util.spec_from_file_location(
module_name, os.path.join(root, file)
)
module_name = _infer_module_name(page_path)
spec = importlib.util.spec_from_file_location(module_name, page_path)
page_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(page_module)
sys.modules[module_name] = page_module

if (
module_name in _pages.PAGE_REGISTRY
Expand Down
Empty file added tests/unit/pages/__init__.py
Empty file.
16 changes: 9 additions & 7 deletions tests/unit/pages/test_pages.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sys
from pathlib import Path

import pytest
import dash
from dash import Dash, _pages
from mock import patch
from mock import patch, Mock


THIS_DIR = Path(__file__).parent
Expand Down Expand Up @@ -36,17 +37,18 @@ def test_infer_path(clear_pages_state, module_name, template, pages_folder, expe


@pytest.mark.parametrize(
"pages_folder, expected_module_name",
"name, pages_folder, expected_module_name",
[
("custom_pages", "custom_pages.page"),
("sub_dir/custom_pages", "sub_dir.custom_pages.page"),
(str(THIS_DIR / "custom_pages"), "custom_pages.page"),
(__name__, "custom_pages", "custom_pages.page"),
(__name__, "sub_dir/custom_pages", "sub_dir.custom_pages.page"),
(__name__, str(THIS_DIR / "custom_pages"), "custom_pages.page"),
(__package__, "custom_pages", "pages.custom_pages.page"),
],
)
def test_import_layouts_from_pages(
clear_pages_state, pages_folder, expected_module_name
clear_pages_state, name, pages_folder, expected_module_name
):
_ = Dash(__name__, use_pages=True, pages_folder=pages_folder)
_ = Dash(name, use_pages=True, pages_folder=pages_folder)
assert len(dash.page_registry) == 1

page_entry = list(dash.page_registry.values())[0]
Expand Down