Databricks Applications — Python backends
First, confirm this skill is the right one. The default for new Databricks Apps is databricks-apps (AppKit — Node.js + TypeScript + React SDK). Load that skill first unless the user explicitly asks for a Python backend, is extending an existing Python app, or the team is Python-only. Everything below is the Python-backend alternative.
Critical Rules for Python apps (always follow)
- MUST confirm framework choice or use Python Framework Selection below
- MUST use SDK
Config() for authentication (never hardcode tokens)
- MUST use
app.yaml valueFrom for resources (never hardcode resource IDs)
- MUST use
dash-bootstrap-components for Dash app layout and styling
- MUST use
@st.cache_resource for Streamlit database connections
- MUST deploy Flask with Gunicorn, FastAPI with uvicorn (not dev servers)
Required Steps for Python apps
Copy this checklist and verify each item:
- [ ] Framework selected
- [ ] Auth strategy decided: app auth, user auth, or both
- [ ] App resources identified (SQL warehouse, Lakebase, serving endpoint, etc.)
- [ ] Backend data strategy decided (SQL warehouse, Lakebase, or SDK)
- [ ] Deployment method: CLI or DABs
Python Framework Selection
| Framework |
Best For |
app.yaml Command |
| FastAPI (default) |
Any Python backend by default — async APIs, auto-generated OpenAPI docs, JSON-serving apps |
["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] |
| Flask |
Custom REST APIs, lightweight apps, webhooks |
["gunicorn", "app:app", "-w", "4", "-b", "0.0.0.0:8000"] |
| Dash |
Production dashboards, BI tools, complex interactivity |
["python", "app.py"] |
| Streamlit |
Rapid prototyping, data science apps, internal tools where the UI is a series of Python widgets |
["streamlit", "run", "app.py"] |
| Gradio |
ML demos, model interfaces, chat UIs |
["python", "app.py"] |
| Reflex |
Full-stack Python apps without JavaScript |
["reflex", "run", "--env", "prod"] |
Default: FastAPI. Reach for FastAPI unless the user explicitly asks for Streamlit-style widget prototyping (Streamlit), a heavy dashboard grid (Dash), or a Gradio-style ML demo. FastAPI pairs naturally with a JS/HTML frontend or a JSON-consuming caller — the same posture databricks-apps uses on the Node side.
Quick Reference
| Concept |
Details |
| Runtime |
Python 3.11, Ubuntu 22.04, 2 vCPU, 6 GB RAM |
| Pre-installed |
Dash 2.18.1, Streamlit 1.38.0, Gradio 4.44.0, Flask 3.0.3, FastAPI 0.115.0 |
| Auth (app) |
Service principal via Config() — auto-injected DATABRICKS_CLIENT_ID/DATABRICKS_CLIENT_SECRET |
| Auth (user) |
x-forwarded-access-token header — see references/1-authorization.md |
| Resources |
valueFrom in app.yaml — see references/2-app-resources.md |
| SDK / Foundation Models / Vector Search / Model Serving |
Use the databricks-python-sdk skill — same WorkspaceClient and OpenAI-compatible foundation-model patterns work inside a Databricks App |
| Docs |
https://docs.databricks.com/dev-tools/databricks-apps/ |
Detailed Guides
Authorization: Use references/1-authorization.md when configuring app or user authorization — covers service principal auth, on-behalf-of user tokens, OAuth scopes, and per-framework code examples. (Keywords: OAuth, service principal, user auth, on-behalf-of, access token, scopes)
App resources: Use references/2-app-resources.md when connecting your app to Databricks resources — covers SQL warehouses, Lakebase, model serving, secrets, volumes, and the valueFrom pattern. (Keywords: resources, valueFrom, SQL warehouse, model serving, secrets, volumes, connections)
Frameworks: See references/3-frameworks.md for Databricks-specific patterns per framework — FastAPI (default), Flask, Dash, Streamlit, Gradio, Reflex — with auth integration and deployment commands. (Keywords: FastAPI, Flask, Dash, Streamlit, Gradio, Reflex, framework selection)
Deployment: Use references/4-deployment.md when deploying your app — covers Databricks CLI, Asset Bundles (DABs), app.yaml configuration, and post-deployment verification. (Keywords: deploy, CLI, DABs, asset bundles, app.yaml, logs)
Lakebase: Use references/5-lakebase.md when using Lakebase (PostgreSQL) as your app's data layer — covers auto-injected env vars, psycopg2/asyncpg patterns, and when to choose Lakebase vs SQL warehouse. (Keywords: Lakebase, PostgreSQL, psycopg2, asyncpg, transactional, PGHOST)
CLI commands: Use references/6-cli-approach.md for managing app lifecycle via CLI — covers creating, deploying, monitoring, and deleting apps. (Keywords: CLI, create app, deploy app, app logs)
Foundation Models / SDK / Vector Search / Model Serving: Use the databricks-python-sdk skill for the OpenAI-compatible foundation-model client, WorkspaceClient calls, Vector Search, and model-serving invocation — the same patterns apply inside a Databricks App. The examples in this skill's examples/ folder (fm-minimal-chat.py, fm-parallel-calls.py, fm-structured-outputs.py, llm_config.py) show the App-side wiring only.
Workflow
Determine the task type:
New app from scratch? → Load databricks-apps first (AppKit / Node). Only stay in this skill if the user explicitly asks for a Python backend.
Python-backend confirmed? → Python Framework Selection — default to FastAPI.
Setting up authorization? → Read references/1-authorization.md
Connecting to data/resources? → Read references/2-app-resources.md
Using Lakebase (PostgreSQL)? → Read references/5-lakebase.md
Deploying to Databricks? → Read references/4-deployment.md
Using CLI for app lifecycle? → Read references/6-cli-approach.md
Calling foundation model / LLM APIs, Vector Search, or model-serving endpoints? → Load the databricks-python-sdk skill. This skill's examples/ folder shows only the App-side wiring on top of those SDK patterns.
Follow the instructions in the relevant guide.
Core Architecture
All Python Databricks apps follow this pattern:
app-directory/
├── app.py # Main application (or framework-specific name)
├── models.py # Pydantic data models
├── backend.py # Data access layer
├── requirements.txt # Additional Python dependencies
├── app.yaml # Databricks Apps configuration
└── README.md
Backend Toggle Pattern
import os
from databricks.sdk.core import Config
USE_MOCK = os.getenv("USE_MOCK_BACKEND", "true").lower() == "true"
if USE_MOCK:
from backend_mock import MockBackend as Backend
else:
from backend_real import RealBackend as Backend
backend = Backend()
SQL Warehouse Connection (shared across all frameworks)
from databricks.sdk.core import Config
from databricks import sql
cfg = Config() # Auto-detects credentials from environment
conn = sql.connect(
server_hostname=cfg.host,
http_path=f"/sql/1.0/warehouses/{os.getenv('DATABRICKS_WAREHOUSE_ID')}",
credentials_provider=lambda: cfg.authenticate,
)
Pydantic Models
from pydantic import BaseModel, Field
from datetime import datetime
from enum import Enum
class Status(str, Enum):
ACTIVE = "active"
PENDING = "pending"
class EntityOut(BaseModel):
id: str
name: str
status: Status
created_at: datetime
class EntityIn(BaseModel):
name: str = Field(..., min_length=1)
status: Status = Status.PENDING
Common Issues
| Issue |
Solution |
| Connection exhausted |
Use @st.cache_resource (Streamlit) or connection pooling |
| Auth token not found |
Check x-forwarded-access-token header — only available when deployed, not locally |
| App won't start |
Check app.yaml command matches framework; check databricks apps logs <name> |
| Resource not accessible |
Add resource via UI, verify SP has permissions, use valueFrom in app.yaml |
| Import error on deploy |
Add missing packages to requirements.txt (pre-installed packages don't need listing) |
| Lakebase app crashes on start |
psycopg2/asyncpg are NOT pre-installed — MUST add to requirements.txt |
| Port conflict |
Apps must bind to DATABRICKS_APP_PORT env var (defaults to 8000). Never use 8080. Streamlit is auto-configured; for others, read the env var in code or use 8000 in app.yaml command |
| Streamlit: set_page_config error |
st.set_page_config() must be the first Streamlit command |
| Dash: unstyled layout |
Add dash-bootstrap-components; use dbc.themes.BOOTSTRAP |
| Slow queries |
Use Lakebase for transactional/low-latency; SQL warehouse for analytical queries |
Platform Constraints
| Constraint |
Details |
| Runtime |
Python 3.11, Ubuntu 22.04 LTS |
| Compute |
2 vCPUs, 6 GB memory (default) |
| Pre-installed frameworks |
Dash, Streamlit, Gradio, Flask, FastAPI, Shiny |
| Custom packages |
Add to requirements.txt in app root |
| Network |
Apps can reach Databricks APIs; external access depends on workspace config |
| User auth |
Public Preview — workspace admin must enable before adding scopes |
Official Documentation
Related Skills
- databricks-apps — the default for new Databricks Apps (AppKit / Node / TypeScript + React); load it first unless a Python backend is explicitly required
- databricks-python-sdk —
WorkspaceClient, OpenAI-compatible foundation-model client, Vector Search, model-serving invocation; the same patterns work inside a Databricks App
- databricks-lakebase — persistent PostgreSQL state (autoscaling managed PG with branching)
- databricks-model-serving — endpoint lifecycle for ML models an App calls
- databricks-dabs — deploying apps via DABs
1---
2name: databricks-apps-python
3description: "Python backend for Databricks Apps — FastAPI (default), Flask, Dash, Streamlit, Gradio, Reflex. **Default for a new Databricks App is `databricks-apps` (AppKit — Node/TypeScript/React) — reach for it first.** Use this skill only when the user asks for a Python backend, extends an existing Python app, or the team is Python-only. Covers OAuth auth, app resources, SQL warehouse and Lakebase connectivity, foundation-model / Vector Search / model-serving APIs (via `databricks-python-sdk`), and deployment via CLI or DABs."
4compatibility: Requires databricks CLI (>= v1.0.0)
5metadata:
6 version: "0.1.0"
7parent: databricks-core
8---
9
10# Databricks Applications — Python backends
11
12> **First, confirm this skill is the right one.** The default for new Databricks Apps is **[databricks-apps](../databricks-apps/SKILL.md)** (AppKit — Node.js + TypeScript + React SDK). Load that skill first unless the user explicitly asks for a Python backend, is extending an existing Python app, or the team is Python-only. Everything below is the Python-backend alternative.
13
14## Critical Rules for Python apps (always follow)
15
16- **MUST** confirm framework choice or use [Python Framework Selection](#python-framework-selection) below
17- **MUST** use SDK `Config()` for authentication (never hardcode tokens)
18- **MUST** use `app.yaml` `valueFrom` for resources (never hardcode resource IDs)
19- **MUST** use `dash-bootstrap-components` for Dash app layout and styling
20- **MUST** use `@st.cache_resource` for Streamlit database connections
21- **MUST** deploy Flask with Gunicorn, FastAPI with uvicorn (not dev servers)
22
23## Required Steps for Python apps
24
25Copy this checklist and verify each item:
26```
27- [ ] Framework selected
28- [ ] Auth strategy decided: app auth, user auth, or both
29- [ ] App resources identified (SQL warehouse, Lakebase, serving endpoint, etc.)
30- [ ] Backend data strategy decided (SQL warehouse, Lakebase, or SDK)
31- [ ] Deployment method: CLI or DABs
32```
33
34---
35
36## Python Framework Selection
37
38| Framework | Best For | app.yaml Command |
39|-----------|----------|------------------|
40| **FastAPI** (default) | Any Python backend by default — async APIs, auto-generated OpenAPI docs, JSON-serving apps | `["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]` |
41| **Flask** | Custom REST APIs, lightweight apps, webhooks | `["gunicorn", "app:app", "-w", "4", "-b", "0.0.0.0:8000"]` |
42| **Dash** | Production dashboards, BI tools, complex interactivity | `["python", "app.py"]` |
43| **Streamlit** | Rapid prototyping, data science apps, internal tools where the UI is a series of Python widgets | `["streamlit", "run", "app.py"]` |
44| **Gradio** | ML demos, model interfaces, chat UIs | `["python", "app.py"]` |
45| **Reflex** | Full-stack Python apps without JavaScript | `["reflex", "run", "--env", "prod"]` |
46
47**Default: FastAPI.** Reach for FastAPI unless the user explicitly asks for Streamlit-style widget prototyping (Streamlit), a heavy dashboard grid (Dash), or a Gradio-style ML demo. FastAPI pairs naturally with a JS/HTML frontend or a JSON-consuming caller — the same posture `databricks-apps` uses on the Node side.
48
49---
50
51## Quick Reference
52
53| Concept | Details |
54|---------|---------|
55| **Runtime** | Python 3.11, Ubuntu 22.04, 2 vCPU, 6 GB RAM |
56| **Pre-installed** | Dash 2.18.1, Streamlit 1.38.0, Gradio 4.44.0, Flask 3.0.3, FastAPI 0.115.0 |
57| **Auth (app)** | Service principal via `Config()` — auto-injected `DATABRICKS_CLIENT_ID`/`DATABRICKS_CLIENT_SECRET` |
58| **Auth (user)** | `x-forwarded-access-token` header — see [references/1-authorization.md](references/1-authorization.md) |
59| **Resources** | `valueFrom` in app.yaml — see [references/2-app-resources.md](references/2-app-resources.md) |
60| **SDK / Foundation Models / Vector Search / Model Serving** | Use the `databricks-python-sdk` skill — same `WorkspaceClient` and OpenAI-compatible foundation-model patterns work inside a Databricks App |
61| **Docs** | https://docs.databricks.com/dev-tools/databricks-apps/ |
62
63---
64
65## Detailed Guides
66
67**Authorization**: Use [references/1-authorization.md](references/1-authorization.md) when configuring app or user authorization — covers service principal auth, on-behalf-of user tokens, OAuth scopes, and per-framework code examples. (Keywords: OAuth, service principal, user auth, on-behalf-of, access token, scopes)
68
69**App resources**: Use [references/2-app-resources.md](references/2-app-resources.md) when connecting your app to Databricks resources — covers SQL warehouses, Lakebase, model serving, secrets, volumes, and the `valueFrom` pattern. (Keywords: resources, valueFrom, SQL warehouse, model serving, secrets, volumes, connections)
70
71**Frameworks**: See [references/3-frameworks.md](references/3-frameworks.md) for Databricks-specific patterns per framework — FastAPI (default), Flask, Dash, Streamlit, Gradio, Reflex — with auth integration and deployment commands. (Keywords: FastAPI, Flask, Dash, Streamlit, Gradio, Reflex, framework selection)
72
73**Deployment**: Use [references/4-deployment.md](references/4-deployment.md) when deploying your app — covers Databricks CLI, Asset Bundles (DABs), app.yaml configuration, and post-deployment verification. (Keywords: deploy, CLI, DABs, asset bundles, app.yaml, logs)
74
75**Lakebase**: Use [references/5-lakebase.md](references/5-lakebase.md) when using Lakebase (PostgreSQL) as your app's data layer — covers auto-injected env vars, psycopg2/asyncpg patterns, and when to choose Lakebase vs SQL warehouse. (Keywords: Lakebase, PostgreSQL, psycopg2, asyncpg, transactional, PGHOST)
76
77**CLI commands**: Use [references/6-cli-approach.md](references/6-cli-approach.md) for managing app lifecycle via CLI — covers creating, deploying, monitoring, and deleting apps. (Keywords: CLI, create app, deploy app, app logs)
78
79**Foundation Models / SDK / Vector Search / Model Serving**: Use the **[databricks-python-sdk](../databricks-python-sdk/SKILL.md)** skill for the OpenAI-compatible foundation-model client, `WorkspaceClient` calls, Vector Search, and model-serving invocation — the same patterns apply inside a Databricks App. The examples in this skill's `examples/` folder (`fm-minimal-chat.py`, `fm-parallel-calls.py`, `fm-structured-outputs.py`, `llm_config.py`) show the App-side wiring only.
80
81---
82
83## Workflow
84
851. Determine the task type:
86
87 **New app from scratch?** → Load **[databricks-apps](../databricks-apps/SKILL.md)** first (AppKit / Node). Only stay in this skill if the user explicitly asks for a Python backend.
88 **Python-backend confirmed?** → [Python Framework Selection](#python-framework-selection) — default to FastAPI.
89 **Setting up authorization?** → Read [references/1-authorization.md](references/1-authorization.md)
90 **Connecting to data/resources?** → Read [references/2-app-resources.md](references/2-app-resources.md)
91 **Using Lakebase (PostgreSQL)?** → Read [references/5-lakebase.md](references/5-lakebase.md)
92 **Deploying to Databricks?** → Read [references/4-deployment.md](references/4-deployment.md)
93 **Using CLI for app lifecycle?** → Read [references/6-cli-approach.md](references/6-cli-approach.md)
94 **Calling foundation model / LLM APIs, Vector Search, or model-serving endpoints?** → Load the **[databricks-python-sdk](../databricks-python-sdk/SKILL.md)** skill. This skill's `examples/` folder shows only the App-side wiring on top of those SDK patterns.
95
962. Follow the instructions in the relevant guide.
97
98---
99
100## Core Architecture
101
102All Python Databricks apps follow this pattern:
103
104```
105app-directory/
106├── app.py # Main application (or framework-specific name)
107├── models.py # Pydantic data models
108├── backend.py # Data access layer
109├── requirements.txt # Additional Python dependencies
110├── app.yaml # Databricks Apps configuration
111└── README.md
112```
113
114### Backend Toggle Pattern
115
116```python
117import os
118from databricks.sdk.core import Config
119
120USE_MOCK = os.getenv("USE_MOCK_BACKEND", "true").lower() == "true"
121
122if USE_MOCK:
123 from backend_mock import MockBackend as Backend
124else:
125 from backend_real import RealBackend as Backend
126
127backend = Backend()
128```
129
130### SQL Warehouse Connection (shared across all frameworks)
131
132```python
133from databricks.sdk.core import Config
134from databricks import sql
135
136cfg = Config() # Auto-detects credentials from environment
137conn = sql.connect(
138 server_hostname=cfg.host,
139 http_path=f"/sql/1.0/warehouses/{os.getenv('DATABRICKS_WAREHOUSE_ID')}",
140 credentials_provider=lambda: cfg.authenticate,
141)
142```
143
144### Pydantic Models
145
146```python
147from pydantic import BaseModel, Field
148from datetime import datetime
149from enum import Enum
150
151class Status(str, Enum):
152 ACTIVE = "active"
153 PENDING = "pending"
154
155class EntityOut(BaseModel):
156 id: str
157 name: str
158 status: Status
159 created_at: datetime
160
161class EntityIn(BaseModel):
162 name: str = Field(..., min_length=1)
163 status: Status = Status.PENDING
164```
165
166---
167
168## Common Issues
169
170| Issue | Solution |
171|-------|----------|
172| **Connection exhausted** | Use `@st.cache_resource` (Streamlit) or connection pooling |
173| **Auth token not found** | Check `x-forwarded-access-token` header — only available when deployed, not locally |
174| **App won't start** | Check `app.yaml` command matches framework; check `databricks apps logs <name>` |
175| **Resource not accessible** | Add resource via UI, verify SP has permissions, use `valueFrom` in app.yaml |
176| **Import error on deploy** | Add missing packages to `requirements.txt` (pre-installed packages don't need listing) |
177| **Lakebase app crashes on start** | `psycopg2`/`asyncpg` are NOT pre-installed — MUST add to `requirements.txt` |
178| **Port conflict** | Apps must bind to `DATABRICKS_APP_PORT` env var (defaults to 8000). Never use 8080. Streamlit is auto-configured; for others, read the env var in code or use 8000 in app.yaml command |
179| **Streamlit: set_page_config error** | `st.set_page_config()` must be the first Streamlit command |
180| **Dash: unstyled layout** | Add `dash-bootstrap-components`; use `dbc.themes.BOOTSTRAP` |
181| **Slow queries** | Use Lakebase for transactional/low-latency; SQL warehouse for analytical queries |
182
183---
184
185## Platform Constraints
186
187| Constraint | Details |
188|------------|---------|
189| **Runtime** | Python 3.11, Ubuntu 22.04 LTS |
190| **Compute** | 2 vCPUs, 6 GB memory (default) |
191| **Pre-installed frameworks** | Dash, Streamlit, Gradio, Flask, FastAPI, Shiny |
192| **Custom packages** | Add to `requirements.txt` in app root |
193| **Network** | Apps can reach Databricks APIs; external access depends on workspace config |
194| **User auth** | Public Preview — workspace admin must enable before adding scopes |
195
196---
197
198## Official Documentation
199
200- **[Databricks Apps Overview](https://docs.databricks.com/dev-tools/databricks-apps/)** — main docs hub
201- **[Authorization](https://docs.databricks.com/dev-tools/databricks-apps/auth)** — app auth and user auth
202- **[Resources](https://docs.databricks.com/dev-tools/databricks-apps/resources)** — SQL warehouse, Lakebase, serving, secrets
203- **[app.yaml Reference](https://docs.databricks.com/dev-tools/databricks-apps/app-runtime)** — command and env config
204- **[System Environment](https://docs.databricks.com/dev-tools/databricks-apps/system-env)** — pre-installed packages, runtime details
205
206## Related Skills
207
208- **[databricks-apps](../databricks-apps/SKILL.md)** — the default for new Databricks Apps (AppKit / Node / TypeScript + React); load it first unless a Python backend is explicitly required
209- **[databricks-python-sdk](../databricks-python-sdk/SKILL.md)** — `WorkspaceClient`, OpenAI-compatible foundation-model client, Vector Search, model-serving invocation; the same patterns work inside a Databricks App
210- **[databricks-lakebase](../databricks-lakebase/SKILL.md)** — persistent PostgreSQL state (autoscaling managed PG with branching)
211- **[databricks-model-serving](../databricks-model-serving/SKILL.md)** — endpoint lifecycle for ML models an App calls
212- **databricks-dabs** — deploying apps via DABs