.md →
Utils API#
Besides the widgets, wigglystuff ships a handful of plain helper functions: one that
builds a forecast chart from a time series, one that converts an Altair chart to SVG,
and two decorators that let a function's plot be swapped in place instead of
re-rendered.
See also: AltairWidget for flicker-free Altair updates, ImageRefreshWidget and HTMLRefreshWidget for the widgets the refresh decorators return.
forecast_chart#
Create a time series chart with an exponential forecast.
Fits y = a * b^x via log-linear regression (Huber loss for robustness)
on the last fit_window data points and projects forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
A Polars DataFrame with at least a date column and a value column. |
required | |
date_col
|
str
|
Name of the date column. |
required |
value_col
|
str
|
Name of the numeric value column. |
required |
fit_window
|
int
|
Number of most-recent data points to use for the fit. |
180
|
projection_days
|
int
|
How many days to project into the future. |
365
|
title
|
str | None
|
Optional chart title. |
None
|
width
|
int | str | None
|
Chart width in pixels, or "container" for responsive. Defaults to "container". |
None
|
height
|
int
|
Chart height in pixels (default 400). |
400
|
Returns:
| Type | Description |
|---|---|
|
An interactive Altair chart with actual data (solid) and |
|
|
forecast (dashed). |
Examples:
import polars as pl
from wigglystuff import forecast_chart
df = pl.DataFrame({"date": [...], "value": [...]})
forecast_chart(df, "date", "value", fit_window=90)
Source code in wigglystuff/utils.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
altair2svg#
Convert an Altair chart to SVG format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart
|
Any
|
An Altair chart object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The SVG representation of the chart as a string. |
Note
This function writes to disk temporarily as Altair doesn't provide an in-memory API for SVG conversion.
Source code in wigglystuff/utils.py
refresh_matplotlib#
Decorator to convert matplotlib plotting functions to base64-encoded images.
This decorator wraps a matplotlib plotting function and returns a base64-encoded data URI that can be used with ImageRefreshWidget for live updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
A function that creates matplotlib plots using plt commands. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
callable |
Callable[..., str]
|
A wrapper function that returns a base64-encoded JPEG data URI. |
Example
@refresh_matplotlib ... def plot_sine(x): ... plt.plot(x, np.sin(x)) ... widget = ImageRefreshWidget() widget.src = plot_sine(np.linspace(0, 2*np.pi, 100))
Source code in wigglystuff/utils.py
refresh_altair#
Decorator to convert Altair chart functions to SVG strings.
This decorator wraps a function that returns an Altair chart and converts the chart to an SVG string that can be used with HTMLRefreshWidget for live updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
A function that returns an Altair chart object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
callable |
Callable[..., str]
|
A wrapper function that returns an SVG string representation of the chart. |
Example
@refresh_altair ... def create_chart(data): ... return alt.Chart(data).mark_bar().encode(x='x', y='y') ... widget = HTMLRefreshWidget() widget.html = create_chart(df)