Logic Bricks
A Logic Brick is a function that a dagboard runs as one of its steps. It takes the values of the Input Bricks and Logic Bricks wired into it, and its result goes on to the next Logic Bricks and to the Viz Bricks.
This page shows Logic Bricks in Python. They can be written in R too, which R bricks shows.
Write a Logic Brick
Make's Logic Brick, in the sidebar, opens the Logic Brick editor. A Logic Brick is a
Python module with a function named handle:
def handle(x: float, exponent: float = 2) -> float:
'''
Raises a number to a power.
:param x: The number.
:param exponent: The power to raise it to; 2 when left empty.
:return: x to the power of exponent.
'''
return x ** exponent
input_fixtures = dict(x=3.0)
- The type hints are the contract. The app reads them to know the Logic Brick's inputs and output, so every input needs a type hint, or a default whose type stands in for one, and so does the return value. Return a value of the type you declare.
-
The types are
str,int,float,bool,bytes,datetime.date,datetime.datetime, lists such aslist[str], dicts with string keys,pd.Seriesandpd.DataFrame. -
Several outputs: return a
TypedDict, and the Logic Brick has one output per key, each of which can be wired on its own. - The docstring is what people see when they pick the Logic Brick, and what the assistant reads to choose it. Say what it does, each input with its units, and the output.
-
input_fixturesgives a sample value for each input without a default, which the app uses as the Logic Brick's sample inputs. -
Errors: raise an exception whose message says what to fix, such as
ValueError("window must be positive"). The dagboard shows the message on the Logic Brick, and the Logic Bricks that depend on it don't run. - Logs: what the Logic Brick prints, and what it logs at INFO and above, shows with its run.
Try it
- The editor checks the code as you type, and marks its errors.
- Save asks for the Logic Brick's name. Run works once your latest changes are saved, with the values you type in Inputs, and shows its result in Outputs and what it printed in Console. Stop cancels a run.
- Suggest Code, on the Code panel, asks the assistant for the Logic Brick's code from what you describe, and Apply Code puts its answer in the editor (The assistant and your key).
- To use the Logic Brick in a dagboard, add it with Logic Brick while editing the dagboard (Dagboards).
A dagboard stores a Logic Brick's name, its inputs' names and its output's type, so changing one of them breaks the dagboards that use it. To rename a Logic Brick or an input, add a new Logic Brick and keep the old one.
Link Bricks
A Link Brick is a Logic Brick that reads from or writes to an API, a file or a database, such as a client of a web API. The sidebar and Search list Link Bricks apart from the other Logic Bricks, and a dagboard's + adds one as it adds a Logic Brick.
- Make's Link Brick opens the editor on a client of a public API that needs no sign-in, with a timeout and errors that say what went wrong, for you to point at yours. With an AI linked, describe the API there instead, and the assistant writes the client.
-
A Link Brick is marked by
brick_kind = 'link'in its module, beside itshandle. - In the browser, the API must let pages from other sites read its answers, as for any request a Logic Brick makes (What holds in the browser).
- Don't write a password or an API token into its code, which goes into the bundles you export and the repos you share.
Timeseries
The charts over time, such as Line and Bar, take a pd.Series of floats with a two-level index,
(timestamp, id), and draw one line per id. The library's Logic Bricks make and take that shape:
-
the index's timestamps arrive as a
DatetimeIndexin UTC, with thedatetime64[ns, UTC]dtype, so date arithmetic works on them as they are; - a series that crosses from one Logic Brick to another is padded with NaN to every timestamp of every id, so compute per id.
The library
The app's default sources hold two libraries of Logic Bricks, which every dagboard can use and which you can read as examples (The default sources):
- brickutils, the general-purpose building blocks: casts between types, dataframe cells and timeseries filters;
- examples: data feeds that need no key, such as Coinbase's crypto prices and the European Central Bank's exchange rates through Frankfurter, technical indicators, returns and risk statistics, a mock portfolio system, graphs, sample data and small utilities.
A library Logic Brick's name starts with its library, such as
brickutils/series.multiply. The Logic Bricks you write keep the name you give them, and
show as private.
What holds in the browser
Logic Bricks run in Python in your browser, so a few things differ from Python on a computer:
-
Time is UTC. A Logic Brick sees UTC, whatever your computer's time zone:
datetime.now()gives UTC. The app's own dates keep your time zone. -
Only the app's packages. Python has the packages of dagboard.ai's environment, which the
next section lists. There's no
pip install, so a Logic Brick that imports another package fails before it runs, with an error that names the module. -
Requests follow your browser's rules. A Logic Brick can fetch data with
requestsfrom the hosts its code names, and your browser applies its rules for requests to other sites (CORS): the host must allow pages from other sites to read its answers. Over plain HTTP, only your own machine can be reached. Some of the library's feeds are left out of the app for this reason. - No environment variables. A Logic Brick gets no workspace variables or secrets, so a feed that needs an API key doesn't work yet. Don't write a key into a Logic Brick's code: the code is kept in your workspace and goes into the bundles you export.
-
Cancel stops Python. Cancelling a run interrupts the Logic Brick with
KeyboardInterrupt. A Logic Brick that doesn't stop, such as one inside a blocking request or in C code, has its Python worker stopped, and a new one starts for the next run. - Several at once. The app runs a Logic Brick per Python worker, and starts more workers when Logic Bricks are waiting, as your device's processor cores allow. A module's globals can last between runs in the same worker, so keep only read-only caches there.
Python's packages
dagboard.ai's environment is the same in every browser, with one version of each package:
- The base, ready when Python starts: Python's standard library, NumPy, pandas and protobuf, and what they need.
- The optional packages, each loaded the first time a Logic Brick imports it: SciPy, scikit-learn, statsmodels, NetworkX, pyarrow, python-calamine, tzdata, cachetools, requests and ta, and what they need.
Nothing for plots or controls, such as matplotlib, seaborn, Plotly, Bokeh, Altair or ipywidgets: widgets show and take a dagboard's data. Nothing from PyPI or micropip.
pandas' features that need an optional package load it too, as if the Logic Brick imported it:
python-calamine for read_excel(..., engine="calamine"), and SciPy for weighted rolling windows
(win_type), corr(method="kendall") and the SciPy methods of
interpolate, such as "spline". When the method comes from a variable, import the
package in the Logic Brick.
A library of Logic Bricks in a repo or a folder declares the packages its Logic Bricks import, each with the
oldest version they run on, in the pyenv.json at its root:
{
"format": 4,
"python": ">=3.13",
"packages": { "scipy": ">=1.14.1", "statsmodels": ">=0.14.6" }
}
A library made before, which declares them in its pyproject.toml's
[tool.bricks] table, works too.
Opening a dagboard that uses the library starts loading them, so its Logic Bricks wait less. A library's Logic Brick that imports an optional package its library doesn't declare fails, with an error that says what to add. A library that declares a package outside the environment can't be added as a source. The default sources' libraries declare theirs the same way. The Logic Bricks you write in the app may import any of the optional packages.
Next: R bricks