diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6f1197d7..6d3e1da244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ 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 `pages_folder="src/pages"`: page modules were named `src/pages.home` and their inferred paths kept the folder (`/src/pages/home` instead of `/home`). Fixes [#4217](https://github.com/plotly/dash/issues/4217). + ## [4.5.2] - 2026-11-27 ### Changed 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", [