Skip to content

Commit cb0f89f

Browse files
committed
Implement session-aware UI management for SCHISM output and calibration plots
1 parent 49c5dc6 commit cb0f89f

4 files changed

Lines changed: 98 additions & 21 deletions

File tree

schismviz/out2dui.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def _make_plot_action(self):
352352

353353
import click
354354
import yaml as _yaml
355+
from schismviz.session import serve_session_app
355356

356357

357358
def _load_yaml_section(config_file, section):
@@ -486,16 +487,15 @@ def show_out2d_ui(
486487
variables_arg = [v.strip() for v in str(variables_str).split(",") if v.strip()]
487488

488489
# ---- build manager and launch UI ---------------------------------------
489-
mgr = SchismOut2DUIManager(
490-
*files,
491-
nodes=nodes_arg,
492-
variables=variables_arg,
493-
study_name=cfg.get("title", "SCHISM out2d"),
494-
)
495-
496490
dashboard_title = cfg.get("title", "SCHISM out2d")
497491
server_port = int(cfg.get("port", 5006))
498492

499-
DataUI(mgr).create_view(title=dashboard_title).show(
500-
port=server_port, open=show
501-
)
493+
def build_manager():
494+
return SchismOut2DUIManager(
495+
*files,
496+
nodes=nodes_arg,
497+
variables=variables_arg,
498+
study_name=cfg.get("title", "SCHISM out2d"),
499+
)
500+
501+
serve_session_app(build_manager, title=dashboard_title, port=server_port, open=show)

schismviz/schismcalibplotui.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ def get_name_to_color(self):
546546

547547

548548
import click
549+
from schismviz.session import serve_session_app
549550

550551

551552
@click.command()
@@ -573,7 +574,8 @@ def get_name_to_color(self):
573574
default=False,
574575
help="Validate config and print resolved settings without launching the UI",
575576
)
576-
def schism_calib_plot_ui(config_file, base_dir=None, dry_run=False, **kwargs):
577+
@click.option("--port", default=5006, help="Port to serve the UI on")
578+
def schism_calib_plot_ui(config_file, base_dir=None, dry_run=False, port=5006, **kwargs):
577579
"""
578580
config_file: str
579581
yaml file containing configuration
@@ -599,6 +601,8 @@ def _serialize_for_yaml(value):
599601
click.echo("Configuration validated successfully.")
600602
click.echo(yaml.safe_dump(_serialize_for_yaml(manager.config), sort_keys=False))
601603
return
602-
DataUI(manager, crs=ccrs.UTM(10)).create_view(
603-
title="Schism Calibration Plot UI"
604-
).show()
604+
605+
def build_manager():
606+
return SchismCalibPlotUIManager(config_file, base_dir=base_dir, **kwargs)
607+
608+
serve_session_app(build_manager, title="Schism Calibration Plot UI", crs=ccrs.UTM(10), port=port)

schismviz/schismui.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def get_map_marker_columns(self):
325325

326326
import click
327327
import yaml
328+
from schismviz.session import serve_session_app
328329

329330

330331
@click.command()
@@ -348,6 +349,7 @@ def get_map_marker_columns(self):
348349
@click.option("--flux_out", default="flux.out", help="Path to the flux.out file")
349350
@click.option("--reftime", default=None, help="Reference time")
350351
@click.option("--yaml-file", default=None, help="Path to the yaml file")
352+
@click.option("--port", default=5006, help="Port to serve the UI on")
351353
def show_schism_output_ui(
352354
schism_dir=".",
353355
flux_xsect_file="flow_station_xsects.yaml",
@@ -357,6 +359,7 @@ def show_schism_output_ui(
357359
repo_dir="screened",
358360
inventory_file="inventory_datasets.csv",
359361
yaml_file=None,
362+
port=5006,
360363
):
361364
"""
362365
Shows Data UI for SCHISM output files.
@@ -442,13 +445,12 @@ def show_schism_output_ui(
442445
else:
443446
logger.info("No datastore inventory file found (%s); running without observation datastore.", inventory_file)
444447

445-
# Create the UI
446-
ui = DataUI(
447-
SchismOutputUIDataManager(
448+
# Launch the session-aware UI
449+
def build_manager():
450+
return SchismOutputUIDataManager(
448451
*studies,
449452
datastore=ds,
450453
time_range=time_range,
451-
),
452-
crs=ccrs.UTM(10),
453-
)
454-
ui.create_view(title="Schism Output UI").show()
454+
)
455+
456+
serve_session_app(build_manager, title="Schism Output UI", crs=ccrs.UTM(10), port=port)

schismviz/session.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""schismviz.session — Session persistence for schismviz CLI apps.
2+
3+
Thin re-export shim over :mod:`dvue.session_persistence`. All logic lives
4+
in dvue so that other dvue-based apps can reuse it.
5+
6+
The diskcache default for schismviz is ``~/.schismviz_sessions`` (rather
7+
than dvue's generic ``~/.dvue_sessions``) so sessions from different dvue
8+
apps on the same machine are kept separate. The cookie name is
9+
``"schismviz_user_id"`` to avoid collisions when multiple dvue apps are
10+
served on the same origin.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
from pathlib import Path
16+
17+
from dvue.session_persistence import (
18+
install_session_handler,
19+
snapshot,
20+
restore,
21+
)
22+
from dvue.session_persistence import serve_session_app as _serve_session_app
23+
24+
__all__ = ["install_session_handler", "snapshot", "restore", "serve_session_app"]
25+
26+
_DEFAULT_CACHE_DIR = Path.home() / ".schismviz_sessions"
27+
_DEFAULT_COOKIE_NAME = "schismviz_user_id"
28+
29+
30+
def serve_session_app(
31+
build_manager_fn,
32+
title: str,
33+
port: int = 0,
34+
crs=None,
35+
station_id_column: str | None = None,
36+
cache_dir: str | Path | None = None,
37+
**pn_serve_kwargs,
38+
) -> None:
39+
"""Launch a session-aware Panel app for a schismviz manager.
40+
41+
Delegates to :func:`dvue.session_persistence.serve_session_app` with
42+
``cache_dir`` defaulting to ``~/.schismviz_sessions`` and
43+
``cookie_name`` defaulting to ``"schismviz_user_id"``.
44+
45+
Parameters
46+
----------
47+
build_manager_fn:
48+
Zero-argument callable returning a fresh ``DataUIManager`` instance.
49+
title:
50+
Browser title; also used as the URL path key.
51+
port:
52+
TCP port (``0`` = random available port).
53+
crs:
54+
Cartopy CRS for the map panel. ``None`` → no map.
55+
station_id_column:
56+
Column identifying stations in the catalog.
57+
cache_dir:
58+
Diskcache directory. Defaults to ``~/.schismviz_sessions``.
59+
**pn_serve_kwargs:
60+
Forwarded to ``pn.serve()``.
61+
"""
62+
_serve_session_app(
63+
build_manager_fn,
64+
title=title,
65+
port=port,
66+
crs=crs,
67+
station_id_column=station_id_column,
68+
cookie_name=_DEFAULT_COOKIE_NAME,
69+
cache_dir=cache_dir if cache_dir is not None else _DEFAULT_CACHE_DIR,
70+
**pn_serve_kwargs,
71+
)

0 commit comments

Comments
 (0)