Skip to content

Commit

Permalink
refactor _infer_module_name
Browse files Browse the repository at this point in the history
  • Loading branch information
ned2 committed Feb 6, 2023
1 parent 78194ae commit 127eaad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 11 additions & 6 deletions dash/_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ def _module_name_is_package(module_name):
)


def _path_to_module(path):
return str(path).replace(".py", "").strip(os.sep).replace(os.sep, ".")


def _infer_module_name(page_path):
page_path = page_path.replace("\\", "/")
_, _, filename = page_path.partition(CONFIG.pages_folder.replace("\\", "/") + "/")
module = filename.replace(".py", "").replace("/", ".")
relative_path = page_path.split(CONFIG.pages_folder)[-1]
module = _path_to_module(relative_path)
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("/", ".")
parent_path = CONFIG.pages_folder[len(proj_root) :]
else:
parent_path = CONFIG.pages_folder
parent_module = _path_to_module(parent_path)

module_name = f"{parent_module}.{module}"
if _module_name_is_package(CONFIG.name):
# Only prefix with CONFIG.name when its an imported package name
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/pages/test_pages.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import sys
from pathlib import Path

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


THIS_DIR = Path(__file__).parent
Expand Down

0 comments on commit 127eaad

Please sign in to comment.