From 09977d83434e692428b510136c0df196429d0cc1 Mon Sep 17 00:00:00 2001 From: Sloan Finger Date: Mon, 19 Oct 2026 11:39:13 -0400 Subject: [PATCH 1/2] Also run the app in the browser with stlite, deployed to Firebase Hosting --- .firebaserc | 5 +++ .github/workflows/ci.yml | 40 ++++++++++++++++++++++++ .gitignore | 3 ++ README.md | 18 +++++++++++ firebase.json | 6 ++++ scripts/build_site.py | 67 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+) create mode 100644 .firebaserc create mode 100644 firebase.json create mode 100644 scripts/build_site.py diff --git a/.firebaserc b/.firebaserc new file mode 100644 index 0000000..21d433a --- /dev/null +++ b/.firebaserc @@ -0,0 +1,5 @@ +{ + "projects": { + "default": "g311-communication-needs-map" + } +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index addee33..e4e342e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,3 +23,43 @@ jobs: run: | uv export --no-dev --no-hashes --no-emit-project -o requirements.txt git diff --exit-code requirements.txt + + # The in-browser (stlite) version on Firebase Hosting. Each pull request gets a preview link; + # merging to main deploys https://g311-communication-needs-map.web.app. + deploy: + needs: check + # Pull requests from forks can't sign in to Google Cloud, so they only get the check. + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write # lets GitHub prove to Google Cloud which repo is deploying (no stored keys) + pull-requests: write # for the preview-link comment + env: + FIREBASE_PROJECT: g311-communication-needs-map + steps: + - uses: actions/checkout@v7 + - uses: astral-sh/setup-uv@v10 + - run: uv run --no-dev python scripts/build_site.py + - uses: google-github-actions/auth@v3 + with: + workload_identity_provider: projects/139531751236/locations/global/workloadIdentityPools/github/providers/github + service_account: github-deploy@g311-communication-needs-map.iam.gserviceaccount.com + - name: Deploy the live site + if: github.event_name == 'push' + run: npx --yes firebase-tools@15 deploy --only hosting --project "$FIREBASE_PROJECT" --non-interactive + - name: Deploy a preview + if: github.event_name == 'pull_request' + id: preview + run: | + npx --yes firebase-tools@15 hosting:channel:deploy "pr-${{ github.event.pull_request.number }}" \ + --expires 7d --project "$FIREBASE_PROJECT" --json > preview.json + echo "url=$(jq -r '.result[].url' preview.json | head -n 1)" >> "$GITHUB_OUTPUT" + - name: Comment the preview link + if: github.event_name == 'pull_request' + uses: marocchino/sticky-pull-request-comment@v3 + with: + header: preview + message: | + Preview: ${{ steps.preview.outputs.url }} + (updates on every push to this PR, expires 7 days after the last push) diff --git a/.gitignore b/.gitignore index c493864..298eaec 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ __pycache__/ .pytest_cache/ .ruff_cache/ .streamlit/secrets.toml +site/ +.firebase/ +*-debug.log diff --git a/README.md b/README.md index a1fb0d0..79cc58c 100644 --- a/README.md +++ b/README.md @@ -46,4 +46,22 @@ uv export --no-dev --no-hashes --no-emit-project -o requirements.txt ## Deploy +### Firebase Hosting (stlite) + +The same `app.py` also runs entirely in the visitor's browser with [stlite](https://github.com/whitphx/stlite), served from Firebase Hosting: + +- **Every pull request** gets a preview link, posted as a comment on the PR. +- **Merging to `main`** deploys https://g311-communication-needs-map.web.app + +`scripts/build_site.py` builds it into `site/`. To try it locally: + +```sh +uv run python scripts/build_site.py +uv run python -m http.server -d site # http://localhost:8000 +``` + +Trade-offs: the first load takes 10 to 30 seconds while Python downloads into the browser, and only pure-Python packages or ones [Pyodide supports](https://pyodide.org/en/stable/usage/packages-in-pyodide.html) work. Keep heavy work (geopandas, API calls) in `scripts/` and commit the results to `data/`. + +### Streamlit Community Cloud + [Streamlit Community Cloud](https://streamlit.io/cloud) is free and runs straight from this repo: sign in with GitHub, choose **Create app**, pick this repo, branch `main` and file `app.py`. It redeploys on every push to `main`. Apps sleep after 12 hours without visitors and wake on the next visit. diff --git a/firebase.json b/firebase.json new file mode 100644 index 0000000..4bfbb85 --- /dev/null +++ b/firebase.json @@ -0,0 +1,6 @@ +{ + "hosting": { + "public": "site", + "ignore": ["firebase.json", "**/node_modules/**"] + } +} diff --git a/scripts/build_site.py b/scripts/build_site.py new file mode 100644 index 0000000..b09c78c --- /dev/null +++ b/scripts/build_site.py @@ -0,0 +1,67 @@ +"""Builds site/: the same app running in the browser with stlite (https://github.com/whitphx/stlite). + +Run with `uv run python scripts/build_site.py`, then preview with +`uv run python -m http.server -d site` and open http://localhost:8000. +CI runs this and deploys site/ to Firebase Hosting. +""" + +import json +import re +import shutil +import tomllib +from pathlib import Path + +ROOT = Path(__file__).parent.parent +SITE = ROOT / "site" +STLITE = "1.12.0" # https://www.npmjs.com/package/@stlite/browser + +# The app's own dependencies (from pyproject.toml); stlite installs them in the browser. +# Version pins are dropped: the browser (Pyodide) ships its own builds of packages like pandas. +# Streamlit itself comes with stlite. +dependencies = tomllib.loads((ROOT / "pyproject.toml").read_text())["project"][ + "dependencies" +] +names = [re.split(r"[<>=!~\[ ]", d, maxsplit=1)[0] for d in dependencies] +requirements = [n for n in names if n != "streamlit"] + +shutil.rmtree(SITE, ignore_errors=True) +shutil.copytree(ROOT / "data", SITE / "data") +shutil.copy(ROOT / "app.py", SITE / "app.py") +if (ROOT / ".streamlit" / "config.toml").exists(): + shutil.copytree( + ROOT / ".streamlit", + SITE / ".streamlit", + ignore=shutil.ignore_patterns("secrets*"), + ) + +files = {"app.py": {"url": "./app.py"}} +for path in sorted(SITE.rglob("*")): + name = path.relative_to(SITE).as_posix() + if path.is_file() and name != "app.py": + files[name] = {"url": f"./{name}"} + +(SITE / "index.html").write_text(f""" + + + + + Communication-needs Map + + + +
+ + + +""") +print(f"Built {SITE} with {len(files)} files") From 9fba61d33d377363aa25295e39b19032aa0699a5 Mon Sep 17 00:00:00 2001 From: Sloan Finger Date: Mon, 19 Oct 2026 12:29:44 -0400 Subject: [PATCH 2/2] Pin setup-uv to v10.3.0 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e4e342e..850439e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,7 +39,7 @@ jobs: FIREBASE_PROJECT: g311-communication-needs-map steps: - uses: actions/checkout@v7 - - uses: astral-sh/setup-uv@v10 + - uses: astral-sh/setup-uv@v10.3.0 - run: uv run --no-dev python scripts/build_site.py - uses: google-github-actions/auth@v3 with: