Skip to content

Commit e92be8e

Browse files
committed
Make frontend-backend talk
1 parent b0452e5 commit e92be8e

File tree

7 files changed

+44
-65
lines changed

7 files changed

+44
-65
lines changed

dashi/src/lib/components/DashiPlot.tsx

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,33 @@
1-
import { createClassFromSpec, type VisualizationSpec } from "react-vega";
1+
import { createClassFromSpec } from "react-vega";
22

33
import { type PlotState } from "@/lib/types/state/component";
4-
import { type PropertyChangeHandler } from "@/lib/types/model/event";
4+
// import { type PropertyChangeHandler } from "@/lib/types/model/event";
55
import type { CSSProperties } from "react";
66

7-
export interface DashiPlotProps
8-
extends Omit<PlotState, "type">,
9-
Omit<PlotState, "onPropertyChange"> {
10-
onPropertyChange: PropertyChangeHandler;
7+
export interface DashiPlotProps extends Omit<PlotState, "type"> {
8+
// onPropertyChange: PropertyChangeHandler;
119
}
1210

1311
interface VegaChartWrapperProps {
1412
id?: string;
1513
style?: CSSProperties;
1614
}
1715

18-
const specification: VisualizationSpec = {
19-
config: { view: { continuousWidth: 300, continuousHeight: 300 } },
20-
data: { name: "myData" },
21-
mark: { type: "bar" },
22-
description: "A simple bar chart with embedded data.",
23-
encoding: {
24-
tooltip: [
25-
{ field: "a", title: "a", type: "nominal" },
26-
{ field: "b", title: "b", type: "quantitative" },
27-
],
28-
x: { field: "a", title: "a", type: "nominal" },
29-
y: { field: "b", title: "b", type: "quantitative" },
30-
},
31-
params: [{ name: "click", select: { type: "point", on: "click" } }],
32-
$schema: "https://vega.github.io/schema/vega-lite/v5.20.1.json",
33-
datasets: {},
34-
};
35-
36-
const data = {
37-
myData: [
38-
{ a: "A", b: 28 },
39-
{ a: "B", b: 55 },
40-
{ a: "C", b: 43 },
41-
{ a: "D", b: 91 },
42-
{ a: "E", b: 81 },
43-
{ a: "F", b: 53 },
44-
{ a: "G", b: 19 },
45-
{ a: "H", b: 87 },
46-
{ a: "I", b: 52 },
47-
],
48-
};
49-
5016
export function DashiPlot({
5117
id,
5218
style,
19+
figure,
5320
// onPropertyChange,
5421
}: DashiPlotProps) {
22+
const { datasets, ...spec } = figure;
5523
const Plot = createClassFromSpec({
5624
mode: "vega-lite",
57-
spec: specification,
25+
spec: spec,
5826
});
5927
const VegaChartWrapper = ({ id, style }: VegaChartWrapperProps) => {
6028
return (
6129
<div id={id} style={style}>
62-
<Plot data={data} />
30+
<Plot data={datasets} />
6331
</div>
6432
);
6533
};

dashi/src/lib/types/state/component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type CSSProperties } from "react";
2-
import { type Config, type Layout, type PlotData } from "plotly.js";
2+
import type { VisualizationSpec } from "react-vega";
33

44
export type ComponentType = "Button" | "Checkbox" | "Dropdown" | "Plot" | "Box";
55

@@ -37,11 +37,9 @@ export interface CheckboxState extends ComponentState {
3737

3838
export interface PlotState extends ComponentState {
3939
type: "Plot";
40-
figure: {
41-
data: PlotData[];
42-
layout: Partial<Layout>;
43-
config: Partial<Config>;
44-
} | null;
40+
figure: VisualizationSpec & {
41+
datasets?: Record<string, unknown>; // Add the datasets property
42+
};
4543
}
4644

4745
export interface BoxState extends ContainerState {

dashipy/dashipy/callback.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def _parameter_to_dict(parameter: inspect.Parameter) -> dict[str, Any]:
207207
_object_types = {
208208
"Figure": "Figure",
209209
"Component": "Component",
210+
"Chart": "Chart"
210211
}
211212

212213

dashipy/dashipy/components/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from dataclasses import dataclass
22
from typing import Any
33

4-
import plotly.graph_objects as go
4+
import altair as alt
55

66
from dashipy import Component
77

88

99
@dataclass(frozen=True)
1010
class Plot(Component):
11-
figure: go.Figure | None = None
11+
figure: alt.Chart | None = None
1212

1313
def to_dict(self) -> dict[str, Any]:
1414
d = super().to_dict()

dashipy/dashipy/demo/context.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
from typing import Union
2+
import pandas as pd
3+
4+
15
class Context:
26
def __init__(self):
3-
self.datasets: dict[int, list[int]] = {
4-
0: [10, 20, 30],
5-
1: [20, 30, 10],
6-
2: [30, 10, 20],
7-
}
7+
self.datasets= {0: pd.DataFrame(({'a': 'A', 'b': 28},
8+
{'a': 'B', 'b': 55},
9+
{'a': 'C', 'b': 43},
10+
{'a': 'D', 'b': 91},
11+
{'a': 'E', 'b': 81})),
12+
1: pd.DataFrame(({'a': 'V', 'b': 99},
13+
{'a': 'W', 'b': 1},
14+
{'a': 'X', 'b': 7},
15+
{'a': 'Y', 'b': 43},
16+
{'a': 'Z', 'b': 49}))
17+
}

dashipy/environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dependencies:
55
- python
66
# Dependencies
77
- pandas
8-
- plotly
8+
- altair
99
- pyaml
1010
- tornado
1111
# Dev Dependencies

dashipy/my_extension/my_panel_1.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import plotly.graph_objects as go
2-
from plotly.graph_objs import Layout
1+
import altair as alt
32

43
from dashipy import (Component, Input, Output)
54
from dashipy.components import (Plot, Box, Dropdown)
@@ -48,14 +47,17 @@ def render_panel(ctx: Context) -> Component:
4847
Input("selected_dataset"),
4948
Output("plot", "figure"),
5049
)
51-
def make_figure(ctx: Context, selected_dataset: int = 0) -> go.Figure:
50+
def make_figure(ctx: Context, selected_dataset: int = 0) -> alt.Chart:
5251
dataset = ctx.datasets[selected_dataset]
53-
fig = go.Figure(
54-
layout=Layout(
55-
title=f"DS #{selected_dataset + 1}",
56-
margin=dict(t=40, r=4, b=4, l=4),
57-
autosize=True,
58-
)
59-
)
60-
fig.add_trace(go.Bar(x=["A", "B", "C"], y=dataset))
52+
fig = alt.Chart(dataset).mark_bar().encode(
53+
x=alt.X('a:N', title='a'),
54+
y=alt.Y('b:Q', title='b'),
55+
tooltip=[
56+
alt.Tooltip('a:N', title='a'),
57+
alt.Tooltip('b:Q', title='b'),
58+
]
59+
).properties(
60+
width=400,
61+
height=400
62+
)
6163
return fig

0 commit comments

Comments
 (0)