Skip to content

Commit 18f546e

Browse files
committed
Express-ify examples where it makes sense to
1 parent 4dadfbd commit 18f546e

File tree

5 files changed

+135
-190
lines changed

5 files changed

+135
-190
lines changed

examples/altair/app.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
import altair as alt
2-
from shiny import App, render, ui
2+
import shiny.express
3+
from shiny import render
34
from vega_datasets import data
45

5-
from shinywidgets import output_widget, reactive_read, register_widget
6+
from shinywidgets import reactive_read, render_altair
67

7-
source = data.cars()
88

9-
app_ui = ui.page_fluid(
10-
ui.output_text_verbatim("selection"),
11-
output_widget("chart")
12-
)
9+
# Output selection information (click on legend in the plot)
10+
@render.text
11+
def selection():
12+
pt = reactive_read(jchart.widget.selections, "point")
13+
return "Selected point: " + str(pt)
1314

14-
def server(input, output, session):
15-
16-
# Replicate JupyterChart interactivity
17-
# https://altair-viz.github.io/user_guide/jupyter_chart.html#point-selections
15+
# Replicate JupyterChart interactivity
16+
# https://altair-viz.github.io/user_guide/jupyter_chart.html#point-selections
17+
@render_altair
18+
def jchart():
1819
brush = alt.selection_point(name="point", encodings=["color"], bind="legend")
19-
chart = alt.Chart(source).mark_point().encode(
20+
return alt.Chart(data.cars()).mark_point().encode(
2021
x='Horsepower:Q',
2122
y='Miles_per_Gallon:Q',
2223
color=alt.condition(brush, 'Origin:N', alt.value('grey')),
2324
).add_params(brush)
24-
25-
jchart = alt.JupyterChart(chart)
26-
27-
# Display/register the chart in the app_ui
28-
register_widget("chart", jchart)
29-
30-
# Reactive-ly read point selections
31-
@output
32-
@render.text
33-
def selection():
34-
pt = reactive_read(jchart.selections, "point")
35-
return "Selected point: " + str(pt)
36-
37-
app = App(app_ui, server)

examples/ipyleaflet/app.py

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
11
import ipyleaflet as L
2-
from shiny import App, reactive, render, req, ui
3-
4-
from shinywidgets import output_widget, reactive_read, render_widget
5-
6-
app_ui = ui.page_sidebar(
7-
ui.sidebar(
8-
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10),
9-
),
10-
ui.card(
11-
ui.output_text("map_bounds"),
12-
fill=False
13-
),
14-
ui.card(
15-
output_widget("lmap")
16-
),
17-
title="ipyleaflet demo"
18-
)
19-
20-
21-
def server(input, output, session):
22-
23-
@output
24-
@render_widget
25-
def lmap():
26-
return L.Map(center=(52, 360), zoom=4)
27-
28-
# When the slider changes, update the map's zoom attribute (2)
29-
@reactive.Effect
30-
def _():
31-
lmap.widget.zoom = input.zoom()
32-
33-
# When zooming directly on the map, update the slider's value (2 and 3)
34-
@reactive.Effect
35-
def _():
36-
zoom = reactive_read(lmap.widget, "zoom")
37-
ui.update_slider("zoom", value=zoom)
38-
39-
# Everytime the map's bounds change, update the output message (3)
40-
@output
41-
@render.text
2+
from shiny import reactive, render, req
3+
from shiny.express import input, ui
4+
5+
from shinywidgets import reactive_read, render_widget
6+
7+
ui.page_opts(title="ipyleaflet demo")
8+
9+
with ui.sidebar():
10+
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10)
11+
12+
@render_widget
13+
def lmap():
14+
return L.Map(center=(52, 360), zoom=4)
15+
16+
# When the slider changes, update the map's zoom attribute
17+
@reactive.Effect
18+
def _():
19+
lmap.widget.zoom = input.zoom()
20+
21+
# When zooming directly on the map, update the slider's value
22+
@reactive.Effect
23+
def _():
24+
zoom = reactive_read(lmap.widget, "zoom")
25+
ui.update_slider("zoom", value=zoom)
26+
27+
28+
with ui.card(fill=False):
29+
# Everytime the map's bounds change, update the output message
30+
@render.ui
4231
def map_bounds():
4332
b = reactive_read(lmap.widget, "bounds")
4433
req(b)
4534
lat = [round(x) for x in [b[0][0], b[0][1]]]
4635
lon = [round(x) for x in [b[1][0], b[1][1]]]
4736
return f"The map bounds is currently {lat} / {lon}"
48-
49-
50-
app = App(app_ui, server)

examples/ipywidgets/app.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1-
import ipywidgets as ipy
2-
from ipywidgets.widgets.widget import Widget
3-
from shiny import *
4-
5-
from shinywidgets import *
6-
7-
app_ui = ui.page_fluid(output_widget("slider", fillable=False, fill=False), ui.output_text("slider_val"))
8-
9-
10-
def server(input: Inputs, output: Outputs, session: Session):
11-
12-
@output
13-
@render_widget
14-
def slider():
15-
return ipy.IntSlider(
16-
value=7,
17-
min=0,
18-
max=10,
19-
step=1,
20-
description="Test:",
21-
disabled=False,
22-
continuous_update=False,
23-
orientation="horizontal",
24-
readout=True,
25-
readout_format="d",
26-
)
27-
28-
@output
29-
@render.text
30-
def slider_val():
31-
val = reactive_read(slider.widget, "value")
32-
return f"The value of the slider is: {val}"
33-
34-
35-
app = App(app_ui, server, debug=True)
1+
import shiny.express
2+
from ipywidgets import IntSlider
3+
from shiny import render
4+
5+
from shinywidgets import reactive_read, render_widget
6+
7+
8+
@render_widget
9+
def slider():
10+
return IntSlider(
11+
value=7,
12+
min=0,
13+
max=10,
14+
step=1,
15+
description="Test:",
16+
disabled=False,
17+
continuous_update=False,
18+
orientation="horizontal",
19+
readout=True,
20+
readout_format="d",
21+
)
22+
23+
@render.ui
24+
def slider_val():
25+
val = reactive_read(slider.widget, "value")
26+
return f"The value of the slider is: {val}"

