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
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "g311-communication-needs-map"
}
}
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.3.0
- 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)
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ __pycache__/
.pytest_cache/
.ruff_cache/
.streamlit/secrets.toml
site/
.firebase/
*-debug.log
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,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.
6 changes: 6 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hosting": {
"public": "site",
"ignore": ["firebase.json", "**/node_modules/**"]
}
}
67 changes: 67 additions & 0 deletions scripts/build_site.py
Original file line number Diff line number Diff line change
@@ -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"""<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Communication-needs Map</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@stlite/browser@{STLITE}/build/stlite.css" />
</head>
<body>
<div id="root"></div>
<script type="module">
import {{ mount }} from "https://cdn.jsdelivr.net/npm/@stlite/browser@{STLITE}/build/stlite.js";
mount(
{{
entrypoint: "app.py",
requirements: {json.dumps(requirements)},
files: {json.dumps(files, indent=2)},
}},
document.getElementById("root"),
);
</script>
</body>
</html>
""")
print(f"Built {SITE} with {len(files)} files")
Loading