Skip to content

Commit ac29b6a

Browse files
committed
Python side of Plot -> VegaChart
1 parent 661b088 commit ac29b6a

File tree

11 files changed

+49
-36
lines changed

11 files changed

+49
-36
lines changed

chartlets.js/CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
## Version 0.1.0 (not started)
22

3-
* Allow for different chart providers:
4-
- Rename `Plot` into `VegaChart`.
5-
- Only define `VegaChart` if `vega-altair` is installed.
63
* Reorganise Chartlets project
74
- Create `chartlets` GH org.
85
- Split current `chartlets` repo and move to `chartlets` org:
@@ -19,6 +16,10 @@
1916

2017
## Version 0.0.30 (in development)
2118

19+
* Allow for different chart providers:
20+
- Renamed `Plot` into `VegaChart`.
21+
- `VegaChart` is defined only if `vega-altair` is installed.
22+
2223
* The `Plot` component now respects a `theme` property. If not given,
2324
it will respect the current MUI theme mode `"dark"`.
2425

chartlets.py/CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Version 0.0.30 (in development)
2+
3+
* Allow for different chart providers:
4+
- Renamed `Plot` into `VegaChart`.
5+
- `VegaChart` is defined only if `vega-altair` is installed.
6+
17
* The `Plot` component now respects a `theme` property. If not given,
28
it will respect the current MUI theme mode `"dark"`.
39

chartlets.py/chartlets/components/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
from .progress import CircularProgressWithLabel
77
from .progress import LinearProgress
88
from .progress import LinearProgressWithLabel
9-
from .plot import Plot
9+
from .charts.vega import VegaChart
1010
from .select import Select
1111
from .typography import Typography

chartlets.py/chartlets/components/charts/__init__.py

Whitespace-only changes.

chartlets.py/chartlets/components/plot.py renamed to chartlets.py/chartlets/components/charts/vega.py

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

4-
import altair as alt
4+
try:
5+
# noinspection PyUnresolvedReferences
6+
import altair
7+
AltairChart = altair.Chart
8+
except ImportError:
9+
AltairChart = type(None)
510

611
from chartlets import Component
712

813

914
@dataclass(frozen=True)
10-
class Plot(Component):
11-
"""The plot component is a container for a
15+
class VegaChart(Component):
16+
"""A container for a
1217
[Vega Altair](https://altair-viz.github.io/) chart."""
1318

1419
theme: str | None = None
1520
"""The name of a [Vega theme](https://vega.github.io/vega-themes/)."""
1621

17-
chart: alt.Chart | None = None
22+
chart: AltairChart | None = None
1823
"""The [Vega Altair chart](https://altair-viz.github.io/gallery/index.html)."""
1924

2025
def to_dict(self) -> dict[str, Any]:

chartlets.py/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ channels:
44
dependencies:
55
# Library Dependencies
66
- python >=3.12,<3.13
7+
# Optional Dependencies
78
- altair
89
# Demo Dependencies
910
- pandas

chartlets.py/my_extension/my_panel_1.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import copy
2-
from types import NoneType
31
from typing import Any
42

53
import altair as alt
6-
import pandas as pd
74

85
from chartlets import Component, Input, Output, State
9-
from chartlets.components import Plot, Box, Select
6+
from chartlets.components import VegaChart, Box, Select
107
from chartlets.demo.contribs import Panel
118
from chartlets.demo.context import Context
129

@@ -17,8 +14,8 @@
1714
@panel.layout()
1815
def render_panel(ctx: Context) -> Component:
1916
selected_dataset: int = 0
20-
plot = Plot(
21-
id="plot", chart=make_figure(ctx, selected_dataset), style={"flexGrow": 1}
17+
chart = VegaChart(
18+
id="chart", chart=make_chart(ctx, selected_dataset), style={"flexGrow": 1}
2219
)
2320
select = Select(
2421
id="selected_dataset",
@@ -44,15 +41,15 @@ def render_panel(ctx: Context) -> Component:
4441
"width": "100%",
4542
"height": "100%",
4643
},
47-
children=[plot, control_group],
44+
children=[chart, control_group],
4845
)
4946

5047

5148
@panel.callback(
5249
Input("selected_dataset"),
53-
Output("plot", "chart"),
50+
Output("chart", "chart"),
5451
)
55-
def make_figure(ctx: Context, selected_dataset: int = 0) -> alt.Chart:
52+
def make_chart(ctx: Context, selected_dataset: int = 0) -> alt.Chart:
5653
dataset_key = tuple(ctx.datasets.keys())[selected_dataset]
5754
dataset = ctx.datasets[dataset_key]
5855

@@ -88,9 +85,9 @@ def make_figure(ctx: Context, selected_dataset: int = 0) -> alt.Chart:
8885

8986

9087
@panel.callback(
91-
Input("plot", property="points"),
92-
State("plot", "chart.encoding"),
93-
Output("plot", "chart.encoding.color"),
88+
Input("chart", property="points"),
89+
State("chart", "chart.encoding"),
90+
Output("chart", "chart.encoding.color"),
9491
)
9592
def get_click_event_points(
9693
ctx: Context, points: dict[str, Any], encoding: dict[str, Any]

chartlets.py/my_extension/my_panel_2.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas as pd
33

44
from chartlets import Component, Input, State, Output
5-
from chartlets.components import Plot, Box, Select
5+
from chartlets.components import VegaChart, Box, Select
66
from chartlets.demo.contribs import Panel
77
from chartlets.demo.context import Context
88

@@ -17,9 +17,9 @@ def render_panel(
1717
) -> Component:
1818
dataset = ctx.datasets.get(selected_dataset_id)
1919
variable_names, var_name = get_variable_names(dataset)
20-
plot = Plot(
21-
id="plot",
22-
chart=make_figure(ctx, selected_dataset_id, var_name),
20+
chart = VegaChart(
21+
id="chart",
22+
chart=make_chart(ctx, selected_dataset_id, var_name),
2323
style={"flexGrow": 1},
2424
)
2525
select = Select(
@@ -46,16 +46,16 @@ def render_panel(
4646
"width": "100%",
4747
"height": "100%",
4848
},
49-
children=[plot, control_group],
49+
children=[chart, control_group],
5050
)
5151

5252

5353
@panel.callback(
5454
Input("@app", "selectedDatasetId"),
5555
Input("selected_variable_name"),
56-
Output("plot", "chart"),
56+
Output("chart", "chart"),
5757
)
58-
def make_figure(
58+
def make_chart(
5959
ctx: Context,
6060
selected_dataset_id: str | None = None,
6161
selected_variable_name: str | None = None,

chartlets.py/pyproject.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ keywords = [
1414
]
1515
license = {text = "MIT"}
1616
requires-python = ">=3.10"
17-
dependencies = [
18-
"altair",
19-
]
17+
dependencies = []
2018
classifiers = [
2119
"Development Status :: 5 - Production/Stable",
2220
"Intended Audience :: Science/Research",
@@ -46,7 +44,11 @@ exclude = [
4644
]
4745

4846
[project.optional-dependencies]
47+
opt = [
48+
"altair",
49+
]
4950
dev = [
51+
"altair",
5052
"black",
5153
"flake8",
5254
"pytest",
@@ -63,6 +65,7 @@ doc = [
6365
"mkdocstrings-python"
6466
]
6567
demo = [
68+
"altair",
6669
"pyaml",
6770
"pandas",
6871
"tornado",

chartlets.py/tests/components/charts/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)