Skip to content

Commit 4019151

Browse files
refactor: examples into structure, and polish
1 parent fa1fe78 commit 4019151

File tree

19 files changed

+344
-137
lines changed

19 files changed

+344
-137
lines changed

solara/components/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
from .select import Select, SelectMultiple # noqa: #F401 F403
3737
from .matplotlib import FigureMatplotlib # noqa: #F401 F403
3838
from .echarts import FigureEcharts # noqa: #F401 F403
39+
from .figure_altair import FigureAltair, AltairChart # noqa: #F401 F403

solara/components/figure_altair.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Any, Callable
2+
3+
import solara
4+
import solara.widgets
5+
6+
7+
@solara.component
8+
def FigureAltair(
9+
chart,
10+
on_click: Callable[[Any], None] = None,
11+
on_hover: Callable[[Any], None] = None,
12+
):
13+
"""Renders an Altair chart using VegaLite.
14+
15+
## Arguments
16+
17+
- chart: Altair chart
18+
- on_click: Callback function for click events.
19+
- on_hover: Callback function for hover events.
20+
21+
"""
22+
import altair as alt
23+
24+
with alt.renderers.enable("mimetype"):
25+
bundle = chart._repr_mimebundle_()[0]
26+
key = "application/vnd.vegalite.v4+json"
27+
if key not in bundle:
28+
raise KeyError(f"{key} not in mimebundle:\n\n{bundle}")
29+
spec = bundle[key]
30+
return solara.widgets.VegaLite.element(
31+
spec=spec, on_click=on_click, listen_to_click=on_click is not None, on_hover=on_hover, listen_to_hover=on_hover is not None
32+
)
33+
34+
35+
# alias for backward compatibility
36+
AltairChart = FigureAltair

solara/components/misc.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,6 @@ def Padding(size, children=[], grow=True):
163163
return v.Sheet(class_=f"pa-{size}", style_=style, elevation=0, children=children)
164164

165165

166-
@solara.component
167-
def AltairChart(chart, on_click=None, on_hover=None):
168-
import altair as alt
169-
170-
with alt.renderers.enable("mimetype"):
171-
bundle = chart._repr_mimebundle_()[0]
172-
key = "application/vnd.vegalite.v4+json"
173-
if key not in bundle:
174-
raise KeyError(f"{key} not in mimebundle:\n\n{bundle}")
175-
spec = bundle[key]
176-
return solara.widgets.VegaLite.element(
177-
spec=spec, on_click=on_click, listen_to_click=on_click is not None, on_hover=on_hover, listen_to_hover=on_hover is not None
178-
)
179-
180-
181166
@solara.component
182167
def FigurePlotly(
183168
fig,

solara/website/pages/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def add(path):
7272
add("error")
7373
with ListItem("Viz", icon_name="mdi-chart-histogram"):
7474
with List():
75+
add("altair")
7576
add("echarts")
7677
add("matplotlib")
7778
add("plotly")

solara/website/pages/api/altair.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""# FigureAltair
2+
3+
"""
4+
5+
import altair as alt
6+
import pandas as pd
7+
8+
import solara
9+
from solara.website.utils import apidoc
10+
11+
title = "FigureAltair"
12+
13+
df = pd.DataFrame({"a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"], "b": [28, 55, 43, 91, 81, 53, 19, 87, 52]})
14+
15+
16+
@solara.component
17+
def Page():
18+
click_data, set_click_data = solara.use_state(None)
19+
hover_data, set_hover_data = solara.use_state(None)
20+
21+
chart = alt.Chart(df).mark_bar().encode(x="a", y="b")
22+
23+
with solara.Div() as main:
24+
solara.AltairChart(chart, on_click=set_click_data, on_hover=set_hover_data)
25+
26+
solara.Markdown(
27+
f"""
28+
Click data:
29+
30+
```
31+
{click_data}
32+
```
33+
34+
Hover data:
35+
```
36+
{hover_data}
37+
```
38+
"""
39+
)
40+
41+
return main
42+
43+
44+
__doc__ += apidoc(solara.FigureAltair.f) # type: ignore

solara/website/pages/api/matplotlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44

55
import numpy as np
6-
import solara
76
from matplotlib.figure import Figure
7+
8+
import solara
89
from solara.website.utils import apidoc
910

1011
x = np.linspace(0, 2, 100)

solara/website/pages/examples/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import inspect
2-
import urllib.parse
1+
# import inspect
2+
# import urllib.parse
33

44
import solara
55

@@ -14,7 +14,13 @@ def Page():
1414

1515
@solara.component
1616
def Layout(children):
17-
route_current, all_routes = solara.use_route()
17+
# TODO: this is using a private API, what is the best way to do this?
18+
# we want to 'eat' the whole route for the current level, and the level below
19+
# for example the utilities directory. But if an example does routing, we don't want
20+
# to take on that route.
21+
router = solara.use_router()
22+
route_current = router.path_routes[-1]
23+
# route_current, all_routes = solara.use_route()
1824

1925
if route_current is None:
2026
return solara.Error("Page not found")
@@ -23,15 +29,20 @@ def Layout(children):
2329
github_url = solara.util.github_url(module.__file__)
2430

2531
with solara.HBox(grow=False) as main:
26-
with solara.VBox(grow=True):
32+
with solara.VBox(grow=True, align_items="baseline"):
33+
doc = module.__doc__
34+
if doc:
35+
with solara.VBox(grow=True):
36+
solara.Markdown(doc)
2737
with solara.HBox():
2838
if route_current.path != "/":
29-
solara.Button("View on GitHub", icon_name="mdi-git", href=github_url, class_="ma-2", target="_blank")
30-
code = inspect.getsource(module)
39+
solara.Button("View source code on GitHub", icon_name="mdi-github-circle", href=github_url, class_="ma-2", target="_blank", text=True)
40+
# code = inspect.getsource(module)
3141

32-
code_quoted = urllib.parse.quote_plus(code)
33-
url = f"https://test.solara.dev/try?code={code_quoted}"
34-
solara.Button("Run on solara.dev", icon_name="mdi-pencil", href=url, class_="ma-2", target="_blank")
42+
# code_quoted = urllib.parse.quote_plus(code)
43+
# url = f"https://test.solara.dev/try?code={code_quoted}"
44+
# solara.Button("Run on solara.dev", icon_name="mdi-pencil", href=url, class_="ma-2", target="_blank")
45+
# with solara.HBox():
3546
if not hasattr(module, "Page"):
3647
solara.Error(f"No Page component found in {module}")
3748
else:

solara/website/pages/examples/altair.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

solara/website/pages/examples/basics/__init__.py

Whitespace-only changes.

solara/website/pages/examples/sine.py renamed to solara/website/pages/examples/basics/sine.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
"""# Interactive sine wave
2+
3+
4+
This example shows how to have two slider control a visualization.
5+
6+
"""
7+
18
import numpy as np
29
import plotly.express as px
10+
311
import solara
412

513
x = np.linspace(0, 2, 100)
614

15+
title = "Interactive sine wave"
16+
717

818
@solara.component
919
def Page():

0 commit comments

Comments
 (0)