diff --git a/.github/workflows/process-flowsheet-studio.yml b/.github/workflows/process-flowsheet-studio.yml index d240412..303529f 100644 --- a/.github/workflows/process-flowsheet-studio.yml +++ b/.github/workflows/process-flowsheet-studio.yml @@ -4,6 +4,8 @@ on: pull_request: paths: - "app_navigation.py" + - "experimental_links.py" + - "tests/test_navigation_warm_import.py" - "home.py" - "welcome.py" - "requirements.txt" @@ -122,6 +124,7 @@ jobs: tests/test_subflowsheet_schema.py - name: Run Studio workspace navigation regressions run: | + python tests/test_navigation_warm_import.py -v python tests/test_experimental_links.py -v python tests/test_app_navigation.py -v python tests/test_studio_navigation.py -v diff --git a/README.md b/README.md index f8a2e0b..3130a53 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,12 @@ These links automatically enable Experimental mode before routing to the page; no query parameter or prior visit to the home page is needed. The toggle can still be turned off afterward. +If a running Streamlit Cloud instance reports an import error immediately after +an update, **Manage app → Reboot app** restarts its Python process and clears +modules retained from the previous version. Refreshing the browser alone does +not restart that process. Direct-link routing is isolated from the older +navigation module to remain compatible during this update. + ## Pipeline Hydraulics The **Pipeline** page provides two native NeqSim calculation paths: diff --git a/app_navigation.py b/app_navigation.py index 74dde8e..f97bf19 100644 --- a/app_navigation.py +++ b/app_navigation.py @@ -5,8 +5,7 @@ import re from dataclasses import dataclass from pathlib import Path -from collections.abc import Iterable -from urllib.parse import unquote, urlsplit +from experimental_links import is_experimental_url APP_ROOT = Path(__file__).resolve().parent @@ -93,10 +92,3 @@ def experimental_page_specs() -> tuple[PageSpec, ...]: if path not in STABLE_PAGE_PATHS ) - -def is_experimental_url(url: str | None, experimental_paths: Iterable[str]) -> bool: - """Recognize a registered experimental URL, including a deployment base path.""" - if not url: - return False - pathname = unquote(urlsplit(url).path).rstrip("/").rsplit("/", 1)[-1] - return bool(pathname) and pathname in experimental_paths diff --git a/experimental_links.py b/experimental_links.py new file mode 100644 index 0000000..426a9ce --- /dev/null +++ b/experimental_links.py @@ -0,0 +1,12 @@ +"""URL policy isolated from the navigation module cached by older deployments.""" + +from collections.abc import Iterable +from urllib.parse import unquote, urlsplit + + +def is_experimental_url(url: str | None, experimental_paths: Iterable[str]) -> bool: + """Recognize a registered experimental URL, including a deployment base path.""" + if not url: + return False + pathname = unquote(urlsplit(url).path).rstrip("/").rsplit("/", 1)[-1] + return bool(pathname) and pathname in experimental_paths diff --git a/tests/test_experimental_links.py b/tests/test_experimental_links.py index 3833b23..987f742 100644 --- a/tests/test_experimental_links.py +++ b/tests/test_experimental_links.py @@ -2,7 +2,7 @@ import unittest -from app_navigation import is_experimental_url +from experimental_links import is_experimental_url class ExperimentalLinkTest(unittest.TestCase): diff --git a/tests/test_navigation_warm_import.py b/tests/test_navigation_warm_import.py new file mode 100644 index 0000000..0421f02 --- /dev/null +++ b/tests/test_navigation_warm_import.py @@ -0,0 +1,88 @@ +"""Router regressions for a deployment with the previous navigation module cached.""" + +import os +from pathlib import Path +import runpy +import sys +from types import ModuleType, SimpleNamespace +import unittest +from unittest.mock import patch + + +ROOT = Path(__file__).resolve().parents[1] + + +class Sidebar: + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + +class WarmNavigationImportTest(unittest.TestCase): + def run_router(self, url, initial_state=None): + # Reproduce the pre-PR-136 public API still held in sys.modules. + old_navigation = ModuleType("app_navigation") + old_navigation.experimental_page_specs = lambda: [ + SimpleNamespace(path="pages/100_CO2_mechanisticModel.py", title="CO2") + ] + old_navigation.stable_page_specs = lambda: [ + SimpleNamespace(path="pages/0_TP_flash.py", title="TP Flash") + ] + streamlit = ModuleType("streamlit") + streamlit.session_state = dict(initial_state or {}) + streamlit.context = SimpleNamespace(url=url) + streamlit.sidebar = Sidebar() + streamlit.divider = lambda: None + streamlit.caption = lambda *args: None + streamlit.toggle = lambda *args, **kwargs: None + executed = [] + + def page(path, **kwargs): + route = "" if kwargs.get("default") else ( + Path(path).stem.split("_", 1)[1] + ) + return SimpleNamespace( + url_path=route, run=lambda: executed.append(path) + ) + + def navigation(groups): + requested = url.rsplit("/", 1)[-1] + pages = [item for group in groups.values() for item in group] + return next( + (item for item in pages if item.url_path == requested), pages[0] + ) + + streamlit.Page = page + streamlit.navigation = navigation + with patch.dict(sys.modules, { + "app_navigation": old_navigation, "streamlit": streamlit, + }), patch.dict(os.environ): + runpy.run_path(str(ROOT / "welcome.py"), run_name="__main__") + return streamlit.session_state, executed + + def test_direct_co2_link_with_previous_module_cached(self): + state, executed = self.run_router( + "https://neqsim.streamlit.app/CO2_mechanisticModel" + ) + self.assertTrue(state["experimental_mode"]) + self.assertTrue(state["_experimental_mode_toggle"]) + self.assertEqual(executed, ["pages/100_CO2_mechanisticModel.py"]) + + def test_home_with_previous_module_cached(self): + state, executed = self.run_router("https://neqsim.streamlit.app/") + self.assertFalse(state["experimental_mode"]) + self.assertEqual(executed, ["home.py"]) + + def test_explicit_off_survives_rerun_with_previous_module_cached(self): + state, executed = self.run_router( + "https://neqsim.streamlit.app/CO2_mechanisticModel", + {"experimental_mode": False, "_experimental_mode_toggle": False}, + ) + self.assertFalse(state["experimental_mode"]) + self.assertEqual(executed, ["home.py"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/welcome.py b/welcome.py index 9a8aa9a..2cc3100 100644 --- a/welcome.py +++ b/welcome.py @@ -16,10 +16,13 @@ from app_navigation import ( experimental_page_specs, - is_experimental_url, stable_page_specs, ) +# Older live processes may still hold the pre-direct-link app_navigation module. +# Import the new policy from its own module instead of that cached module. +from experimental_links import is_experimental_url + EXPERIMENTAL_MODE_KEY = "experimental_mode" EXPERIMENTAL_MODE_WIDGET_KEY = "_experimental_mode_toggle"