Embed in your app¶
BI Components are native JavaScript (ES modules, no build step), so they fit in anything that renders HTML. How you embed them depends on one question: who wrote the dashboard?
| The dashboard is written by | Embed it as | Queries run |
|---|---|---|
| You, or your team | Tags in your own page | In the browser (DuckDB-WASM), or in your own connector |
| A language model, or your users | A sandboxed frame driven by a Host | In your backend, with the viewer's rights |
In your own pages¶
Put the tags in the page and load the scripts, as in Get started. This works in:
- plain HTML pages and web apps;
- frameworks such as React, Vue or Svelte (the tags are standard custom elements);
- static site generators such as mkdocs (this site), Docusaurus, Hugo or Jekyll.
Custom scripts and the built-in components share one interface, window.dashboard, to query data and to read or set filters.
Dashboards written by someone else: the sandboxed frame¶
A dashboard generated by a model or saved by another user is untrusted code. It goes into a frame that has no network access and cannot read your page; your page (the Host) answers its queries.
import { HostBridge, bundleModules, buildSrcdoc, restrictFilters } from 'https://unytics.io/bi-components/src/connectors/host-bridge.js';
const SRC = 'https://unytics.io/bi-components/src';
const iframe = document.querySelector('iframe'); // <iframe sandbox="allow-scripts" referrerpolicy="no-referrer">
// 1. What the dashboard may ask for, and who answers
const bridge = new HostBridge({
iframe,
dictionary: { metrics: ['revenue', 'orders'], dimensions: ['country', 'channel'] },
enforce: (spec) => ({
...spec,
filters: restrictFilters(spec.filters, { country: user.countries }), // the viewer's rights
}),
resolveQuery: (spec) => // -> { rows, meta }
fetch('/api/query', { method: 'POST', body: JSON.stringify(spec) }).then((response) => response.json()),
});
// 2. The frame: the components, then the untrusted HTML
const bundle = await bundleModules([
`${SRC}/connectors/iframe-bridge.js`,
`${SRC}/components/echarts.js`,
`${SRC}/components/score_cards.js`,
`${SRC}/components/datatable.js`,
`${SRC}/components/filters.js`,
]);
iframe.srcdoc = buildSrcdoc({ body: dashboardHtml, ...bundle, lang: 'en' });
// 3. Later: theme, filters or period, without reloading the frame
bridge.setState({ theme: { mode: 'dark' } });
What the Host does for you:
- Validates every query: only names of your dictionary, bounded limits, plain values in filters. Anything else is refused and shown in the component that asked.
- Applies your rules last, in
enforce: row-level security withrestrictFilters, which narrows what was asked and refuses when nothing is allowed. - Keeps the frame closed: content security policy, no network, no access to the parent page.
Your backend should still check what it receives, as any API does: the Host runs in the viewer's browser.
Go further:
- Semantic playground: a Host in a static page, with a hostile dashboard to try and a live view of the messages.
- Architecture and security: the protocol, the options of
HostBridge, the security model. - Generative AI: the system prompt that makes a model write these dashboards.
Python: FastAPI or Streamlit¶
Three examples, all on the same DuckDB engine and the same server-side checks:
| What it is | Source | |
|---|---|---|
| The playground, served by FastAPI | A chat product. A static page, and two endpoints: one gives the page its configuration, one answers each batch of queries. The usual shape of a web application. | servers/fastapi |
| The same playground, served by Streamlit | The same interface as one full-page Streamlit component. No native widget is visible. | servers/streamlit |
| A dashboard in a Streamlit app | 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 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:
- The dashboard never goes into the page itself. The page mounts
<iframe sandbox="allow-scripts">, driven byHostBridgeandbuildSrcdoc. - No build step, no copy of
src/: a small Python bundler reads the ES modules fromsrc/and inlines them, with ECharts cached on first run. - Queries travel in batches. The page gathers the queries of a dashboard during a fixed 10 ms window and sends them together.
- Checked again on the server: Python validates every query again (white lists, no extra field), applies row-level security that fails closed, and only uses query parameters for values.
Where they differ is the transport. With FastAPI, a batch is one POST and its answers are the response. With Streamlit (Custom Components v2), a component has a single value, not a message queue: the batch leaves as that value, holding every query still waiting, and one rerun delivers the answers.
Another engine¶
A connector is a small script that installs window.dashboard with a function able to answer a query. To run the components on something else than DuckDB-WASM or a Host (a REST API, a warehouse proxy), see createDashboard and the two existing connectors.