Skip to content
Open
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
66 changes: 66 additions & 0 deletions streamlit-postgres-app/README.md
Original file line number Diff line number Diff line change
@@ -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