Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/process-flowsheet-studio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ on:
- "tests/test_process_chat_studio_context.py"
- "tests/test_studio_navigation.py"
- "tests/test_app_navigation.py"
- "tests/test_experimental_links.py"
- "tests/test_studio_results_context.py"
- "tests/test_studio_results_shell.py"
- "tests/test_studio_study_context.py"
Expand Down Expand Up @@ -121,6 +122,7 @@ jobs:
tests/test_subflowsheet_schema.py
- name: Run Studio workspace navigation regressions
run: |
python tests/test_experimental_links.py -v
python tests/test_app_navigation.py -v
python tests/test_studio_navigation.py -v
- name: Run shared Studio case lifecycle regressions
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ Studio card is also hidden from the front page.
Enable **Experimental mode** with the toggle directly below the sidebar menu to
register and show the wider set of developing models and interfaces, including
NeqSim Studio. The selection is retained in Streamlit session state while the
user moves between pages and defaults to off for each new session.
user moves between pages. New sessions start in normal mode unless opened through
an experimental page's direct URL, such as
[CO₂ Mechanistic Model](https://neqsim.streamlit.app/CO2_mechanisticModel).
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.

## Pipeline Hydraulics

Expand Down
10 changes: 10 additions & 0 deletions app_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import re
from dataclasses import dataclass
from pathlib import Path
from collections.abc import Iterable
from urllib.parse import unquote, urlsplit


APP_ROOT = Path(__file__).resolve().parent
Expand Down Expand Up @@ -90,3 +92,11 @@ def experimental_page_specs() -> tuple[PageSpec, ...]:
for path in discover_page_paths()
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
3 changes: 2 additions & 1 deletion home.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
### Getting Started
Use the left-hand menu to select a calculation. Normal mode contains TP Flash,
Phase Envelope, Gas Hydrate, Hydrogen, and EOS-CG. Enable **Experimental mode**
below the menu to add the wider set of developing tools.
below the menu to add the wider set of developing tools. Opening a direct link
to an experimental page automatically enables Experimental mode for that session.

### Documentation & Tutorials
- [NeqSim Documentation](https://equinor.github.io/neqsim/)
Expand Down
33 changes: 32 additions & 1 deletion tests/test_app_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

from pathlib import Path
import unittest
from unittest.mock import PropertyMock, patch

from streamlit.runtime.context import ContextProxy
from streamlit.navigation.page import StreamlitPage

from streamlit.testing.v1 import AppTest

Expand Down Expand Up @@ -89,7 +93,34 @@ def test_studio_front_page_action_tracks_experimental_mode(self):
self.assertFalse(app.exception)
self.assertNotIn("Open NeqSim Studio", [button.label for button in app.button])

def test_studio_url_is_unavailable_in_normal_mode(self):

def test_direct_link_registers_experimental_pages_and_toggle_can_disable(self):
app_path = Path(__file__).resolve().parents[1] / "welcome.py"
for spec in experimental_page_specs():
with self.subTest(page=spec.path):
# Page execution is isolated: this test exercises the real router
# without importing unrelated Java models or optional AI clients.
with patch.object(StreamlitPage, "run"), patch.object(
ContextProxy, "url", new_callable=PropertyMock,
return_value="https://neqsim.streamlit.app/"
+ spec.path.split("/", 1)[1].split("_", 1)[1][:-3].replace(" ", "_"),
):
app = AppTest.from_file(str(app_path)).run(timeout=30)
self.assertFalse(app.exception)
self.assertTrue(app.session_state["experimental_mode"])
toggle = next(
item for item in app.sidebar.toggle
if item.label == "Experimental mode"
)
self.assertTrue(toggle.value)
toggle.set_value(False)
app.run(timeout=30)
self.assertFalse(app.exception)
self.assertFalse(app.session_state["experimental_mode"])
app.run(timeout=30)
self.assertFalse(app.session_state["experimental_mode"])

def test_programmatic_studio_switch_is_unavailable_without_opt_in(self):
app_path = Path(__file__).resolve().parents[1] / "welcome.py"
app = AppTest.from_file(str(app_path)).run(timeout=30)

Expand Down
38 changes: 38 additions & 0 deletions tests/test_experimental_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Direct-link policy tests; no simulation or Streamlit runtime required."""

import unittest

from app_navigation import is_experimental_url


class ExperimentalLinkTest(unittest.TestCase):
def test_experimental_links(self):
for url in (
"https://neqsim.streamlit.app/CO2_mechanisticModel",
"https://neqsim.streamlit.app/CO2_mechanisticModel?embed=true#results",
"https://example.org/neqsim/CO2_mechanisticModel/",
"https://example.org/neqsim/%43O2_mechanisticModel",
):
with self.subTest(url=url):
self.assertTrue(is_experimental_url(url, ["CO2_mechanisticModel"]))

def test_home_stable_and_unknown_links_do_not_enable_experimental(self):
for url in (
None, "", "https://neqsim.streamlit.app/",
"https://neqsim.streamlit.app/TP_flash",
"https://neqsim.streamlit.app/unknown",
"https://neqsim.streamlit.app/?page=CO2_mechanisticModel",
"https://neqsim.streamlit.app/CO2_mechanisticModel_extra",
):
with self.subTest(url=url):
self.assertFalse(is_experimental_url(url, ["CO2_mechanisticModel"]))

def test_any_registered_experimental_route_is_supported(self):
self.assertTrue(is_experimental_url(
"https://neqsim.streamlit.app/NeqSim_Studio",
iter(["CO2_mechanisticModel", "NeqSim_Studio"]),
))


if __name__ == "__main__":
unittest.main()
11 changes: 7 additions & 4 deletions welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from app_navigation import (
experimental_page_specs,
is_experimental_url,
stable_page_specs,
)

Expand Down Expand Up @@ -66,8 +67,12 @@ def persist_experimental_mode():
)


# Build page metadata before navigation so direct links can opt a new session in.
experimental_pages = [create_page(spec) for spec in experimental_page_specs()]
if EXPERIMENTAL_MODE_KEY not in st.session_state:
st.session_state[EXPERIMENTAL_MODE_KEY] = False
st.session_state[EXPERIMENTAL_MODE_KEY] = is_experimental_url(
st.context.url, (page.url_path for page in experimental_pages)
)
if EXPERIMENTAL_MODE_WIDGET_KEY not in st.session_state:
st.session_state[EXPERIMENTAL_MODE_WIDGET_KEY] = st.session_state[
EXPERIMENTAL_MODE_KEY
Expand All @@ -79,9 +84,7 @@ def persist_experimental_mode():
"Stable tools": [create_page(spec) for spec in stable_page_specs()],
}
if experimental_mode:
navigation_pages["Experimental"] = [
create_page(spec) for spec in experimental_page_specs()
]
navigation_pages["Experimental"] = experimental_pages

selected_page = st.navigation(navigation_pages)

Expand Down
Loading