diff --git a/CHANGELOG.md b/CHANGELOG.md index 471f3d86a6..038f1bd9fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [UNRELEASED] ### Fixed +- [#4218](https://github.com/plotly/dash/pull/4218) Fix `pages_folder` written with `/` on Windows, such as `"src/pages"`, giving wrong page module names and paths. Fixes [#4217](https://github.com/plotly/dash/issues/4217). - [#4220](https://github.com/plotly/dash/pull/4220) Fix the `DASH_VALIDATE_CALLBACKS` environment variable being ignored, although `Dash.run` and `Dash.enable_dev_tools` document it for `dev_tools_validate_callbacks`. Fixes [#4219](https://github.com/plotly/dash/issues/4219). ## [4.5.2] - 2026-11-27 diff --git a/dash/_pages.py b/dash/_pages.py index 8beef7b183..a2012f0a0e 100644 --- a/dash/_pages.py +++ b/dash/_pages.py @@ -94,7 +94,11 @@ def _module_name_is_package(module_name): def _path_to_module_name(path): - return str(path).replace(".py", "").strip(os.sep).replace(os.sep, ".") + path = str(path) + if os.altsep: + # On Windows a folder given as "app/pages" mixes "/" with "\\" + path = path.replace(os.altsep, os.sep) + return path.replace(".py", "").strip(os.sep).replace(os.sep, ".") def _infer_module_name(page_path): diff --git a/tests/unit/pages/test_pages.py b/tests/unit/pages/test_pages.py index ab9ff07f09..ce9a898e23 100644 --- a/tests/unit/pages/test_pages.py +++ b/tests/unit/pages/test_pages.py @@ -1,3 +1,4 @@ +import os from pathlib import Path import pytest @@ -62,6 +63,21 @@ def test_path_to_module_name(path, expected): assert _pages._path_to_module_name(path) == expected +@pytest.mark.parametrize( + "path, expected", + [ + (r"\sub_dir/custom_pages", "sub_dir.custom_pages"), + (r"\src/pages\home.py", "src.pages.home"), + (r"\sub_dir\custom_pages", "sub_dir.custom_pages"), + ], +) +def test_path_to_module_name_windows_mixed_separators(monkeypatch, path, expected): + # a pages_folder written as "src/pages" on Windows mixes "/" and "\\" + monkeypatch.setattr(os, "sep", "\\") + monkeypatch.setattr(os, "altsep", "/") + assert _pages._path_to_module_name(path) == expected + + @pytest.mark.parametrize( "name, pages_folder, expected_module_name", [