Skip to content

Python, FastAPI, Streamlit

BI Components run in the browser, so a Python application has no JavaScript tooling to add: its pages load the files by their address, and Python answers the queries. Both server-side setups work:

Setup In Python you write Example
2 · Your backend One endpoint that answers batches of queries A dashboard on a FastAPI backend
3 · Sandbox The same endpoint, and a page that calls embedDashboard The playground, on FastAPI or Streamlit

Four examples, one engine

All four run on the same DuckDB engine, with the same checks on the server:

What it is Source
A dashboard on a FastAPI backend Setup 2. No frame: the components of a Markdown page of this site call POST api/queries through <source-api>. fastapi-direct.md
The playground, served by FastAPI Setup 3. A chat product: a static page and two endpoints, one for the configuration of the page, one for each batch of queries. The usual shape of a web application. servers/fastapi
The same playground, served by Streamlit Setup 3. The same interface as one full-page Streamlit component. No native widget is visible. servers/streamlit
A dashboard in a Streamlit app Setup 3. Native widgets (a select box for the viewer, a toggle, tabs) and one component that mounts the sandboxed dashboard. Start here to add dashboards to an existing Streamlit app. streamlit-basic

The first two share one server: the endpoint that answers the frames of the playground also answers the page without a frame. The two playground servers are interchangeable: one interface, one core, and a test checking that they give the same answers. You only need one.

What they share

  • A dashboard written by someone else never goes into the page itself. It only lives in the <iframe sandbox="allow-scripts"> created by embedDashboard. In fastapi-direct the dashboard is the page, written by its developer.
  • No JavaScript tooling in your application: the page imports bi-components-host.js by its address, and its frames load the bi-components.js next to it.
  • Queries travel in batches. The page gathers the queries of a dashboard during a fixed 10 ms window and sends them together.
  • Checked on the server: Python validates every query (the names it knows, no extra field), applies row-level security that fails closed, and only uses query parameters for values. See What your server must check.

The engine, semantic_engine.py, has no web framework in it: it is the part to read, and to adapt to your own data.

FastAPI

A batch is one POST, and its answers are the response. Nothing is kept between two requests.

class Batch(BaseModel):
    queries: dict[str, Any]

@app.post("/api/queries")
def queries(batch: Batch, user: User = Depends(signed_in_user)) -> dict:
    answers = {}
    for id, query in batch.queries.items():
        try:
            answers[id] = engine.query(query, row_level_filters={"country": user.countries})
        except (ValueError, PermissionError) as error:
            answers[id] = {"error": str(error)}
    return {"answers": answers}

This is the protocol of <source-api>, so a page of your own can call it directly. For a sandbox, resolveQuery gathers the queries of the frame and posts them to the same endpoint: see send in playground_ui.js. A complete small server is in the backend guide.

Streamlit

A dashboard is one custom component (Custom Components v2) whose JavaScript calls embedDashboard. Queries cannot be posted one by one:

A component value is not a message queue

A Streamlit component has a single value, overwritten by each call. A dashboard sends several queries in the same millisecond: sent one by one, only the last would reach Python, and the other components would wait until their timeout.

The pattern that works: resolveQuery gathers the queries during a fixed 10 ms window, then sends every query still waiting for an answer as one value. Python answers those it has not answered yet, and one rerun delivers them. Because each value holds all the waiting queries, nothing is lost when Streamlit abandons a run to start a newer one.

Run them

Each example has a README with its commands: the playground (FastAPI or Streamlit, Docker, deployment on Cloud Run) and streamlit-basic.