examples/plotly/app.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import numpy as np
22
import plotly.graph_objs as go
3-
from shiny import *
3+
from shiny import reactive
4+
from shiny.express import input, ui
45
from sklearn.linear_model import LinearRegression
56

6-
from shinywidgets import output_widget, render_widget
7+
from shinywidgets import render_plotly
78

89
# Generate some data and fit a linear regression
910
n = 10000
@@ -13,39 +14,31 @@
1314
fit = LinearRegression().fit(x.reshape(-1, 1), dat[1])
1415
xgrid = np.linspace(start=min(x), stop=max(x), num=30)
1516

16-
app_ui = ui.page_fillable(
17-
ui.input_checkbox("show_fit", "Show fitted line", value=True),
18-
output_widget("scatterplot")
19-
)
20-
21-
22-
def server(input, output, session):
23-
24-
@output
25-
@render_widget
26-
def scatterplot():
27-
return go.FigureWidget(
28-
data=[
29-
go.Scattergl(
30-
x=x,
31-
y=y,
32-
mode="markers",
33-
marker=dict(color="rgba(0, 0, 0, 0.05)", size=5),
34-
),
35-
go.Scattergl(
36-
x=xgrid,
37-
y=fit.intercept_ + fit.coef_[0] * xgrid,
38-
mode="lines",
39-
line=dict(color="red", width=2),
40-
),
41-
],
42-
layout={"showlegend": False},
43-
)
44-
45-
@reactive.Effect
46-
def _():
47-
scatterplot.widget.data[1].visible = input.show_fit()
48-
49-
50-
51-
app = App(app_ui, server)
17+
ui.page_opts(title="Plotly demo", fillable=True)
18+
19+
ui.input_checkbox("show_fit", "Show fitted line", value=True)
20+
21+
@render_plotly
22+
def scatterplot():
23+
return go.FigureWidget(
24+
data=[
25+
go.Scattergl(
26+
x=x,
27+
y=y,
28+
mode="markers",
29+
marker=dict(color="rgba(0, 0, 0, 0.05)", size=5),
30+
),
31+
go.Scattergl(
32+
x=xgrid,
33+
y=fit.intercept_ + fit.coef_[0] * xgrid,
34+
mode="lines",
35+
line=dict(color="red", width=2),
36+
),
37+
],
38+
layout={"showlegend": False},
39+
)
40+
41+
42+
@reactive.Effect
43+
def _():
44+
scatterplot.widget.data[1].visible = input.show_fit()

examples/pydeck/app.py

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,37 @@
11
import pydeck as pdk
2-
from shiny import *
3-
4-
from shinywidgets import *
5-
6-
app_ui = ui.page_fillable(
7-
ui.input_slider("zoom", "Zoom", 0, 20, 6, step=1),
8-
output_widget("deckmap")
9-
)
10-
11-
def server(input: Inputs, output: Outputs, session: Session):
12-
13-
@output
14-
@render_widget
15-
def deckmap():
16-
17-
UK_ACCIDENTS_DATA = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv"
18-
19-
layer = pdk.Layer(
20-
"HexagonLayer", # `type` positional argument is here
21-
UK_ACCIDENTS_DATA,
22-
get_position=["lng", "lat"],
23-
auto_highlight=True,
24-
elevation_scale=50,
25-
pickable=True,
26-
elevation_range=[0, 3000],
27-
extruded=True,
28-
coverage=1,
29-
)
30-
31-
view_state = pdk.ViewState(
32-
longitude=-1.415,
33-
latitude=52.2323,
34-
zoom=6,
35-
min_zoom=5,
36-
max_zoom=15,
37-
pitch=40.5,
38-
bearing=-27.36,
39-
)
40-
41-
return pdk.Deck(layers=[layer], initial_view_state=view_state)
42-
43-
@reactive.Effect()
44-
def _():
45-
deckmap.value.initial_view_state.zoom = input.zoom()
46-
deckmap.value.update()
47-
48-
49-
app = App(app_ui, server)
2+
from shiny import reactive
3+
from shiny.express import input, ui
4+
5+
from shinywidgets import render_pydeck
6+
7+
ui.input_slider("zoom", "Zoom", 0, 20, 6, step=1)
8+
9+
@render_pydeck
10+
def deckmap():
11+
UK_ACCIDENTS_DATA = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv"
12+
layer = pdk.Layer(
13+
"HexagonLayer",
14+
UK_ACCIDENTS_DATA,
15+
get_position=["lng", "lat"],
16+
auto_highlight=True,
17+
elevation_scale=50,
18+
pickable=True,
19+
elevation_range=[0, 3000],
20+
extruded=True,
21+
coverage=1,
22+
)
23+
view_state = pdk.ViewState(
24+
longitude=-1.415,
25+
latitude=52.2323,
26+
zoom=6,
27+
min_zoom=5,
28+
max_zoom=15,
29+
pitch=40.5,
30+
bearing=-27.36,
31+
)
32+
return pdk.Deck(layers=[layer], initial_view_state=view_state)
33+
34+
@reactive.effect()
35+
def _():
36+
deckmap.value.initial_view_state.zoom = input.zoom()
37+
deckmap.value.update()

0 commit comments

Comments
 (0)