Skip to content

About

a proof of concept front end to register, see access, then preview and download data from the API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

api-frontend

A proof-of-concept web app for the WattTime API: create an account, see on a map which grid regions and signals the account includes, then preview and download historical data as CSV. Built with Streamlit and hosted on Streamlit Community Cloud for internal testing.

Run it locally

Uses pixi. From the repo root:

pixi run app     # http://localhost:8501
pixi run test    # unit tests (HTTP is mocked; no API calls)

Testers sign in with their own WattTime API account. The app itself needs no WattTime credentials.

Secrets

The app reads three optional values with st.secrets. Locally they go in .streamlit/secrets.toml (git-ignored); on Community Cloud, in the app's Settings -> Secrets. Same TOML in both places:

UPGRADE_REQUEST_URL = "https://script.google.com/macros/s/<deployment-id>/exec"
UPGRADE_REQUEST_KEY = "<shared key>"
UPGRADE_REQUEST_EMAIL = "<address that receives upgrade requests>"
  • UPGRADE_REQUEST_URL + UPGRADE_REQUEST_KEY: a Google Apps Script web app (scripts/upgrade_request.gs, setup steps at its top; the key must match its SHARED_KEY property). The "Request permissions upgrade" button posts the signed-in username to it, and it emails the request. The same script records usage rows (below).
  • UPGRADE_REQUEST_EMAIL: fallback. If the URL and key aren't set, or a send fails, the button opens a prefilled email to this address instead.
  • With none of the three set, the button is shown disabled.

Refreshing the map outlines and time zones

Do this when WattTime publishes a new map (new regions, or a signal covering more regions). It needs an ANALYST or PRO account, because /v3/maps is restricted.

export WATTTIME_USER=...  WATTTIME_PASSWORD=...
pixi run fetch-maps            # all signals in the account's my-access

This downloads the full-resolution outlines to data/maps/ (git-ignored, ~34 MB), writes simplified copies to data/boundaries/ (~4 MB, the files the app draws), and then checks that every mapped region has a row in data/region_timezone.csv.

  1. If it lists missing regions, add a row for each to data/region_timezone.csv: region code and IANA time zone name (e.g. Europe/Paris). The other two columns count the zones the region spans; use 1,1 for a single-zone region. Update data/region_timezone.SOURCE.txt if you re-copy the table from watttime-analysis-util.
  2. Re-run pixi run fetch-maps --skip-fetch to rebuild from the downloaded files and repeat the check without calling the API again.
  3. Commit data/boundaries/ (and the time zone table if changed) and push to main; Community Cloud redeploys automatically.

The simplified outlines are for drawing only. The region for a dropped pin comes from /v3/region-from-loc, which uses the API's full-resolution shapes.

Deploying on Streamlit Community Cloud

  • Repository WattTime/api-frontend, branch main, main file app.py.
  • Python 3.13 (chosen under Advanced settings at deploy time).
  • Paste the secrets above under Advanced settings -> Secrets, or later under Settings -> Secrets.
  • Community Cloud installs from requirements.txt, not pixi.toml. When you change a dependency, update both.
  • Each push to main redeploys.

Usage log

Each step a session reaches (landing, sign-in, pin dropped, data downloaded, CSV downloaded, upgrade requested, ...) is recorded once per session: as a line in the app log (under Manage app), and as a row in the "usage" tab of the Google Sheet named by the Apps Script's SHEET_ID property. A row has the UTC time, a random session id, the username (blank before sign-in), the step, seconds since the session started, the selected signal and region, and the host (local runs show localhost). Steps are listed in usage_log.py's callers (grep usage_log.step).

Code map

File What it does
app.py, routes.py Entry point and page routing
auth.py, identity_pages.py Sign-in state and the sign-in / register / reset screens (simulated account screens; replace both for a real single sign-on)
main_page.py The map, dates, download and preview steps
wt_api.py WattTime API client: raw requests, one token per browser session
access.py Reads /v3/my-access
geo.py, map_view.py Region outlines and the map
carpet.py Unit conversion, time averaging, the heatmap and the CSV
upgrade.py, scripts/upgrade_request.gs Permissions-upgrade request
boundary_build.py, scripts/fetch_maps.py Map outline refresh
usage_log.py Usage rows for usability tests
app_secrets.py Reads optional secrets

About

a proof of concept front end to register, see access, then preview and download data from the API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages