diff --git a/streamlit-postgres-app/README.md b/streamlit-postgres-app/README.md new file mode 100644 index 00000000..767d0165 --- /dev/null +++ b/streamlit-postgres-app/README.md @@ -0,0 +1,66 @@ +# Streamlit + Lakebase Postgres App + +A simple todo list template that demonstrates how to build a Streamlit application backed by an autoscaling Postgres database (Lakebase) hosted on Databricks Apps. Tasks are stored, listed, toggled complete, and deleted — all persisted in Postgres. + +## Architecture + +``` +Streamlit Frontend (Python) + ↓ psycopg connection pool +Lakebase Postgres (Databricks) + ↑ OAuth credentials auto-refreshed via Databricks SDK +Databricks Apps +``` + +The app authenticates to Postgres using short-lived OAuth tokens generated by the Databricks SDK. A custom `psycopg.Connection` subclass (`OAuthConnection`) refreshes the credential on every new connection, and a `ConnectionPool` (min 2, max 10) manages connections. Each user gets their own schema, named `{PGAPPNAME}_schema_{PGUSER}`, containing a `todos` table. + +## Setup + +Create and activate a virtual environment: +```bash +python -m venv .venv +source .venv/bin/activate +``` + +Install Python dependencies: +```bash +pip install -r requirements.txt +``` + +## Configuration + +The app reads its database connection settings from environment variables: + +- `PGENDPOINT` - Lakebase Postgres endpoint (injected via the `postgres` app resource) +- `PGDATABASE` - Database name +- `PGUSER` - Database user (also used to namespace the per-user schema) +- `PGHOST` - Database host +- `PGPORT` - Database port +- `PGSSLMODE` - SSL mode (defaults to `require`) +- `PGAPPNAME` - Application name (defaults to `my_app`, used to namespace the schema) + +When deployed on Databricks Apps, these are populated automatically from the bound Postgres resource. The password is not set directly — it is generated as an OAuth token at connection time. + +## Development + +Run the Streamlit app locally: +```bash +streamlit run app.py +``` + +This starts the app at http://localhost:8501. You will need the `PG*` environment variables above pointing at a reachable Lakebase Postgres instance and valid Databricks authentication configured for the SDK. + +## Databricks Apps Deployment + +Configured for Databricks Apps via `app.yaml`, which runs `streamlit run app.py` and binds the `postgres` resource to the `PGENDPOINT` environment variable. + +The `manifest.yaml` declares a Postgres resource spec with `CAN_CONNECT_AND_CREATE` permission so the app can create its own schema and table on first run. + +## Features + +- Add new todos +- View todos (most recent first) +- Toggle todos complete/incomplete (completed tasks are struck through) +- Delete todos +- Per-user schema isolation +- Automatic schema and table initialization on startup