scidatasim-ui 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scidatasim_ui-0.7.0/PKG-INFO +69 -0
- scidatasim_ui-0.7.0/README.md +46 -0
- scidatasim_ui-0.7.0/pyproject.toml +41 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/PKG-INFO +69 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/SOURCES.txt +14 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/dependency_links.txt +1 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/entry_points.txt +2 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/requires.txt +6 -0
- scidatasim_ui-0.7.0/scidatasim_ui.egg-info/top_level.txt +1 -0
- scidatasim_ui-0.7.0/sds_ui/__init__.py +62 -0
- scidatasim_ui-0.7.0/sds_ui/__main__.py +5 -0
- scidatasim_ui-0.7.0/sds_ui/app.py +278 -0
- scidatasim_ui-0.7.0/sds_ui/cli.py +18 -0
- scidatasim_ui-0.7.0/sds_ui/core.py +269 -0
- scidatasim_ui-0.7.0/setup.cfg +4 -0
- scidatasim_ui-0.7.0/tests/test_ui.py +197 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scidatasim-ui
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Streamlit UI for Scientific Data Simulator: examples, runs, results, installation check
|
|
5
|
+
Author: Daniel Sandner
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/sandner-art/scientific-data-simulator
|
|
8
|
+
Project-URL: Source, https://github.com/sandner-art/scientific-data-simulator/tree/main/ui
|
|
9
|
+
Project-URL: Issues, https://github.com/sandner-art/scientific-data-simulator/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
Requires-Dist: scidatasim>=0.7.0
|
|
19
|
+
Requires-Dist: streamlit>=1.67
|
|
20
|
+
Requires-Dist: pillow>=13.0
|
|
21
|
+
Provides-Extra: test
|
|
22
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
23
|
+
|
|
24
|
+
# Scientific Data Simulator UI
|
|
25
|
+
|
|
26
|
+
A browser UI for [Scientific Data Simulator](../README.md), for convenience and an overview of
|
|
27
|
+
what the simulator can do. It is a separate package that uses only the simulator's public
|
|
28
|
+
functions. Everything it does is also available from the command line, and every run it starts
|
|
29
|
+
is recorded exactly like `sds examples run`, including the command that repeats it.
|
|
30
|
+
|
|
31
|
+
## Install and start
|
|
32
|
+
|
|
33
|
+
From the repository folder:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bash scripts/install.sh ui # Linux, macOS (Windows: scripts\install.cmd ui)
|
|
37
|
+
sds ui # or: bash scripts/start-ui.sh (Windows: scripts\start-ui.cmd)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Or into an existing environment: `pip install -e ./ui`, then `sds ui`. Your browser opens at
|
|
41
|
+
http://127.0.0.1:8501. Options: `sds ui --port 8600 --runs-dir my_runs`.
|
|
42
|
+
|
|
43
|
+
By default the UI listens on this computer only. Anyone who can open it can run the examples
|
|
44
|
+
with any parameters, fill the disk with results, and (in example 12) make Python import a
|
|
45
|
+
module by name. `--address 0.0.0.0` exposes it to the network: do that only on a network you
|
|
46
|
+
trust. The Results page shows only the runs folder given at start.
|
|
47
|
+
|
|
48
|
+
## Pages
|
|
49
|
+
|
|
50
|
+
- **Examples**: every example, from its `example.yaml`, searchable by text, field and level, with
|
|
51
|
+
the optional packages each one needs.
|
|
52
|
+
- **Run**: a form built from the example's parameters (types, ranges and choices come from the
|
|
53
|
+
manifest), a quick mode, a time limit, a live log, then the outputs (figures, tables, reports,
|
|
54
|
+
JSON) and the command that repeats the run.
|
|
55
|
+
- **Results**: past runs in the runs folder (default `sds_runs` in the folder where the UI was
|
|
56
|
+
started). Click a row to open a run: its figures, parameters, the command that repeats it, the
|
|
57
|
+
log and the other outputs. If an example's manifest lists no figures, the figures found in the
|
|
58
|
+
run folder are shown.
|
|
59
|
+
Interactive plots load only when you ask, since each is several megabytes.
|
|
60
|
+
- **Installation**: the `sds doctor` check, with the fix for anything missing.
|
|
61
|
+
|
|
62
|
+
New examples appear automatically once they have a manifest; the UI needs no change.
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
The UI is tested headless with Streamlit's AppTest (`pytest ui/tests`). The tests open every page
|
|
67
|
+
and run a real example through the form. CI runs them on Linux, Windows and macOS. Its version
|
|
68
|
+
always equals the simulator's, and the simulator never imports it (both checked by
|
|
69
|
+
`tests/test_ui_boundary.py`). It needs Python 3.11 or newer and Streamlit 1.67 or newer (the first that accepts Pillow 13).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Scientific Data Simulator UI
|
|
2
|
+
|
|
3
|
+
A browser UI for [Scientific Data Simulator](../README.md), for convenience and an overview of
|
|
4
|
+
what the simulator can do. It is a separate package that uses only the simulator's public
|
|
5
|
+
functions. Everything it does is also available from the command line, and every run it starts
|
|
6
|
+
is recorded exactly like `sds examples run`, including the command that repeats it.
|
|
7
|
+
|
|
8
|
+
## Install and start
|
|
9
|
+
|
|
10
|
+
From the repository folder:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bash scripts/install.sh ui # Linux, macOS (Windows: scripts\install.cmd ui)
|
|
14
|
+
sds ui # or: bash scripts/start-ui.sh (Windows: scripts\start-ui.cmd)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or into an existing environment: `pip install -e ./ui`, then `sds ui`. Your browser opens at
|
|
18
|
+
http://127.0.0.1:8501. Options: `sds ui --port 8600 --runs-dir my_runs`.
|
|
19
|
+
|
|
20
|
+
By default the UI listens on this computer only. Anyone who can open it can run the examples
|
|
21
|
+
with any parameters, fill the disk with results, and (in example 12) make Python import a
|
|
22
|
+
module by name. `--address 0.0.0.0` exposes it to the network: do that only on a network you
|
|
23
|
+
trust. The Results page shows only the runs folder given at start.
|
|
24
|
+
|
|
25
|
+
## Pages
|
|
26
|
+
|
|
27
|
+
- **Examples**: every example, from its `example.yaml`, searchable by text, field and level, with
|
|
28
|
+
the optional packages each one needs.
|
|
29
|
+
- **Run**: a form built from the example's parameters (types, ranges and choices come from the
|
|
30
|
+
manifest), a quick mode, a time limit, a live log, then the outputs (figures, tables, reports,
|
|
31
|
+
JSON) and the command that repeats the run.
|
|
32
|
+
- **Results**: past runs in the runs folder (default `sds_runs` in the folder where the UI was
|
|
33
|
+
started). Click a row to open a run: its figures, parameters, the command that repeats it, the
|
|
34
|
+
log and the other outputs. If an example's manifest lists no figures, the figures found in the
|
|
35
|
+
run folder are shown.
|
|
36
|
+
Interactive plots load only when you ask, since each is several megabytes.
|
|
37
|
+
- **Installation**: the `sds doctor` check, with the fix for anything missing.
|
|
38
|
+
|
|
39
|
+
New examples appear automatically once they have a manifest; the UI needs no change.
|
|
40
|
+
|
|
41
|
+
## Development
|
|
42
|
+
|
|
43
|
+
The UI is tested headless with Streamlit's AppTest (`pytest ui/tests`). The tests open every page
|
|
44
|
+
and run a real example through the form. CI runs them on Linux, Windows and macOS. Its version
|
|
45
|
+
always equals the simulator's, and the simulator never imports it (both checked by
|
|
46
|
+
`tests/test_ui_boundary.py`). It needs Python 3.11 or newer and Streamlit 1.67 or newer (the first that accepts Pillow 13).
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "scidatasim-ui"
|
|
7
|
+
version = "0.7.0" # released together with the simulator, same version
|
|
8
|
+
description = "Streamlit UI for Scientific Data Simulator: examples, runs, results, installation check"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
authors = [{name = "Daniel Sandner"}]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Science/Research",
|
|
16
|
+
"Intended Audience :: Education",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Scientific/Engineering",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"scidatasim>=0.7.0",
|
|
23
|
+
# 1.67 is the first Streamlit that accepts Pillow 13 (earlier releases cap it below 13);
|
|
24
|
+
# the floor also makes pip upgrade an older Streamlit that is already installed.
|
|
25
|
+
"streamlit>=1.67",
|
|
26
|
+
"pillow>=13.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
test = ["pytest>=7"]
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
sds-ui = "sds_ui.cli:main"
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/sandner-art/scientific-data-simulator"
|
|
37
|
+
Source = "https://github.com/sandner-art/scientific-data-simulator/tree/main/ui"
|
|
38
|
+
Issues = "https://github.com/sandner-art/scientific-data-simulator/issues"
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.packages.find]
|
|
41
|
+
include = ["sds_ui*"]
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scidatasim-ui
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Streamlit UI for Scientific Data Simulator: examples, runs, results, installation check
|
|
5
|
+
Author: Daniel Sandner
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/sandner-art/scientific-data-simulator
|
|
8
|
+
Project-URL: Source, https://github.com/sandner-art/scientific-data-simulator/tree/main/ui
|
|
9
|
+
Project-URL: Issues, https://github.com/sandner-art/scientific-data-simulator/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
Requires-Dist: scidatasim>=0.7.0
|
|
19
|
+
Requires-Dist: streamlit>=1.67
|
|
20
|
+
Requires-Dist: pillow>=13.0
|
|
21
|
+
Provides-Extra: test
|
|
22
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
23
|
+
|
|
24
|
+
# Scientific Data Simulator UI
|
|
25
|
+
|
|
26
|
+
A browser UI for [Scientific Data Simulator](../README.md), for convenience and an overview of
|
|
27
|
+
what the simulator can do. It is a separate package that uses only the simulator's public
|
|
28
|
+
functions. Everything it does is also available from the command line, and every run it starts
|
|
29
|
+
is recorded exactly like `sds examples run`, including the command that repeats it.
|
|
30
|
+
|
|
31
|
+
## Install and start
|
|
32
|
+
|
|
33
|
+
From the repository folder:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bash scripts/install.sh ui # Linux, macOS (Windows: scripts\install.cmd ui)
|
|
37
|
+
sds ui # or: bash scripts/start-ui.sh (Windows: scripts\start-ui.cmd)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Or into an existing environment: `pip install -e ./ui`, then `sds ui`. Your browser opens at
|
|
41
|
+
http://127.0.0.1:8501. Options: `sds ui --port 8600 --runs-dir my_runs`.
|
|
42
|
+
|
|
43
|
+
By default the UI listens on this computer only. Anyone who can open it can run the examples
|
|
44
|
+
with any parameters, fill the disk with results, and (in example 12) make Python import a
|
|
45
|
+
module by name. `--address 0.0.0.0` exposes it to the network: do that only on a network you
|
|
46
|
+
trust. The Results page shows only the runs folder given at start.
|
|
47
|
+
|
|
48
|
+
## Pages
|
|
49
|
+
|
|
50
|
+
- **Examples**: every example, from its `example.yaml`, searchable by text, field and level, with
|
|
51
|
+
the optional packages each one needs.
|
|
52
|
+
- **Run**: a form built from the example's parameters (types, ranges and choices come from the
|
|
53
|
+
manifest), a quick mode, a time limit, a live log, then the outputs (figures, tables, reports,
|
|
54
|
+
JSON) and the command that repeats the run.
|
|
55
|
+
- **Results**: past runs in the runs folder (default `sds_runs` in the folder where the UI was
|
|
56
|
+
started). Click a row to open a run: its figures, parameters, the command that repeats it, the
|
|
57
|
+
log and the other outputs. If an example's manifest lists no figures, the figures found in the
|
|
58
|
+
run folder are shown.
|
|
59
|
+
Interactive plots load only when you ask, since each is several megabytes.
|
|
60
|
+
- **Installation**: the `sds doctor` check, with the fix for anything missing.
|
|
61
|
+
|
|
62
|
+
New examples appear automatically once they have a manifest; the UI needs no change.
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
The UI is tested headless with Streamlit's AppTest (`pytest ui/tests`). The tests open every page
|
|
67
|
+
and run a real example through the form. CI runs them on Linux, Windows and macOS. Its version
|
|
68
|
+
always equals the simulator's, and the simulator never imports it (both checked by
|
|
69
|
+
`tests/test_ui_boundary.py`). It needs Python 3.11 or newer and Streamlit 1.67 or newer (the first that accepts Pillow 13).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
scidatasim_ui.egg-info/PKG-INFO
|
|
4
|
+
scidatasim_ui.egg-info/SOURCES.txt
|
|
5
|
+
scidatasim_ui.egg-info/dependency_links.txt
|
|
6
|
+
scidatasim_ui.egg-info/entry_points.txt
|
|
7
|
+
scidatasim_ui.egg-info/requires.txt
|
|
8
|
+
scidatasim_ui.egg-info/top_level.txt
|
|
9
|
+
sds_ui/__init__.py
|
|
10
|
+
sds_ui/__main__.py
|
|
11
|
+
sds_ui/app.py
|
|
12
|
+
sds_ui/cli.py
|
|
13
|
+
sds_ui/core.py
|
|
14
|
+
tests/test_ui.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sds_ui
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# sds_ui/__init__.py
|
|
2
|
+
"""Streamlit UI for Scientific Data Simulator: examples, runs, results, installation check.
|
|
3
|
+
|
|
4
|
+
A separate package that depends on the simulator's public functions; the simulator never imports
|
|
5
|
+
it. Start it with ``sds ui`` or ``python -m sds_ui``.
|
|
6
|
+
"""
|
|
7
|
+
import os
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
from typing import List, Optional
|
|
11
|
+
|
|
12
|
+
__version__ = "0.7.0"
|
|
13
|
+
|
|
14
|
+
AUTHOR = "Daniel Sandner"
|
|
15
|
+
LICENSE = "Apache-2.0"
|
|
16
|
+
REPOSITORY = "https://github.com/sandner-art/scientific-data-simulator"
|
|
17
|
+
|
|
18
|
+
APP = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app.py")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def launch_command(port: int = 8501, address: str = "127.0.0.1") -> List[str]:
|
|
22
|
+
"""The streamlit command. It listens on this computer only (127.0.0.1) unless you choose
|
|
23
|
+
otherwise: the UI runs example code with the parameters you enter."""
|
|
24
|
+
return [sys.executable, "-m", "streamlit", "run", APP, "--server.address", address,
|
|
25
|
+
"--server.port", str(port), "--browser.gatherUsageStats", "false",
|
|
26
|
+
"--client.toolbarMode", "minimal"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _enable_windows_colours() -> None:
|
|
30
|
+
"""Let the Windows console show Streamlit's coloured start-up text instead of raw escape codes
|
|
31
|
+
such as ←[0m (switch on 'virtual terminal processing' for the console this process shares)."""
|
|
32
|
+
if os.name != "nt":
|
|
33
|
+
return
|
|
34
|
+
try:
|
|
35
|
+
import ctypes
|
|
36
|
+
kernel32 = ctypes.windll.kernel32
|
|
37
|
+
for handle_id in (-11, -12): # stdout, stderr
|
|
38
|
+
handle = kernel32.GetStdHandle(handle_id)
|
|
39
|
+
mode = ctypes.c_uint32()
|
|
40
|
+
if kernel32.GetConsoleMode(handle, ctypes.byref(mode)):
|
|
41
|
+
kernel32.SetConsoleMode(handle, mode.value | 0x0004) # ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
42
|
+
except Exception: # not a console (e.g. redirected output): nothing to do
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def launch(port: int = 8501, address: str = "127.0.0.1", runs_dir: Optional[str] = None,
|
|
47
|
+
examples_dir: Optional[str] = None) -> int:
|
|
48
|
+
env = dict(os.environ)
|
|
49
|
+
if runs_dir:
|
|
50
|
+
env["SDS_RUNS"] = os.path.abspath(runs_dir)
|
|
51
|
+
if examples_dir:
|
|
52
|
+
env["SDS_EXAMPLES"] = os.path.abspath(examples_dir)
|
|
53
|
+
if address not in ("127.0.0.1", "localhost", "::1"):
|
|
54
|
+
print(f"WARNING: listening on {address}. Anyone who can reach this port can run the examples, "
|
|
55
|
+
"fill the disk with results, and (example 12) import Python modules by name. Use only on "
|
|
56
|
+
"a network you trust.")
|
|
57
|
+
_enable_windows_colours()
|
|
58
|
+
print(f"Starting the UI on http://{address}:{port} (Ctrl+C stops it) ...")
|
|
59
|
+
try:
|
|
60
|
+
return subprocess.call(launch_command(port, address), env=env)
|
|
61
|
+
except KeyboardInterrupt:
|
|
62
|
+
return 0
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# sds_ui/app.py
|
|
2
|
+
"""Streamlit app: examples gallery, run form with live log, results browser, installation check.
|
|
3
|
+
|
|
4
|
+
Start it with ``sds ui`` (or ``python -m sds_ui``). Every page reads the example manifests, so new
|
|
5
|
+
examples appear without changes here.
|
|
6
|
+
"""
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
import pandas as pd
|
|
12
|
+
import streamlit as st
|
|
13
|
+
|
|
14
|
+
from sds_ui import AUTHOR, LICENSE, REPOSITORY
|
|
15
|
+
from sds_ui import __version__ as UI_VERSION
|
|
16
|
+
from sds_ui import core
|
|
17
|
+
|
|
18
|
+
PAGES = ("Examples", "Run", "Results", "Installation")
|
|
19
|
+
STATUS_ICON = {"ok": "✅", "info": "ℹ️", "warn": "⚠️", "fail": "❌"}
|
|
20
|
+
MAX_IMAGES = 6
|
|
21
|
+
MAX_HTML_BYTES = 20 * 1024 * 1024
|
|
22
|
+
|
|
23
|
+
st.set_page_config(page_title="Scientific Data Simulator", layout="wide")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@st.cache_data(ttl=30, show_spinner=False)
|
|
27
|
+
def _examples_cached(folder):
|
|
28
|
+
return core.load_examples(folder)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def examples():
|
|
32
|
+
try:
|
|
33
|
+
return _examples_cached(os.environ.get("SDS_EXAMPLES") or None)
|
|
34
|
+
except FileNotFoundError as e:
|
|
35
|
+
st.error(f"{e}")
|
|
36
|
+
st.stop()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def go(page, **state):
|
|
40
|
+
"""Button callback: switch page (callbacks run before the widgets are drawn again)."""
|
|
41
|
+
st.session_state.update(state)
|
|
42
|
+
if "example" in state:
|
|
43
|
+
st.session_state["run_example_select"] = state["example"]
|
|
44
|
+
st.session_state["page"] = page
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ---------------------------------------------------------------------------- output rendering
|
|
48
|
+
def show_outputs(outputs, key, folder=None):
|
|
49
|
+
images = [o for o in outputs if o["kind"] == "image"]
|
|
50
|
+
if not images and folder: # the manifest lists no figures: show the ones the run made
|
|
51
|
+
images = core.figures_in(folder)
|
|
52
|
+
if not outputs and not images:
|
|
53
|
+
st.caption("No outputs listed by the example's manifest.")
|
|
54
|
+
return
|
|
55
|
+
others = [o for o in outputs if o["kind"] != "image"]
|
|
56
|
+
if images:
|
|
57
|
+
cols = st.columns(min(3, len(images)))
|
|
58
|
+
for i, o in enumerate(images[:MAX_IMAGES]):
|
|
59
|
+
with cols[i % len(cols)]:
|
|
60
|
+
try:
|
|
61
|
+
st.image(o["path"], caption=os.path.basename(o["path"]))
|
|
62
|
+
except Exception:
|
|
63
|
+
st.caption(f"(cannot display) {o['path']}")
|
|
64
|
+
if len(images) > MAX_IMAGES:
|
|
65
|
+
st.caption(f"{len(images) - MAX_IMAGES} more image(s) in the output folder.")
|
|
66
|
+
for i, o in enumerate(others[:10]):
|
|
67
|
+
with st.expander(f"{o['kind']}: {os.path.basename(o['path'])}", expanded=(o["kind"] == "markdown" and i == 0)):
|
|
68
|
+
st.caption(o["path"])
|
|
69
|
+
try:
|
|
70
|
+
if o["kind"] == "markdown":
|
|
71
|
+
with open(o["path"], encoding="utf-8") as f:
|
|
72
|
+
st.markdown(f.read())
|
|
73
|
+
elif o["kind"] == "table":
|
|
74
|
+
st.dataframe(pd.read_csv(o["path"], nrows=500, encoding_errors="replace"))
|
|
75
|
+
elif o["kind"] == "json":
|
|
76
|
+
with open(o["path"], encoding="utf-8") as f:
|
|
77
|
+
st.json(json.load(f), expanded=False)
|
|
78
|
+
elif o["kind"] == "html":
|
|
79
|
+
# interactive plots are megabytes each: load one only when asked
|
|
80
|
+
size = os.path.getsize(o["path"])
|
|
81
|
+
if size > MAX_HTML_BYTES:
|
|
82
|
+
st.caption(f"{size / 1e6:.0f} MB: open it from the folder in a browser.")
|
|
83
|
+
elif st.toggle("Show interactive plot", key=f"html_{key}_{i}"):
|
|
84
|
+
# a local file the simulator wrote (st.iframe reads HTML files directly)
|
|
85
|
+
from pathlib import Path
|
|
86
|
+
st.iframe(Path(o["path"]), height=480)
|
|
87
|
+
elif o["kind"] == "folder":
|
|
88
|
+
st.write(sorted(os.listdir(o["path"]))[:50])
|
|
89
|
+
else:
|
|
90
|
+
st.code(core.tail(o["path"], 100), language=None)
|
|
91
|
+
except Exception as e: # one unreadable file must not break the page
|
|
92
|
+
st.warning(f"Could not show this file: {type(e).__name__}: {e}")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# ---------------------------------------------------------------------------- pages
|
|
96
|
+
def page_examples():
|
|
97
|
+
st.header("Examples")
|
|
98
|
+
found = examples()
|
|
99
|
+
all_fields = sorted({f for e in found.values() for f in e.fields})
|
|
100
|
+
c1, c2, c3 = st.columns([2, 2, 1])
|
|
101
|
+
text = c1.text_input("Search", key="search")
|
|
102
|
+
fields = c2.multiselect("Fields", all_fields, key="fields_filter")
|
|
103
|
+
level = c3.selectbox("Level", ["any", "student", "research"], key="level_filter")
|
|
104
|
+
shown = core.filter_examples(found, text, fields, level)
|
|
105
|
+
st.caption(f"{len(shown)} of {len(found)} examples")
|
|
106
|
+
for e in shown:
|
|
107
|
+
with st.container():
|
|
108
|
+
left, right = st.columns([5, 1])
|
|
109
|
+
left.subheader(e.title)
|
|
110
|
+
runtime = e.runtime.get("full") or ""
|
|
111
|
+
left.caption(f"{e.name} · {e.level} · {', '.join(e.fields)}" + (f" · about {runtime}" if runtime else ""))
|
|
112
|
+
left.write(e.summary)
|
|
113
|
+
missing = core.missing_extras(e)
|
|
114
|
+
if missing:
|
|
115
|
+
left.warning("Needs: " + ", ".join(missing) + f'. Install with: pip install -e ".[{",".join(missing)}]"')
|
|
116
|
+
right.button("Open", key=f"open_{e.name}", on_click=go, args=("Run",), kwargs={"example": e.name})
|
|
117
|
+
st.divider()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def page_run():
|
|
121
|
+
st.header("Run an example")
|
|
122
|
+
found = examples()
|
|
123
|
+
names = list(found)
|
|
124
|
+
if st.session_state.get("run_example_select") not in found: # first visit, or a removed example
|
|
125
|
+
st.session_state["run_example_select"] = names[0]
|
|
126
|
+
name = st.selectbox("Example", names, format_func=lambda n: f"{found[n].title} ({n})",
|
|
127
|
+
key="run_example_select")
|
|
128
|
+
example = found[name]
|
|
129
|
+
st.write(example.summary)
|
|
130
|
+
missing = core.missing_extras(example)
|
|
131
|
+
if missing:
|
|
132
|
+
st.warning("This example needs: " + ", ".join(missing) + ". It may fail without them.")
|
|
133
|
+
|
|
134
|
+
live = st.session_state.get("live")
|
|
135
|
+
running = live is not None and not live.done
|
|
136
|
+
|
|
137
|
+
with st.form(key=f"form_{name}"):
|
|
138
|
+
raw = {}
|
|
139
|
+
for p in example.params:
|
|
140
|
+
spec = core.widget_spec(p)
|
|
141
|
+
key = f"p_{name}_{p.name}"
|
|
142
|
+
if spec.kind == "checkbox":
|
|
143
|
+
raw[p.name] = st.checkbox(spec.label, value=spec.default, help=spec.help or None, key=key)
|
|
144
|
+
elif spec.kind == "select":
|
|
145
|
+
raw[p.name] = st.selectbox(spec.label, spec.options, index=spec.options.index(spec.default)
|
|
146
|
+
if spec.default in spec.options else 0, help=spec.help or None, key=key)
|
|
147
|
+
elif spec.kind == "number":
|
|
148
|
+
kwargs = {"value": spec.default, "help": spec.help or None, "key": key}
|
|
149
|
+
if spec.min is not None:
|
|
150
|
+
kwargs["min_value"] = type(spec.default)(spec.min)
|
|
151
|
+
if spec.max is not None:
|
|
152
|
+
kwargs["max_value"] = type(spec.default)(spec.max)
|
|
153
|
+
if spec.step is not None:
|
|
154
|
+
kwargs["step"] = type(spec.default)(spec.step)
|
|
155
|
+
raw[p.name] = st.number_input(spec.label, **kwargs)
|
|
156
|
+
else:
|
|
157
|
+
placeholder = "(example's default)" if spec.optional else None
|
|
158
|
+
raw[p.name] = st.text_input(spec.label, value=spec.default, help=spec.help or None,
|
|
159
|
+
placeholder=placeholder, key=key)
|
|
160
|
+
quick = st.checkbox("Quick mode (the example's fast settings)", value=bool(example.quick_args),
|
|
161
|
+
disabled=not example.quick_args, key=f"quick_{name}")
|
|
162
|
+
timeout = st.number_input("Stop after (seconds, 0 = no limit)", min_value=0, value=600, step=60,
|
|
163
|
+
key=f"timeout_{name}")
|
|
164
|
+
submitted = st.form_submit_button("Run", disabled=running)
|
|
165
|
+
|
|
166
|
+
if submitted and not running:
|
|
167
|
+
try:
|
|
168
|
+
values = core.form_values(example, raw)
|
|
169
|
+
except ValueError as e:
|
|
170
|
+
st.error(str(e))
|
|
171
|
+
else:
|
|
172
|
+
st.session_state["live"] = core.start_run(example, values, quick, timeout=timeout or None)
|
|
173
|
+
live = st.session_state["live"]
|
|
174
|
+
|
|
175
|
+
if live is None:
|
|
176
|
+
st.caption(f"Runs are saved under {core.runs_dir()}.")
|
|
177
|
+
return
|
|
178
|
+
st.subheader(f"{live.example.title}")
|
|
179
|
+
st.caption(live.output_dir)
|
|
180
|
+
if not live.done: # draw one frame, then rerun: the page stays responsive and nothing goes stale
|
|
181
|
+
st.info("Running ...")
|
|
182
|
+
st.code("\n".join(live.lines[-200:]) or "(starting)", language=None)
|
|
183
|
+
time.sleep(0.5)
|
|
184
|
+
st.rerun()
|
|
185
|
+
st.code("\n".join(live.lines[-200:]) or "(no output)", language=None)
|
|
186
|
+
if live.error:
|
|
187
|
+
st.error(live.error)
|
|
188
|
+
return
|
|
189
|
+
result = live.result
|
|
190
|
+
status_box = st.empty()
|
|
191
|
+
if result.ok:
|
|
192
|
+
status_box.success(f"Done in {result.seconds:.1f} s.")
|
|
193
|
+
elif result.returncode is None:
|
|
194
|
+
status_box.error(f"Stopped after {result.seconds:.0f} s (time limit).")
|
|
195
|
+
else:
|
|
196
|
+
status_box.error(f"Failed (exit code {result.returncode}). The log above shows why.")
|
|
197
|
+
st.markdown("**Repeat from a terminal**")
|
|
198
|
+
st.code(core.repeat_command(result.cwd, os.path.dirname(os.path.dirname(live.example.path)),
|
|
199
|
+
result.command_line), language=None)
|
|
200
|
+
show_outputs(result.outputs, key="live", folder=result.output_dir)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def page_results():
|
|
204
|
+
st.header("Results")
|
|
205
|
+
folder = core.runs_dir() # fixed (choose it with: sds ui --runs-dir DIR), never an arbitrary path
|
|
206
|
+
st.caption(f"Runs folder: {folder}")
|
|
207
|
+
records = core.list_runs(folder)
|
|
208
|
+
if not records:
|
|
209
|
+
st.info("No runs here yet. Start one on the Run page.")
|
|
210
|
+
return
|
|
211
|
+
def short(path):
|
|
212
|
+
try:
|
|
213
|
+
return os.path.relpath(path, folder)
|
|
214
|
+
except ValueError: # another drive on Windows
|
|
215
|
+
return path
|
|
216
|
+
table = pd.DataFrame([{"example": r.example, "finished": r.finished, "status": r.status,
|
|
217
|
+
"seconds": r.seconds, "quick": r.quick, "folder": short(r.folder)}
|
|
218
|
+
for r in records])
|
|
219
|
+
st.caption("Click a row to open the run (or choose it below).")
|
|
220
|
+
event = st.dataframe(table, on_select="rerun", selection_mode="single-row", hide_index=True,
|
|
221
|
+
key="results_table")
|
|
222
|
+
rows = list(getattr(getattr(event, "selection", None), "rows", None) or [])
|
|
223
|
+
if rows and rows[0] < len(records) and st.session_state.get("_results_row") != rows[0]:
|
|
224
|
+
st.session_state["_results_row"] = rows[0] # a new click: open that run
|
|
225
|
+
st.session_state["result_pick"] = rows[0]
|
|
226
|
+
if st.session_state.get("result_pick", 0) >= len(records):
|
|
227
|
+
st.session_state["result_pick"] = 0
|
|
228
|
+
labels = [f"{r.finished} · {r.example} · {r.status}" for r in records]
|
|
229
|
+
choice = st.selectbox("Open a run", range(len(records)), format_func=lambda i: labels[i], key="result_pick")
|
|
230
|
+
record = records[choice]
|
|
231
|
+
st.subheader(f"{record.title or record.example} · {record.status}")
|
|
232
|
+
c1, c2 = st.columns(2)
|
|
233
|
+
c1.markdown("**Parameters**")
|
|
234
|
+
c1.json(record.parameters, expanded=True)
|
|
235
|
+
c2.markdown("**Repeat from a terminal**")
|
|
236
|
+
c2.code(core.repeat_command(record.cwd, record.pythonpath, record.command_line), language=None)
|
|
237
|
+
with st.expander("Log (last lines)"):
|
|
238
|
+
st.code(core.tail(os.path.join(record.folder, "run.log")), language=None)
|
|
239
|
+
show_outputs(core.outputs_of(record, examples()), key="result", folder=record.folder)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def page_installation():
|
|
243
|
+
st.header("Installation check")
|
|
244
|
+
from scidatasim.doctor import run_checks
|
|
245
|
+
if st.button("Run the check", key="doctor_run") or "doctor" not in st.session_state:
|
|
246
|
+
import tempfile
|
|
247
|
+
with st.spinner("Checking ..."):
|
|
248
|
+
st.session_state["doctor"] = run_checks(os.path.join(tempfile.gettempdir(), "sds_doctor_probe"))
|
|
249
|
+
checks = st.session_state["doctor"]
|
|
250
|
+
for c in checks:
|
|
251
|
+
st.markdown(f"{STATUS_ICON.get(c.status, '')} **{c.name}** — {c.detail}")
|
|
252
|
+
if c.fix:
|
|
253
|
+
st.caption(f"{'Add it if you need it' if c.status == 'info' else 'Fix'}: `{c.fix}`")
|
|
254
|
+
failed = sum(c.status == "fail" for c in checks)
|
|
255
|
+
(st.error if failed else st.success)(f"{failed} problem(s)." if failed else
|
|
256
|
+
"The core works. Optional parts (ℹ️) are needed only for what they name.")
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# ---------------------------------------------------------------------------- layout
|
|
260
|
+
with st.sidebar:
|
|
261
|
+
st.title("Scientific Data Simulator")
|
|
262
|
+
st.caption(f"simulator {core.SIMULATOR_VERSION} · UI {UI_VERSION}")
|
|
263
|
+
if "page" not in st.session_state:
|
|
264
|
+
st.session_state["page"] = PAGES[0]
|
|
265
|
+
st.radio("Page", PAGES, key="page")
|
|
266
|
+
st.caption("Everything here is also available from the command line: `sds examples ...`, `sds doctor`.")
|
|
267
|
+
st.divider()
|
|
268
|
+
cite = core.citation()
|
|
269
|
+
st.caption(f"© {AUTHOR} · {LICENSE} · [source code]({REPOSITORY})")
|
|
270
|
+
if cite.get("title"):
|
|
271
|
+
with st.expander("How to cite"):
|
|
272
|
+
text = (f"{cite['authors']}. *{cite['title']}*. Version {cite['version']}. "
|
|
273
|
+
+ (f"https://doi.org/{cite['doi']}" if cite.get("doi") else cite.get("url", "")))
|
|
274
|
+
st.markdown(text)
|
|
275
|
+
st.caption("Details and BibTeX: CITATION.cff in the repository.")
|
|
276
|
+
|
|
277
|
+
{"Examples": page_examples, "Run": page_run, "Results": page_results,
|
|
278
|
+
"Installation": page_installation}[st.session_state["page"]]()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# sds_ui/cli.py
|
|
2
|
+
"""The ``sds-ui`` command (``sds ui`` calls the same)."""
|
|
3
|
+
import argparse
|
|
4
|
+
from typing import List, Optional
|
|
5
|
+
|
|
6
|
+
from sds_ui import launch
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
10
|
+
parser = argparse.ArgumentParser(prog="sds-ui", description="Start the simulator UI in your browser.")
|
|
11
|
+
parser.add_argument("--port", type=int, default=8501)
|
|
12
|
+
parser.add_argument("--address", default="127.0.0.1",
|
|
13
|
+
help="127.0.0.1 = this computer only (default). Another address lets anyone who can "
|
|
14
|
+
"reach it run examples on this computer")
|
|
15
|
+
parser.add_argument("--runs-dir", default=None, help="where runs are saved (default: sds_runs)")
|
|
16
|
+
parser.add_argument("--examples-dir", default=None)
|
|
17
|
+
args = parser.parse_args(argv)
|
|
18
|
+
return launch(args.port, args.address, args.runs_dir, args.examples_dir)
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# sds_ui/core.py
|
|
2
|
+
"""Everything the UI needs from the simulator, as plain functions (no Streamlit here).
|
|
3
|
+
|
|
4
|
+
The UI uses only the simulator's public functions - the example manifests and runner
|
|
5
|
+
(:mod:`scidatasim.examples`), the doctor (:mod:`scidatasim.doctor`) and the version - so whatever
|
|
6
|
+
it does can also be done from the command line, and a run started here is recorded exactly
|
|
7
|
+
like one started with ``sds examples run``.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import importlib.util
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import threading
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
from datetime import datetime
|
|
17
|
+
from typing import Any, Dict, List, Mapping, Optional
|
|
18
|
+
|
|
19
|
+
from scidatasim import __version__ as SIMULATOR_VERSION
|
|
20
|
+
from scidatasim.doctor import EXTRAS
|
|
21
|
+
from scidatasim.examples import Example, ExampleRun, Param, discover, effective_values, run_example
|
|
22
|
+
|
|
23
|
+
UI_SETTINGS = ("SDS_EXAMPLES", "SDS_RUNS")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def runs_dir() -> str:
|
|
27
|
+
"""Where runs started from the UI go (``$SDS_RUNS``, default ``sds_runs``)."""
|
|
28
|
+
return os.path.abspath(os.environ.get("SDS_RUNS") or "sds_runs")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def load_examples(examples_dir: Optional[str] = None) -> Dict[str, Example]:
|
|
32
|
+
return discover(examples_dir or os.environ.get("SDS_EXAMPLES") or None)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def missing_extras(example: Example) -> List[str]:
|
|
36
|
+
"""Extras the example needs that are not installed (checked by importing their modules)."""
|
|
37
|
+
missing = []
|
|
38
|
+
for extra in example.extras:
|
|
39
|
+
modules = EXTRAS.get(extra, ((),))[0]
|
|
40
|
+
if any(importlib.util.find_spec(m) is None for m in modules):
|
|
41
|
+
missing.append(extra)
|
|
42
|
+
return missing
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def filter_examples(examples: Mapping[str, Example], text: str = "", fields: Optional[List[str]] = None,
|
|
46
|
+
level: str = "any") -> List[Example]:
|
|
47
|
+
text = text.strip().lower()
|
|
48
|
+
out = []
|
|
49
|
+
for e in examples.values():
|
|
50
|
+
if text and text not in f"{e.name} {e.title} {e.summary}".lower():
|
|
51
|
+
continue
|
|
52
|
+
if fields and not set(fields) & set(e.fields):
|
|
53
|
+
continue
|
|
54
|
+
if level != "any" and e.level != level:
|
|
55
|
+
continue
|
|
56
|
+
out.append(e)
|
|
57
|
+
return out
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# ---------------------------------------------------------------------------- parameter forms
|
|
61
|
+
@dataclass
|
|
62
|
+
class WidgetSpec:
|
|
63
|
+
"""How to show one parameter: ``kind`` is number, text, checkbox or select."""
|
|
64
|
+
name: str
|
|
65
|
+
label: str
|
|
66
|
+
kind: str
|
|
67
|
+
default: Any
|
|
68
|
+
help: str = ""
|
|
69
|
+
min: Optional[float] = None
|
|
70
|
+
max: Optional[float] = None
|
|
71
|
+
step: Optional[float] = None
|
|
72
|
+
options: List[Any] = field(default_factory=list)
|
|
73
|
+
optional: bool = False # no default: an empty field keeps the example's own behaviour
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def widget_spec(p: Param) -> WidgetSpec:
|
|
77
|
+
label = p.name.replace("_", " ")
|
|
78
|
+
if p.type == "flag":
|
|
79
|
+
return WidgetSpec(p.name, label, "checkbox", bool(p.default), p.help)
|
|
80
|
+
if p.type == "choice":
|
|
81
|
+
return WidgetSpec(p.name, label, "select", p.default, p.help, options=list(p.choices or []))
|
|
82
|
+
if p.type in ("int", "float") and p.default is not None:
|
|
83
|
+
cast = int if p.type == "int" else float # the widget's number type follows the parameter's
|
|
84
|
+
return WidgetSpec(p.name, label, "number", cast(p.default), p.help,
|
|
85
|
+
None if p.min is None else cast(p.min), None if p.max is None else cast(p.max),
|
|
86
|
+
1 if p.type == "int" else None)
|
|
87
|
+
return WidgetSpec(p.name, label, "text", "" if p.default is None else str(p.default), p.help,
|
|
88
|
+
optional=p.default is None)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def form_values(example: Example, raw: Mapping[str, Any]) -> Dict[str, Any]:
|
|
92
|
+
"""Values to pass to the runner: only those that differ from the defaults, converted and
|
|
93
|
+
checked by the manifest's own rules (raises ValueError with the parameter's name)."""
|
|
94
|
+
values = {}
|
|
95
|
+
for p in example.params:
|
|
96
|
+
if p.name not in raw:
|
|
97
|
+
continue
|
|
98
|
+
value = raw[p.name]
|
|
99
|
+
if isinstance(value, str) and value.strip() == "" and p.default is None:
|
|
100
|
+
continue
|
|
101
|
+
value = p.coerce(value)
|
|
102
|
+
if value != p.default:
|
|
103
|
+
values[p.name] = value
|
|
104
|
+
return values
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# ---------------------------------------------------------------------------- running
|
|
108
|
+
@dataclass
|
|
109
|
+
class LiveRun:
|
|
110
|
+
"""An example running in a background thread; the page polls ``lines`` and ``done``."""
|
|
111
|
+
example: Example
|
|
112
|
+
values: Dict[str, Any]
|
|
113
|
+
quick: bool
|
|
114
|
+
output_dir: str
|
|
115
|
+
lines: List[str] = field(default_factory=list)
|
|
116
|
+
result: Optional[ExampleRun] = None
|
|
117
|
+
error: Optional[str] = None
|
|
118
|
+
thread: Optional[threading.Thread] = None
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def done(self) -> bool:
|
|
122
|
+
return self.thread is not None and not self.thread.is_alive()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def run_folder_name() -> str:
|
|
126
|
+
"""Time stamp plus a random suffix: two runs started in the same second (two browser tabs)
|
|
127
|
+
never share a folder."""
|
|
128
|
+
import uuid
|
|
129
|
+
return datetime.now().strftime("%Y%m%d-%H%M%S-") + uuid.uuid4().hex[:6]
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def start_run(example: Example, values: Mapping[str, Any], quick: bool, timeout: Optional[float] = None,
|
|
133
|
+
output_dir: Optional[str] = None) -> LiveRun:
|
|
134
|
+
output_dir = output_dir or os.path.join(runs_dir(), example.name, run_folder_name())
|
|
135
|
+
live = LiveRun(example, dict(values), quick, os.path.abspath(output_dir))
|
|
136
|
+
|
|
137
|
+
def target():
|
|
138
|
+
try:
|
|
139
|
+
live.result = run_example(example, live.values, quick=quick, output_dir=live.output_dir,
|
|
140
|
+
timeout=timeout, on_line=live.lines.append)
|
|
141
|
+
except Exception as e: # shown on the page, never lost
|
|
142
|
+
live.error = f"{type(e).__name__}: {e}"
|
|
143
|
+
|
|
144
|
+
live.thread = threading.Thread(target=target, daemon=True)
|
|
145
|
+
live.thread.start()
|
|
146
|
+
return live
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# ---------------------------------------------------------------------------- past runs
|
|
150
|
+
@dataclass
|
|
151
|
+
class RunRecord:
|
|
152
|
+
folder: str
|
|
153
|
+
example: str
|
|
154
|
+
title: str
|
|
155
|
+
finished: str
|
|
156
|
+
quick: bool
|
|
157
|
+
returncode: Optional[int]
|
|
158
|
+
seconds: float
|
|
159
|
+
parameters: Dict[str, Any]
|
|
160
|
+
command_line: str
|
|
161
|
+
outputs: List[str]
|
|
162
|
+
cwd: str = ""
|
|
163
|
+
pythonpath: str = ""
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def status(self) -> str:
|
|
167
|
+
if self.returncode == 0:
|
|
168
|
+
return "ok"
|
|
169
|
+
return "timed out" if self.returncode is None else f"failed ({self.returncode})"
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def list_runs(folder: Optional[str] = None) -> List[RunRecord]:
|
|
173
|
+
"""Runs recorded under ``folder`` (any depth), newest first. Unreadable or malformed
|
|
174
|
+
``run.json`` files are skipped, and runs still in progress (a ``run.log`` without
|
|
175
|
+
``run.json``) are not searched."""
|
|
176
|
+
folder = folder or runs_dir()
|
|
177
|
+
records = []
|
|
178
|
+
if not os.path.isdir(folder):
|
|
179
|
+
return records
|
|
180
|
+
for root, dirs, files in os.walk(folder):
|
|
181
|
+
if "run.json" not in files:
|
|
182
|
+
if "run.log" in files:
|
|
183
|
+
dirs[:] = [] # a run in progress: its outputs are not runs
|
|
184
|
+
continue
|
|
185
|
+
dirs[:] = [] # a run's own outputs are not searched for more runs
|
|
186
|
+
try:
|
|
187
|
+
with open(os.path.join(root, "run.json"), encoding="utf-8") as f:
|
|
188
|
+
data = json.load(f)
|
|
189
|
+
if not isinstance(data, dict):
|
|
190
|
+
continue
|
|
191
|
+
outputs = data.get("outputs") or []
|
|
192
|
+
records.append(RunRecord(
|
|
193
|
+
root, str(data.get("example", "?")), str(data.get("title", "")), str(data.get("finished") or ""),
|
|
194
|
+
bool(data.get("quick")), data.get("returncode") if isinstance(data.get("returncode"), int) else None,
|
|
195
|
+
float(data.get("seconds") or 0),
|
|
196
|
+
data.get("parameters") if isinstance(data.get("parameters"), dict) else {},
|
|
197
|
+
str(data.get("command_line", "")),
|
|
198
|
+
[os.path.join(root, str(o)) for o in outputs] if isinstance(outputs, list) else [],
|
|
199
|
+
cwd=str(data.get("cwd") or ""), pythonpath=str(data.get("pythonpath") or "")))
|
|
200
|
+
except (OSError, ValueError, TypeError):
|
|
201
|
+
continue
|
|
202
|
+
return sorted(records, key=lambda r: r.finished, reverse=True)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def repeat_command(cwd: str, pythonpath: str, command_line: str, windows: Optional[bool] = None) -> str:
|
|
206
|
+
"""Shell lines that repeat a run from any folder, for this system's shell."""
|
|
207
|
+
windows = os.name == "nt" if windows is None else windows
|
|
208
|
+
if windows:
|
|
209
|
+
lines = [f'cd /d "{cwd}"'] if cwd else []
|
|
210
|
+
if pythonpath:
|
|
211
|
+
lines.append(f'set "PYTHONPATH={pythonpath}"')
|
|
212
|
+
return "\n".join(lines + [command_line])
|
|
213
|
+
import shlex
|
|
214
|
+
prefix = f"cd {shlex.quote(cwd)} && " if cwd else ""
|
|
215
|
+
env = f"PYTHONPATH={shlex.quote(pythonpath)} " if pythonpath else ""
|
|
216
|
+
return prefix + env + command_line
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def outputs_of(record: RunRecord, examples: Mapping[str, Example]) -> List[Dict[str, str]]:
|
|
220
|
+
"""The outputs of a past run, with the kinds its example's manifest gives them."""
|
|
221
|
+
from scidatasim.examples import find_outputs
|
|
222
|
+
example = examples.get(record.example)
|
|
223
|
+
if example is not None:
|
|
224
|
+
return find_outputs(example, record.folder)
|
|
225
|
+
return [{"path": p, "kind": guess_kind(p)} for p in record.outputs]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def figures_in(folder: str, limit: int = 12) -> List[Dict[str, str]]:
|
|
229
|
+
"""Figures anywhere in a run folder (PNG, SVG, JPEG), for runs whose manifest lists none."""
|
|
230
|
+
from glob import glob
|
|
231
|
+
found: List[str] = []
|
|
232
|
+
for pattern in ("*.png", "*.svg", "*.jpg", "*.jpeg"):
|
|
233
|
+
found += glob(os.path.join(folder, "**", pattern), recursive=True)
|
|
234
|
+
return [{"path": p, "kind": "image"} for p in sorted(found)[:limit]]
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def citation() -> Dict[str, str]:
|
|
238
|
+
"""Title, authors, version and DOI from ``CITATION.cff`` in a source checkout (empty if absent)."""
|
|
239
|
+
import scidatasim
|
|
240
|
+
import yaml
|
|
241
|
+
path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(scidatasim.__file__))), "CITATION.cff")
|
|
242
|
+
try:
|
|
243
|
+
with open(path, encoding="utf-8") as f:
|
|
244
|
+
data = yaml.safe_load(f) or {}
|
|
245
|
+
except (OSError, yaml.YAMLError):
|
|
246
|
+
return {}
|
|
247
|
+
authors = ", ".join(" ".join(filter(None, [a.get("given-names"), a.get("family-names")]))
|
|
248
|
+
for a in data.get("authors") or [] if isinstance(a, dict))
|
|
249
|
+
return {"title": str(data.get("title", "")), "authors": authors, "version": str(data.get("version", "")),
|
|
250
|
+
"doi": str(data.get("doi", "") or ""), "url": str(data.get("repository-code", "") or "")}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def guess_kind(path: str) -> str:
|
|
254
|
+
ext = os.path.splitext(path)[1].lower()
|
|
255
|
+
return {".md": "markdown", ".png": "image", ".jpg": "image", ".jpeg": "image", ".svg": "image",
|
|
256
|
+
".html": "html", ".csv": "table", ".json": "json"}.get(ext, "text")
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def tail(path: str, n: int = 200, max_bytes: int = 256 * 1024) -> str:
|
|
260
|
+
"""The last ``n`` lines, reading at most ``max_bytes`` from the end of the file."""
|
|
261
|
+
try:
|
|
262
|
+
with open(path, "rb") as f:
|
|
263
|
+
f.seek(0, os.SEEK_END)
|
|
264
|
+
size = f.tell()
|
|
265
|
+
f.seek(max(0, size - max_bytes))
|
|
266
|
+
text = f.read().decode("utf-8", errors="replace")
|
|
267
|
+
return "\n".join(text.splitlines()[-n:])
|
|
268
|
+
except OSError:
|
|
269
|
+
return ""
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# ui/tests/test_ui.py
|
|
2
|
+
"""The UI, run headless with Streamlit's AppTest: every page, and a real example run."""
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
pytest.importorskip("streamlit")
|
|
9
|
+
from streamlit.testing.v1 import AppTest # noqa: E402
|
|
10
|
+
|
|
11
|
+
from sds_ui import APP, core # noqa: E402
|
|
12
|
+
from scidatasim.examples import discover # noqa: E402
|
|
13
|
+
|
|
14
|
+
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
15
|
+
EXAMPLES = os.path.join(ROOT, "examples")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture
|
|
19
|
+
def app(tmp_path, monkeypatch):
|
|
20
|
+
monkeypatch.setenv("SDS_RUNS", str(tmp_path / "runs"))
|
|
21
|
+
monkeypatch.setenv("SDS_EXAMPLES", EXAMPLES)
|
|
22
|
+
monkeypatch.chdir(ROOT)
|
|
23
|
+
at = AppTest.from_file(APP, default_timeout=60)
|
|
24
|
+
at.run()
|
|
25
|
+
assert not at.exception, at.exception
|
|
26
|
+
return at
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _page(at, name):
|
|
30
|
+
at.sidebar.radio(key="page").set_value(name).run()
|
|
31
|
+
assert not at.exception, at.exception
|
|
32
|
+
return at
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ------------------------------------------------------------------ pure helpers
|
|
36
|
+
def test_widget_specs_cover_every_manifest_parameter():
|
|
37
|
+
for example in discover(EXAMPLES).values():
|
|
38
|
+
for p in example.params:
|
|
39
|
+
spec = core.widget_spec(p)
|
|
40
|
+
assert spec.kind in ("number", "text", "checkbox", "select")
|
|
41
|
+
if spec.kind == "number":
|
|
42
|
+
assert p.default is not None and (p.min is None or p.min <= spec.default)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_form_values_keep_only_changes_and_validate():
|
|
46
|
+
ex = discover(EXAMPLES)["example_10_law_discovery"]
|
|
47
|
+
assert core.form_values(ex, {"replicates": 1, "suite_seed": "", "export": False}) == {}
|
|
48
|
+
assert core.form_values(ex, {"replicates": 3, "suite_seed": "7", "export": True}) == \
|
|
49
|
+
{"replicates": 3, "suite_seed": 7, "export": True}
|
|
50
|
+
with pytest.raises(ValueError, match="replicates"):
|
|
51
|
+
core.form_values(ex, {"replicates": 99})
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_filter_and_missing_extras():
|
|
55
|
+
found = discover(EXAMPLES)
|
|
56
|
+
assert {e.name for e in core.filter_examples(found, level="student")} >= {"example_1", "example_doe_1"}
|
|
57
|
+
assert all("geometry" in e.fields for e in core.filter_examples(found, fields=["geometry"]))
|
|
58
|
+
assert [e.name for e in core.filter_examples(found, text="law discovery")][0] == "example_10_law_discovery"
|
|
59
|
+
assert isinstance(core.missing_extras(found["example_8_geometry_benchmark"]), list)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_live_run_and_listing(tmp_path, monkeypatch):
|
|
63
|
+
monkeypatch.setenv("SDS_RUNS", str(tmp_path))
|
|
64
|
+
ex = discover(EXAMPLES)["example_4"]
|
|
65
|
+
live = core.start_run(ex, {}, quick=True)
|
|
66
|
+
live.thread.join(60)
|
|
67
|
+
assert live.done and live.result.ok and any("Experiment completed" in l for l in live.lines)
|
|
68
|
+
records = core.list_runs()
|
|
69
|
+
assert len(records) == 1 and records[0].example == "example_4" and records[0].status == "ok"
|
|
70
|
+
outputs = core.outputs_of(records[0], discover(EXAMPLES))
|
|
71
|
+
assert outputs and all(o["kind"] in ("image", "html", "json") for o in outputs)
|
|
72
|
+
assert core.list_runs(str(tmp_path / "missing")) == []
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# ------------------------------------------------------------------ pages
|
|
76
|
+
def test_examples_page_lists_and_filters(app):
|
|
77
|
+
titles = [s.value for s in app.subheader]
|
|
78
|
+
assert len(titles) == len(discover(EXAMPLES))
|
|
79
|
+
app.text_input(key="search").set_value("geometry").run()
|
|
80
|
+
assert [s.value for s in app.subheader] == ["Geometry benchmark"]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_open_button_goes_to_the_run_form(app):
|
|
84
|
+
app.button(key="open_example_8_geometry_benchmark").click().run()
|
|
85
|
+
assert not app.exception and not app.warning, [w.value for w in app.warning]
|
|
86
|
+
assert app.session_state["page"] == "Run"
|
|
87
|
+
assert app.selectbox(key="run_example_select").value == "example_8_geometry_benchmark"
|
|
88
|
+
assert app.number_input(key="p_example_8_geometry_benchmark_n").value == 12
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_run_page_runs_an_example_and_shows_outputs(app, tmp_path):
|
|
92
|
+
_page(app, "Run")
|
|
93
|
+
app.selectbox(key="run_example_select").set_value("example_4").run()
|
|
94
|
+
app.button[0].click().run(timeout=120) # the form's Run button
|
|
95
|
+
assert not app.exception, app.exception
|
|
96
|
+
assert any("Done in" in s.value for s in app.success)
|
|
97
|
+
assert any("examples.example_4.run_experiment" in c.value and "PYTHONPATH" in c.value for c in app.code)
|
|
98
|
+
assert not app.warning, [w.value for w in app.warning]
|
|
99
|
+
labels = [e.label for e in app.expander]
|
|
100
|
+
assert "json: ground_truth.json" in labels and any(l.startswith("html:") for l in labels)
|
|
101
|
+
assert len(app.get("imgs")) + len(app.get("image")) >= 1 # the figures (element name varies by version)
|
|
102
|
+
runs = core.list_runs(str(tmp_path / "runs"))
|
|
103
|
+
assert len(runs) == 1 and runs[0].status == "ok"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_invalid_form_value_is_reported(app, tmp_path):
|
|
107
|
+
_page(app, "Run")
|
|
108
|
+
app.selectbox(key="run_example_select").set_value("example_10_law_discovery").run()
|
|
109
|
+
app.text_input(key="p_example_10_law_discovery_suite_seed").set_value("not a number")
|
|
110
|
+
app.button[0].click().run()
|
|
111
|
+
assert not app.exception
|
|
112
|
+
assert any("suite_seed" in e.value for e in app.error)
|
|
113
|
+
assert core.list_runs(str(tmp_path / "runs")) == [] # nothing was started
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_results_page_shows_past_runs(app, tmp_path):
|
|
117
|
+
folder = tmp_path / "runs" / "example_4" / "x"
|
|
118
|
+
folder.mkdir(parents=True)
|
|
119
|
+
(folder / "run.json").write_text(json.dumps({"example": "example_4", "title": "Linear function",
|
|
120
|
+
"finished": "2026-01-01T00:00:00", "returncode": 1,
|
|
121
|
+
"seconds": 1.0, "parameters": {}, "command_line": "python -m x",
|
|
122
|
+
"outputs": []}))
|
|
123
|
+
(folder / "run.log").write_text("Traceback: boom\n")
|
|
124
|
+
_page(app, "Results")
|
|
125
|
+
assert len(app.dataframe) >= 1
|
|
126
|
+
assert any("boom" in c.value for c in app.code)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_corrupt_run_records_are_skipped(app, tmp_path):
|
|
130
|
+
runs = tmp_path / "runs"
|
|
131
|
+
for i, text in enumerate(['[1, 2]', '"str"', '{"seconds": "abc"}', '{"finished": null, "example": "x"}', "{bad"]):
|
|
132
|
+
folder = runs / f"r{i}"
|
|
133
|
+
folder.mkdir(parents=True)
|
|
134
|
+
(folder / "run.json").write_text(text)
|
|
135
|
+
records = core.list_runs(str(runs))
|
|
136
|
+
assert [r.example for r in records] == ["x"]
|
|
137
|
+
_page(app, "Results")
|
|
138
|
+
assert not app.exception
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_two_runs_in_the_same_second_get_their_own_folders(tmp_path, monkeypatch):
|
|
142
|
+
monkeypatch.setenv("SDS_RUNS", str(tmp_path))
|
|
143
|
+
ex = discover(EXAMPLES)["example_4"]
|
|
144
|
+
a, b = core.start_run(ex, {}, True), core.start_run(ex, {}, True)
|
|
145
|
+
a.thread.join(60)
|
|
146
|
+
b.thread.join(60)
|
|
147
|
+
assert a.output_dir != b.output_dir and len(core.list_runs(str(tmp_path))) == 2
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_repeat_command():
|
|
151
|
+
posix = core.repeat_command("/my runs/x", "/repo", "python -m examples.e.run", windows=False)
|
|
152
|
+
assert posix == "cd '/my runs/x' && PYTHONPATH=/repo python -m examples.e.run"
|
|
153
|
+
win = core.repeat_command(r"C:\my runs", r"C:\repo", "python -m e", windows=True)
|
|
154
|
+
assert win.splitlines() == ['cd /d "C:\\my runs"', 'set "PYTHONPATH=C:\\repo"', "python -m e"]
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_results_page_empty(app):
|
|
158
|
+
_page(app, "Results")
|
|
159
|
+
assert any("No runs here yet" in i.value for i in app.info)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_installation_page(app):
|
|
163
|
+
_page(app, "Installation")
|
|
164
|
+
text = " ".join(m.value for m in app.markdown)
|
|
165
|
+
assert "test simulation" in text and "python" in text
|
|
166
|
+
assert app.success or app.error
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_figures_fallback_and_citation(tmp_path):
|
|
170
|
+
(tmp_path / "a" / "b").mkdir(parents=True)
|
|
171
|
+
(tmp_path / "a" / "b" / "fig.png").write_bytes(b"\x89PNG")
|
|
172
|
+
(tmp_path / "notes.txt").write_text("x")
|
|
173
|
+
assert [os.path.basename(f["path"]) for f in core.figures_in(str(tmp_path))] == ["fig.png"]
|
|
174
|
+
cite = core.citation()
|
|
175
|
+
assert "Sandner" in cite["authors"] and cite["title"] and cite["version"]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def test_sidebar_credits(app):
|
|
179
|
+
text = " ".join(c.value for c in app.sidebar.caption)
|
|
180
|
+
assert "Daniel Sandner" in text and "Apache-2.0" in text
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_interactive_plot_uses_st_iframe(app, tmp_path):
|
|
184
|
+
_page(app, "Run")
|
|
185
|
+
app.selectbox(key="run_example_select").set_value("example_4").run()
|
|
186
|
+
app.button[0].click().run(timeout=120)
|
|
187
|
+
toggles = [t for t in app.toggle if t.key.startswith("html_live_")]
|
|
188
|
+
assert toggles
|
|
189
|
+
toggles[0].set_value(True).run()
|
|
190
|
+
assert not app.exception, app.exception
|
|
191
|
+
with open(APP, encoding="utf-8") as f:
|
|
192
|
+
assert "components.v1" not in f.read() # deprecated in Streamlit 1.67
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def test_windows_colour_switch_is_harmless():
|
|
196
|
+
from sds_ui import _enable_windows_colours
|
|
197
|
+
_enable_windows_colours() # a no-op off Windows; must never raise
|