The problem
I've worked with Streamlit on a number of projects, and I've found that once an app gets real users, the same problems come up. Every reader's reruns are Python on your server, so load grows with readers. The frontend and backend are coupled: the view only runs inside Streamlit, so a frontend team can't take the project over or embed it in another app.
The usual exit is a rewrite to FastAPI and React.
The cause is that a Streamlit app is one Python process holding your session, so the UI, the state and the compute are the same object.
What n6k does
n6k is a Python app server that puts a data protocol between the data and the UI. You still write Python, top to bottom, and get a live data app. The one change: data.py publishes tables, and page.py queries them by name.
That split is where each team can take over. A backend team can replace data.py with n6kd, a Go server that serves tables from a SQL file, including straight from Snowflake, without touching the page. A frontend team can rewrite page.py in TypeScript with the same component library, or any other way they want, without touching the data.
An example
A small sales dashboard: a title, a category filter, two KPIs and a revenue-by-month chart.
data.py:
import pandas as pd
from n6k_app import Database
db = Database()
@db.data()
def orders() -> pd.DataFrame:
return pd.read_parquet("orders.parquet")
@orders.derive()
def revenue_by_month(df: pd.DataFrame) -> pd.DataFrame:
return (
df.assign(month=df.order_date.dt.strftime("%Y-%m"))
.groupby(["month", "category"], as_index=False)
.agg(revenue=("amount", "sum"), orders=("amount", "count"))
)
page.py:
from n6k_app.frontend import Components, page
from .data import revenue_by_month
@page
def sales(c: Components) -> None:
c.h1("Sales")
category = c.Select(key="category", label="Category",
options=["Hardware", "Software", "Services"], empty="All")
c.Metric(label="Revenue", data=revenue_by_month, value="sum(revenue)",
valueFormat="$,.0f", filters={"category": category})
c.Metric(label="Orders", data=revenue_by_month, value="sum(orders)",
filters={"category": category})
c.LineChart(data=revenue_by_month, x="month", y="sum(revenue)",
filters={"category": category})
orders and revenue_by_month are computed once per server process. Moving the filter re-runs sales(), which only lays out components, so your pandas code doesn't run again. Each component names a table and a filter, and the browser queries that table for exactly what the component shows.
While the app runs, you can query the same tables the page shows with SQL, from a notebook or a terminal (the client is a DuckDB extension):
ATTACH 'n6k://localhost:7823' AS db (TYPE n6k);
SELECT * FROM db.revenue_by_month;
No Python in the page
The same page can be written as page.mdx with no Python in it. Python only runs in data.py:
# Sales
::Select[category]{label="Category" options="Hardware,Software,Services" empty="All"}
<Metric label="Revenue" data="db.revenue_by_month" value="sum(revenue)" valueFormat="$,.0f" filters={{ category }} />
<Metric label="Orders" data="db.revenue_by_month" value="sum(orders)" filters={{ category }} />
<LineChart data="db.revenue_by_month" x="month" y="sum(revenue)" filters={{ category }} />
The components are a React library, so the chart can live in your own React app pointed at the same server.
Try it
uvx n6k init myapp --ref streamlit-v2
cd myapp
uv run n6k dev
Open http://localhost:7823 for the page.py version, and http://localhost:7823/mdx for the page.mdx version.
I think I've built something with the ease of creating a Streamlit app and a path forward to production code that can integrate with your existing apps or websites.
- Side by side with the Streamlit version
-
Running Streamlit in production (reruns, sticky sessions, row permissions as
ifstatements)
Top comments (0)