Skip to content

Commit c5214e1

Browse files
committed
add better docs for layout controls
1 parent 9c00d1d commit c5214e1

9 files changed

Lines changed: 6648 additions & 1811 deletions

File tree

docs/examples/demo.ipynb

Lines changed: 1794 additions & 1794 deletions
Large diffs are not rendered by default.

docs/examples/layout.ipynb

Lines changed: 4816 additions & 0 deletions
Large diffs are not rendered by default.

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pip install "manywidgets[lonboard]"
3030

3131
**Layout:** [`Row`](widgets/row.ipynb), [`Column`](widgets/column.ipynb),
3232
[`Grid`](widgets/grid.ipynb) — arrange widgets side-by-side while keeping them
33-
linked, live and in static export.
33+
linked, live and in static export. See the [layout examples](examples/layout.ipynb)
34+
for how they compose.
3435

3536
**Lonboard interop** (optional, `pip install "manywidgets[lonboard]"`):
3637
[`LayerToggle`](widgets/layer_toggle.ipynb),

docs/myst.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ project:
5555
- title: Examples
5656
children:
5757
- file: examples/demo.ipynb
58+
- file: examples/layout.ipynb
5859
- file: examples/lonboard-map.ipynb
5960
site:
6061
template: book-theme

src/manywidgets/chart/src/index.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ const PALETTE = [
4242
"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf",
4343
];
4444

45-
function formatSeriesData(seriesData: Series[]) {
45+
function formatSeriesData(seriesData: Series[], defaultType: string) {
4646
return (seriesData || []).map((series, index) => {
4747
const color = series.color || PALETTE[index % PALETTE.length];
48+
const type = series.type || defaultType || "line";
4849

4950
let data: unknown;
5051
if (series.data && series.data.length > 0) {
@@ -62,19 +63,19 @@ function formatSeriesData(seriesData: Series[]) {
6263
data,
6364
borderColor: color,
6465
backgroundColor: color + "33",
65-
type: series.type || "line",
66+
type,
6667
fill: series.fill !== undefined ? series.fill : false,
6768
tension: series.tension ?? 0.1,
6869
pointRadius: series.pointRadius !== undefined ? series.pointRadius : 3,
6970
pointHoverRadius: 5,
7071
borderWidth: series.borderWidth || 2,
7172
};
7273

73-
if (series.type === "scatter") {
74+
if (type === "scatter") {
7475
dataset.showLine = false;
7576
dataset.pointRadius = series.pointRadius || 5;
7677
}
77-
if (series.type === "bar") {
78+
if (type === "bar") {
7879
dataset.backgroundColor = color + "80";
7980
}
8081
return dataset;
@@ -146,31 +147,33 @@ function render({ model, el }: RenderProps<ChartModel>): () => void {
146147
return options;
147148
}
148149

150+
function build(): void {
151+
chart?.destroy();
152+
chart = new Chart(canvas, {
153+
type: (model.get("chart_type") || "line") as never,
154+
data: { datasets: formatSeriesData(model.get("series_data"), model.get("chart_type")) as never },
155+
options: buildOptions() as never,
156+
});
157+
}
158+
149159
function createOrUpdate(): void {
150-
const datasets = formatSeriesData(model.get("series_data"));
151-
const options = buildOptions();
152160
if (!chart) {
153-
chart = new Chart(canvas, {
154-
type: (model.get("chart_type") || "line") as never,
155-
data: { datasets: datasets as never },
156-
options: options as never,
157-
});
161+
build();
158162
} else {
159-
chart.data = { datasets: datasets as never };
160-
chart.options = options as never;
163+
chart.data = { datasets: formatSeriesData(model.get("series_data"), model.get("chart_type")) as never };
164+
chart.options = buildOptions() as never;
161165
chart.update();
162166
}
163167
}
164168

165-
if ((model.get("series_data") || []).length > 0) createOrUpdate();
169+
if ((model.get("series_data") || []).length > 0) build();
166170

167171
// NOTE: register one listener per trait — the static-export model emitter does
168172
// not support space-separated event names (see core's onChanges docstring).
169173
onChanges(
170174
model,
171175
[
172176
"series_data",
173-
"chart_type",
174177
"chart_options",
175178
"animation_enabled",
176179
"tooltips_enabled",
@@ -182,6 +185,10 @@ function render({ model, el }: RenderProps<ChartModel>): () => void {
182185
createOrUpdate,
183186
);
184187

188+
// Switching the default series type requires a fresh chart — Chart.js won't
189+
// re-type existing datasets via update().
190+
onChanges(model, ["chart_type"], build);
191+
185192
onChanges(model, ["width", "height"], () => {
186193
container.style.width = `${model.get("width")}px`;
187194
container.style.height = `${model.get("height")}px`;

src/manywidgets/chart/widget.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def add_series(
8787
"""
8888
data = self._coerce_points(x, y, data)
8989
series = {
90-
"type": series_type or self.chart_type,
90+
# Only pin a type when the caller asks for one; otherwise leave it
91+
# unset so the series follows the chart's live ``chart_type`` (lets a
92+
# control bound to ``chart_type`` re-type the chart).
93+
"type": series_type,
9194
"data": data,
9295
"name": name or f"Series {len(self.series_data) + 1}",
9396
}

src/manywidgets/column/doc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ Column(s, nd, gap="12px")
2626
{api-table}
2727

2828
Pass children positionally (`Column(a, b)`) or as a list (`Column(children=[a, b])`).
29+
30+
See [Layout](../examples/layout.ipynb) for how `Row`, `Column`, and `Grid`
31+
compose into a full screen.

src/manywidgets/grid/doc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ Grid(
3030
{api-table}
3131

3232
Pass children positionally (`Grid(a, b, c)`) or as a list (`Grid(children=[a, b, c])`).
33+
34+
See [Layout](../examples/layout.ipynb) for how `Row`, `Column`, and `Grid`
35+
compose into a full screen.

src/manywidgets/row/doc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ Row(s, stat, gap="24px")
2727
{api-table}
2828

2929
Pass children positionally (`Row(a, b)`) or as a list (`Row(children=[a, b])`).
30+
31+
See [Layout](../examples/layout.ipynb) for how `Row`, `Column`, and `Grid`
32+
compose into a full screen.

0 commit comments

Comments
 (0)