diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fd0cd7..6a4c68a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm sooner. scipy is loaded in the background after the app appears, and pandas and the plotting libraries are imported while you read the Home page. A page that needs scipy says so for a moment if you open it within the first seconds. +- **Install the web app like a program.** Chrome and Edge offer **Install EaglePlot**: + it then opens in its own window from a desktop icon, with no admin rights, and after one + online visit it also works offline. Later visits load from the computer instead of the + network. - Importing `eagleplot.plotting` no longer imports seaborn, plotly or scipy (they load when a plot type needs them), and importing `eagleplot.utils` no longer imports pandas. This also makes the installed GUI start a little faster. diff --git a/packaging/web/README.md b/packaging/web/README.md index 1d2c9c5..60ea39c 100644 --- a/packaging/web/README.md +++ b/packaging/web/README.md @@ -47,6 +47,8 @@ Streamlit exception, uncaught page error, failed download or request to another | --- | --- | | `index.html` | Loading splash (below), the stlite mount call, and a download workaround (below) | | `404.html` | A copy of `index.html`, for hosts that serve the nearest `404.html` (below) | +| `manifest.webmanifest`, `icons/` | Web app manifest and icons: the app can be installed (below) | +| `sw.js` | Service worker: caches the app and Pyodide, so it opens offline (below) | | `site-config.js` | Written by the build: entrypoint, packages, wheels, Pyodide URL, Streamlit flags, storage mount, environment | | `eagleplot-app.zip` | `eagleplot/` (code, assets, gallery, example data) and `.streamlit/config.toml`; entrypoint `eagleplot/Home.py` | | `wheels/` | Pure-Python packages not in Pyodide: seaborn, plotly, narwhals, openpyxl, et-xmlfile, jcamp (`.jdx` uploads), and Streamlit's python-multipart and itsdangerous | @@ -73,6 +75,29 @@ Opening `index.html` from disk fails: the app runs in a module Web Worker and lo app zip, wheels and Pyodide with `fetch()`, and browsers block both for `file://` pages. Serve the directory over HTTP (`python -m http.server`, GitHub Pages, any static host). +## Installable app and offline use + +`manifest.webmanifest` (written by the build, icons in `icons/`) and the service worker +`sw.js` make the app installable: Chrome and Edge show **Install EaglePlot** in the +address bar or menu. It then opens in its own window from a desktop or Start-menu icon, +with no admin rights. After one online visit it also opens offline. + +- **First visit:** `index.html` registers `sw.js` and waits (at most 3 s) until it + controls the page before starting Python, so this visit's downloads, the Python + worker's included, pass through it and are cached. The page and the stlite runtime load + before that; the page sends their URLs to the worker to cache. +- **Site files:** network first (a new deploy shows up at once), the cached copy offline. + The cache is per build (`build_site.py` stamps `sw.js` with a hash of the build), and + older builds' caches are removed when a new one activates. +- **Pyodide from the CDN:** cache first; its URLs contain the version and never change. +- `window.eaglePlotOffline` says what happened: `ready`, `next-visit` (the worker didn't + take over within 3 s), `failed`, or `unsupported` (no service workers, or not https). +- `scripts/web/smoke.js` checks both: Chromium's installability check, then a reload + with the network off. +- The icons are PNGs made from `eagleplot/assets/logo.png` (192 and 512 px, maskable + versions on the dark background, a 180 px Apple touch icon). Regenerate them if the + logo changes. + ## Loading splash `index.html` draws the splash before anything else loads: a classic inline script, inline diff --git a/packaging/web/build_site.py b/packaging/web/build_site.py index ec5fd08..bda9e78 100644 --- a/packaging/web/build_site.py +++ b/packaging/web/build_site.py @@ -23,6 +23,7 @@ from __future__ import annotations import argparse +import hashlib import io import json import shutil @@ -238,6 +239,7 @@ def main(argv: Optional[List[str]] = None) -> None: "window.EAGLEPLOT_STLITE = " + json.dumps(config, indent=2) + ";\n", encoding="utf-8" ) (out / ".nojekyll").write_text("") + write_pwa_files(out) pyodide_dir = out / "pyodide" size = site_size_mb(out, exclude=pyodide_dir) @@ -249,5 +251,54 @@ def main(argv: Optional[List[str]] = None) -> None: print(msg) +#: Web app manifest (roadmap D4.2): with sw.js, Chrome and Edge offer "Install EaglePlot". +MANIFEST = { + "name": "EaglePlot", + "short_name": "EaglePlot", + "description": "Scientific data visualization and analysis, running in your browser.", + "start_url": "./", + "scope": "./", + "display": "standalone", + "background_color": "#1b1b1b", + "theme_color": "#1b1b1b", + "icons": [ + {"src": "icons/icon-192.png", "sizes": "192x192", "type": "image/png", "purpose": "any"}, + {"src": "icons/icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "any"}, + { + "src": "icons/maskable-192.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "maskable", + }, + { + "src": "icons/maskable-512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable", + }, + ], +} + + +def build_id(out: Path) -> str: + """A hash of the files that change between deploys (app, config, page, wheel names).""" + h = hashlib.sha256() + for name in ("index.html", "site-config.js", "eagleplot-app.zip"): + h.update((out / name).read_bytes()) + for wheel in sorted((out / "wheels").glob("*.whl")): + h.update(wheel.name.encode()) + return h.hexdigest()[:12] + + +def write_pwa_files(out: Path) -> None: + """Manifest, icons and the service worker (stamped with this build's id).""" + (out / "manifest.webmanifest").write_text(json.dumps(MANIFEST, indent=2), encoding="utf-8") + shutil.copytree(HERE / "icons", out / "icons") + sw = (HERE / "sw.js").read_text(encoding="utf-8") + if "__EAGLEPLOT_BUILD__" not in sw: + sys.exit("packaging/web/sw.js has lost its __EAGLEPLOT_BUILD__ placeholder") + (out / "sw.js").write_text(sw.replace("__EAGLEPLOT_BUILD__", build_id(out)), encoding="utf-8") + + if __name__ == "__main__": main() diff --git a/packaging/web/icons/apple-touch-icon.png b/packaging/web/icons/apple-touch-icon.png new file mode 100644 index 0000000..e7e793a Binary files /dev/null and b/packaging/web/icons/apple-touch-icon.png differ diff --git a/packaging/web/icons/icon-192.png b/packaging/web/icons/icon-192.png new file mode 100644 index 0000000..ce2145e Binary files /dev/null and b/packaging/web/icons/icon-192.png differ diff --git a/packaging/web/icons/icon-512.png b/packaging/web/icons/icon-512.png new file mode 100644 index 0000000..8d22094 Binary files /dev/null and b/packaging/web/icons/icon-512.png differ diff --git a/packaging/web/icons/maskable-192.png b/packaging/web/icons/maskable-192.png new file mode 100644 index 0000000..04d89f7 Binary files /dev/null and b/packaging/web/icons/maskable-192.png differ diff --git a/packaging/web/icons/maskable-512.png b/packaging/web/icons/maskable-512.png new file mode 100644 index 0000000..a33e040 Binary files /dev/null and b/packaging/web/icons/maskable-512.png differ diff --git a/packaging/web/index.html b/packaging/web/index.html index a40f992..4271b81 100644 --- a/packaging/web/index.html +++ b/packaging/web/index.html @@ -5,6 +5,10 @@