Skip to content

Commit 2ab35ea

Browse files
committed
refactor _infer_module_name
1 parent 7b90fa7 commit 2ab35ea

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Diff for: dash/_pages.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ def _module_name_is_package(module_name):
9090
)
9191

9292

93+
def _path_to_module(path):
94+
return str(path).replace(".py", "").strip(os.sep).replace(os.sep, ".")
95+
96+
9397
def _infer_module_name(page_path):
94-
page_path = page_path.replace("\\", "/")
95-
_, _, filename = page_path.partition(CONFIG.pages_folder.replace("\\", "/") + "/")
96-
module = filename.replace(".py", "").replace("/", ".")
98+
relative_path = page_path.split(CONFIG.pages_folder)[-1]
99+
module = _path_to_module(relative_path)
97100
proj_root = flask.helpers.get_root_path(CONFIG.name)
98-
parent_module = CONFIG.pages_folder
99101
if CONFIG.pages_folder.startswith(proj_root):
100-
parent_module = parent_module[len(proj_root) :]
101-
parent_module = parent_module.lstrip("/").replace("/", ".")
102+
parent_path = CONFIG.pages_folder[len(proj_root) :]
103+
else:
104+
parent_path = CONFIG.pages_folder
105+
parent_module = _path_to_module(parent_path)
106+
102107
module_name = f"{parent_module}.{module}"
103108
if _module_name_is_package(CONFIG.name):
104109
# Only prefix with CONFIG.name when its an imported package name

Diff for: tests/unit/pages/test_pages.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import sys
21
from pathlib import Path
32

43
import pytest
54
import dash
65
from dash import Dash, _pages
7-
from mock import patch, Mock
6+
from mock import patch
87

98

109
THIS_DIR = Path(__file__).parent

0 commit comments

Comments
 (0)