Building a Data App
A
Data App is one self-contained HTML file under the repo's
.
lists it by
its
and runs it in a sandboxed frame where the SDK is already on the global
. The
SDK supplies data and state; every pixel is yours.
References, read when a step needs them:
- references/api.md: every SDK call, the result shape, the plugins, errors.
Read it before writing any code; do not invent APIs.
- references/plugins/: one file per built-in plugin beyond
(export, render,
lineage). Read one only when the agreed design uses that plugin,
and check before using it.
- references/downloads.md: the download button for exports and pictures.
Read it only when the agreed design lists a download.
- references/example.html: a complete app in the default look. Start a
new file from its skeleton, or from the closest app already in .
- references/DESIGN.md: the anfra app design guide (colors, type,
spacing, chart palette) behind the default look. Read it when you build in the default look.
- The SDK docs, for what these references don't cover: look it up with .
1. Learn the datasets
sh
anfra show # every dataset: models, fields (dimensions, measures), metrics
anfra show <dataset fqn> # one dataset
The fqn is what a query's
takes. A field is
(
), a
dataset metric its bare name. A measure is already aggregated: select it as it is, never wrap it
in
. Read labels and descriptions: they carry the modeler's caveats on grain, fan-out and
which date to use. For a metric's definition, read its
.
If a number the app needs has no field or metric, add it to the AML with the skill
rather than computing it in JavaScript.
2. Agree on the design
Restate the request as one result the user can check ("revenue for the last full week against
the week before, with a country picker"). Ask only what changes the numbers: what a business word
means, which date counts, what to exclude, each with your recommended answer.
Also ask which look the user wants, unless they already said: the
default anfra style
(recommended),
something creative (you choose the palette and layout), or
their own design
(a brand, colors or a reference to follow). See
Look and feel.
Then write the declaration list: every
(dataset, dimensions, measures),
,
,
and
, with real field names, how
each query renders (chart or table; default ECharts 6 from jsDelivr, as in the example), and which
blocks, if any, the reader can download from, one line each with its formats (
). List
a download only when the user asked for one, or propose one where it plainly helps (a detail
table's data) and let them decide; none is the default. Leave pictures (PNG, PDF) to the host,
which offers them for every block and the whole page, unless the user asks for them in the app.
Show it to the user and get a yes before writing the file. When the data cannot answer the
request, say so and offer the nearest thing it can.
2.1 Lineage
Lineage explains and governs a metric rather than showing its value: it answers what the metric is
built from, the dimensions, measures and other metrics it depends on, and the models and source
tables behind them. Add it when the user asks for it, or when the request is about tracing, understanding
or governing a metric: "how is revenue calculated", "which tables feed this KPI", a field catalog,
a definitions panel beside a chart. An app that only shows numbers needs none.
When it is in, add to the declaration list which fields the app traces, when (on load, or when the
reader picks a field or clicks a chart), and how the diagram is drawn (see
references/plugins/lineage.md).
Look and feel
An app must look finished without the user asking: native controls, default buttons and unstyled
text are not acceptable. What counts as finished depends on the look the user chose.
- Their own design: follow it, and skip the defaults below.
- Creative: pick your own palette, type and layout, and keep the basics: readable contrast,
tabular numerals, light and dark (), styled loading, empty and error
states.
- Default anfra style: start from references/example.html, which
applies the tokens in references/DESIGN.md to
Basecoat UI, a plain HTML/CSS component library. Use its tokens as
CSS variables, its three text sizes (20, 14, 12px), 32px controls, one violet primary action and
the chart palette (violet first). Ignore what only fits the anfra web app itself: Tailwind class
names, the sidebar, the sign-in sheet, the logo. Load Inter from the CDN with a
fallback.
In every look:
- A single HTML file uses Basecoat (pinned CDN CSS and JS, as in the example). Use shadcn
( for Vue) only when the app is written in React or Vue.
- Filters are Basecoat comboboxes, with
aria-multiselectable="true"
for several values. Never
a native . Basecoat has no date picker: use two styled
and preset buttons, as in the example.
- Basecoat is a flex column that clips overflow. A filter bar built as with only
display: flex; flex-wrap: wrap
keeps the column direction, so
pushes every control to the right edge, and the clipped overflow cuts off a
combobox's dropdown at the card's bottom edge. Override both on the bar: flex-direction: row; align-items: flex-end; overflow: visible
(as does in the example). Check
the filter bar in a browser with a dropdown open before handing over.
- Charts: if a skill is available, follow it. Give ECharts hex colors: it cannot parse
.
3. Write and check the queries
A query's
is AQL: write it with the
and
skills. A Data App adds
four rules, because the SDK does not run your AQL as written: it sends it with the reader's state
(filter values, the cross-filter selection, sorts, date drills, the page), which anfra applies to
the query's
before it runs.
- Write an , optionally after query-local and declarations.
Any AQL expression works inside it. A pipe-style query () runs, but fails
as soon as a reader touches a control, sort or date drill mapped to it.
- Alias every dimension and measure (
revenue: sum(orders.amount)
). The alias is the row key
in , and what and name.
- No limit. Paging is the SDK's (, ). A query without gets
every row, up to the server's maximum, past which it fails rather than truncating. The 10,000
rows stops at are that command's page, not a cap on apps.
- Nothing a reader changes goes in the AQL text. Static conditions go in or
; a reader's choice is a control ( + ), never a string built
into the query.
Run each query before it goes in the app, so a mistake surfaces here rather than in a frame:
sh
anfra query --dataset <fqn> '<aql>'
Check its headline number against a direct SQL query on the source, or a simple invariant (parts
summing to the total). Trace each lineage target the same way, and check its
path is the
one you expect:
sh
anfra lineage --targets '[{"parent":"<dataset fqn>","child":"<metric>"}]'
4. Write the file
One self-contained
file in
(subfolders group apps in the list), with a
the list shows.
opens at
. Follow the example's skeleton: declare
everything,
a render function,
, then wire controls and clicks to set state
and
again. Keep its two layers apart: the look (styles, markup, chart theme) and the
SDK wiring.
The frame is a
sandbox (
only, opaque origin) under a
content policy. Write
for it:
- Data comes only from the SDK. The frame has no network of its own: , XHR, WebSocket
and are blocked but to the CDNs below. Never call anfra's API or any other service
from the app.
- Libraries, styles and fonts only from cdn.jsdelivr.net, cdnjs.cloudflare.com and Google
Fonts (fonts.googleapis.com, fonts.gstatic.com), each by its URL with a pinned
version. A script, style or font from anywhere else is blocked, esm.sh and unpkg.com included:
load an npm package from jsDelivr (
/npm/<pkg>@<version>/<file>
), and an ES module from its
build (https://cdn.jsdelivr.net/npm/<pkg>@<version>/+esm
). Images may come
from any server. When the user needs another origin (their own CDN), add it to the
repo's under (, an origin with
nothing after it), and tell them anfra Cloud keeps its own list, which an Admin edits in the
Organization's settings.
- Every CDN and stylesheet carries its hash:
integrity="sha384-…" crossorigin="anonymous"
, as in the example, so a library changed at its CDN fails to load
rather than runs beside the reader's data. jsDelivr shows a file's hash on its page; otherwise
curl -s <url> | openssl dgst -sha384 -binary | openssl base64 -A
gives what follows .
Load fonts from on jsDelivr, as the example does, rather than Google Fonts' CSS,
which differs per browser and so cannot be hashed.
- A library may load after the SDK's first notification. Load any library a render function
calls with a plain (no , or ), and still guard the
call: when the library is missing (
typeof echarts === 'undefined'
), the render function
returns early and the next notification draws.
- , and cookies throw: keep state in variables. This includes
Basecoat's theme switcher, which stores the mode: follow , or the host's
own mode with (references/api.md), instead.
- Forms, popups, navigation, and are blocked: use buttons and
listeners, never .
- The app is its one HTML file: nothing it loads may be a file of the repo. No relative
or (, , ). Put your own scripts and styles
inline (, ), images inline as SVG or a URI, and libraries and fonts
from a CDN as above. Never split an app into several files or make one app load another's, and
never add anything but apps under .
- Every query is an : controls, cross-filters, sorting and date drills only apply to
one.
- Mark the page's structure in the HTML, as references/api.md
shows: and on the parts of the page, with a
, and / on the elements each query
and control draws into. Every query and control sits in a block: a standalone control gets a
block of its own, a chart's own control goes in the chart's block. The Inspect panel shows the
tree, finds each part on the page, and gives the user a handle for it to hand back to you.
Mark every part; it is one attribute each.
Render a selection without rebuilding the chart. A query that drives a cross-filter is not re-run
by its own selection, so its result is unchanged when the reader clicks: restyle the marks it
already draws (dim the unselected ones) rather than recreating the series, or the chart replays
its entry animation on every click. With ECharts, merge with a plain
for a
selection change and keep
for a new result.
Add a download only where the agreed design lists one, never on your own initiative, and read
references/downloads.md first: a block gets at most one download
control, in its header, with every agreed format an item of it, never a button per format; a
picture of the whole app is one control in the page's header. Don't build a CSV from
: a paged result holds only the pages loaded. Any app may be pictured from the
host's toolbar, so keep every app render-safe: chart entry animations off (ECharts
), images and web fonts from servers that send CORS headers (jsDelivr, Google
Fonts), and
preserveDrawingBuffer: true
on a WebGL chart.
Render each query's four states:
(loading),
,
, and success with no
rows ("No sales this week", never a bare zero). Render an error by who must act, as
references/api.md shows: no access or sign in again for
and
, the message for any other
or
error, and for a
one
"couldn't load" with a Retry that calls
, which reruns only the failed queries. Log
,
and
result.debug?.executedAql
on each query's first success:
the real row keys, value types and the AQL that ran. A
stays in the browser's
console (only warnings and errors reach
's log): working from the log alone, run the
query with
for its columns. Put on the page the definitions, dates and
any rounding a reader needs to trust a number.
5. Run and check it
First run
and fix every
warning it reports: a file referenced beside
the app (every
,
and CSS
must be
,
or a
fragment), or a CDN
script or stylesheet without
.
sh
anfra status # is a server already running for this repo, and where?
anfra serve # if not: http://127.0.0.1:7878/apps/ (or the free port it prints)
Leave it running: it reloads the open app whenever its file, or the AML, changes. Lineage is the
exception: run
after an AML change, or it shows the old definitions. Its header shows
the repo's problems (AML that does not compile, a data source not configured).
Open the app in a browser (yours, or ask the user to) and exercise every control and click. Then
read what went wrong without the browser: while the app is open,
writes each of its
runtime errors (a failed query, an uncaught error or rejection, a listener's exception, a call
the host failed) and its
/
lines to its log, the file
prints as
, one JSON line each with
, the app's path as
,
and
(the error's message as
, with its
,
,
,
,
) or
(the line as
). The
says where the error came from:
(a failed query),
,
and the like (a call you left
unhandled),
,
(your code),
(a plugin's), or
(the host's):
sh
grep '"service":"app"' <repo.logs> | grep '"app":"sales/overview.html"' | tail -20
No
lines at all, with the app open, means
runs with
: read the panel instead, or ask the user to.
Each load of the app starts with an
line, with the file's
time:
after an edit, read only the lines after the last one, or you are reading the old file's errors.
A stack names the file's own lines as
definition:<line>:<column>
, and the SDK's as
.
The same errors are in the panel's Overview, under Problems. Read errors against
references/api.md, by
: a
error
(
) names a declaring line;
names a call made wrong (an
call with an empty or malformed target list; an unknown field comes back as that
target's
or an empty diagram);
carries anfra's diagnostics for the AQL, with
their lines, and
names the bad entry a control or cross-filter added
(
: a
typo, an unsupported operator, a drill on a field no
dimension is built on).
Compare a misbehaving query's
with what you meant, and fix the smallest thing that
explains it. When the server fails a query, its full cause is in the same log: find the server's
line by the app line's
. A bare "The call failed." is a host-side error: its app line
(
) has the original message and the call.
If you could not check the interface, say so rather than claiming it works.
6. Hand over
Tell the user, in their terms: what the page shows and how to use each control, its URL, the
assumptions you made, and what you checked. When a later request names a part by its handle,
[Revenue over time](data-anfra-block="trend")
or a bare
, search the
file for that attribute: it is the element the user means. A
URL opens only on this machine;
has no authentication, so ask before serving it beyond loopback (
). Make each
requested change the same way: restate it if it changes what a number means, change the smallest
thing, and check that the agreed result still holds.
When the user says something is wrong ("the chart is empty"), read the app's log lines since its
last
before asking them anything (see "Run and check it"): the error, its
and
the server's line by
usually say what. If there is no mount since your last edit, ask
them to reload the app first.
Related skills
- — add the fields and metrics an app needs.
- — write and validate the queries.
- — set up the repo and its data source first.
anfra skills 0.19.0