ChartJS
ChartJS is an optional Chart.js integration for teams that want the Chart.js ecosystem while keeping Faststrap's core package lightweight.
Core Faststrap still ships Chart. Use ChartJS when you specifically want Chart.js configuration, plugins, or behavior.
Install
The chartjs extra is intentionally dependency-free in the Python package. The integration loads Chart.js from a CDN unless you pass your own asset URL, so no additional Python dependency is required.
Faststrap exposes CHARTJS_VERSION and CHARTJS_CDN_URL when you need to inspect or reuse the default pinned asset. ChartJSType is the type alias for supported Chart.js chart kinds.
Import
Add Assets
Basic Usage
ChartJS(
"revenue-chart",
type="bar",
data={
"labels": ["Jan", "Feb", "Mar"],
"datasets": [
{"label": "Revenue", "data": [120, 180, 240]},
],
},
)
With Options
ChartJS(
"conversion-chart",
type="line",
data={
"labels": ["Mon", "Tue", "Wed"],
"datasets": [{"label": "Conversion", "data": [4.2, 5.1, 4.8]}],
},
options={
"plugins": {"legend": {"display": False}},
"scales": {"y": {"beginAtZero": True}},
},
height=260,
)
Parameters
| Param | Type | Description |
|---|---|---|
chart_id |
str |
Canvas element ID. Must be unique on the page. |
type |
ChartJSType |
Chart.js chart type. |
data |
dict | None |
Chart.js data config. |
options |
dict | None |
Chart.js options config. |
height |
int | None |
Canvas height. |
width |
int | None |
Canvas width. |
responsive |
bool |
Adds Chart.js responsive option. |
faststrap.integrations.chartjs.chartjs_assets(*, version=CHARTJS_VERSION, cdn_url=None, defer=True)
Return script tags required by Chart.js.
Source code in src/faststrap/integrations/chartjs.py
faststrap.integrations.chartjs.add_chartjs(app, *, version=CHARTJS_VERSION, cdn_url=None, defer=True)
Attach Chart.js assets to a FastHTML app once.
Source code in src/faststrap/integrations/chartjs.py
faststrap.integrations.chartjs.ChartJS(chart_id, *, type='line', data=None, options=None, height=None, width=None, responsive=True, **kwargs)
Render a Chart.js canvas and initialization script.