Skip to content

Commit 648945b

Browse files
committed
Update readme and examples to use latest API
1 parent 872a58e commit 648945b

File tree

9 files changed

+73
-71
lines changed

9 files changed

+73
-71
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pip install ipyshiny --extra-index-url=https://pyshiny.netlify.app/pypi
1414

1515
Every Shiny app has two main parts: the user interface (UI) and server logic.
1616
`{ipyshiny}` provides `output_widget()` for defining where to place a widget in the UI
17-
and `register_widget()` (or `@render_widget()`) for supplying a widget-like object to
17+
and `register_widget()` (or `@render_widget`) for supplying a widget-like object to
1818
the `output_widget()` container. More technically, widget-like means:
1919

2020
* Any object that subclasses `{ipywidgets}`'s `Widget` class.
@@ -30,7 +30,7 @@ The recommended way to incorporate `{ipyshiny}` widgets into Shiny apps is to:
3030
the `server` function first executes), but if the widget is slow to initialize and
3131
doesn't need to be shown right away, you may want to delay that initialization
3232
until it's needed.
33-
2. Use Shiny's `@reactive.Effect()` to reactively modify the widget whenever relevant
33+
2. Use Shiny's `@reactive.Effect` to reactively modify the widget whenever relevant
3434
reactive values change.
3535
3. Use `{ipyshiny}`'s `reactive_read()` to update other outputs whenever the widget changes.
3636
* This way, relevant output(s) invalidate (i.e., recalculate) whenever the relevant
@@ -56,19 +56,19 @@ def server(input, output, session):
5656
register_widget("map", map)
5757

5858
# When the slider changes, update the map's zoom attribute (2)
59-
@reactive.Effect()
59+
@reactive.Effect
6060
def _():
6161
map.zoom = input.zoom()
6262

6363
# When zooming directly on the map, update the slider's value (2 and 3)
64-
@reactive.Effect()
64+
@reactive.Effect
6565
def _():
6666
ui.update_slider("zoom", value=reactive_read(map, "zoom"))
6767

6868
# Everytime the map's bounds change, update the output message (3)
69-
@output(name="map_bounds")
70-
@render_text()
71-
def _():
69+
@output
70+
@render.text
71+
def map_bounds():
7272
b = reactive_read(map, "bounds")
7373
lat = [b[0][0], b[0][1]]
7474
lon = [b[1][0], b[1][1]]
@@ -125,7 +125,7 @@ def server(input, output, session):
125125

126126
register_widget("scatterplot", scatterplot)
127127

128-
@reactive.Effect()
128+
@reactive.Effect
129129
def _():
130130
scatterplot.data[1].visible = input.show_fit()
131131

@@ -161,9 +161,9 @@ app_ui = ui.page_fluid(
161161
)
162162

163163
def server(input, output, session):
164-
@output(name="map")
165-
@render_widget()
166-
def _():
164+
@output
165+
@render_widget
166+
def map():
167167
return L.Map(center=(52, 360), zoom=input.zoom())
168168

169169
app = App(app_ui, server)

examples/ipyleaflet/app.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,38 @@
77
ui.div(
88
ui.input_slider("zoom", "Map zoom level", value=4, min=1, max=10),
99
ui.output_text("map_bounds"),
10-
style=css(display="flex", justify_content="center", align_items="center", gap="2rem"),
10+
style=css(
11+
display="flex", justify_content="center", align_items="center", gap="2rem"
12+
),
1113
),
12-
output_widget("map")
14+
output_widget("map"),
1315
)
1416

17+
1518
def server(input, output, session):
1619

1720
# Initialize and display when the session starts (1)
1821
map = L.Map(center=(52, 360), zoom=4)
1922
register_widget("map", map)
2023

2124
# When the slider changes, update the map's zoom attribute (2)
22-
@reactive.Effect()
25+
@reactive.Effect
2326
def _():
2427
map.zoom = input.zoom()
2528

2629
# When zooming directly on the map, update the slider's value (2 and 3)
27-
@reactive.Effect()
30+
@reactive.Effect
2831
def _():
2932
ui.update_slider("zoom", value=reactive_read(map, "zoom"))
3033

3134
# Everytime the map's bounds change, update the output message (3)
32-
@output(name="map_bounds")
33-
@render_text()
34-
def _():
35+
@output
36+
@render.text
37+
def map_bounds():
3538
b = reactive_read(map, "bounds")
3639
lat = [b[0][0], b[0][1]]
3740
lon = [b[1][0], b[1][1]]
3841
return f"The current latitude is {lat} and longitude is {lon}"
3942

43+
4044
app = App(app_ui, server)

examples/ipywidgets/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def server(input: Inputs, output: Outputs, session: Session):
2323

2424
register_widget("slider", s)
2525

26-
@reactive.Effect()
26+
@reactive.Effect
2727
def _():
2828
return f"The value of the slider is: {reactive_read(s, 'value')}"
2929

examples/ipywidgets/requirements.txt

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

examples/ipywidgets/rsconnect-python/ipywidgets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"https://connect.rstudioservices.com/": {
33
"server_url": "https://connect.rstudioservices.com/",
44
"filename": "/Users/cpsievert/github/ipyshiny/examples/ipywidgets",
5-
"app_url": "https://connect.rstudioservices.com/content/346038e1-cbb2-407a-b0ba-91f421c128df/",
6-
"app_id": 8699,
5+
"app_url": "https://connect.rstudioservices.com/connect/#/apps/346038e1-cbb2-407a-b0ba-91f421c128df",
6+
"app_id": "346038e1-cbb2-407a-b0ba-91f421c128df",
77
"app_guid": "346038e1-cbb2-407a-b0ba-91f421c128df",
88
"title": "ipywidgets",
99
"app_mode": "python-shiny"

examples/outputs/app.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@
5353

5454

5555
def server(input: Inputs, output: Outputs, session: Session):
56-
@output(name="figure")
57-
@render_ui()
56+
@output(id="figure")
57+
@render.ui
5858
def _():
5959
return output_widget(input.framework())
6060

61-
@output(name="ipyleaflet")
62-
@render_widget()
61+
@output(id="ipyleaflet")
62+
@render_widget
6363
def _():
6464
from ipyleaflet import Map, Marker
6565

6666
m = Map(center=(52.204793, 360.121558), zoom=4)
6767
m.add_layer(Marker(location=(52.204793, 360.121558)))
6868
return m
6969

70-
@output(name="qgrid")
71-
@render_widget()
70+
@output(id="qgrid")
71+
@render_widget
7272
def _():
7373
import qgrid
7474

@@ -120,8 +120,8 @@ def _():
120120
df_types["E"] = df_types["D"] == "foo"
121121
return qgrid.show_grid(df_types, show_toolbar=True)
122122

123-
@output(name="altair")
124-
@render_widget()
123+
@output(id="altair")
124+
@render_widget
125125
def _():
126126
import altair as alt
127127
from vega_datasets import data
@@ -136,8 +136,8 @@ def _():
136136
)
137137
)
138138

139-
@output(name="plotly")
140-
@render_widget()
139+
@output(id="plotly")
140+
@render_widget
141141
def _():
142142
import plotly.graph_objects as go
143143

@@ -146,8 +146,8 @@ def _():
146146
layout_title_text="A Figure Displayed with fig.show()",
147147
)
148148

149-
@output(name="bqplot")
150-
@render_widget()
149+
@output(id="bqplot")
150+
@render_widget
151151
def _():
152152
from bqplot import OrdinalScale, LinearScale, Bars, Lines, Axis, Figure
153153

@@ -187,8 +187,8 @@ def _():
187187
],
188188
)
189189

190-
@output(name="ipychart")
191-
@render_widget()
190+
@output(id="ipychart")
191+
@render_widget
192192
def _():
193193
from ipychart import Chart
194194

@@ -208,8 +208,8 @@ def _():
208208

209209
return Chart(data=dataset, kind="bar")
210210

211-
@output(name="ipywebrtc")
212-
@render_widget()
211+
@output(id="ipywebrtc")
212+
@render_widget
213213
def _():
214214
from ipywebrtc import CameraStream
215215

@@ -221,16 +221,16 @@ def _():
221221
}
222222
)
223223

224-
@output(name="ipyvolume")
225-
@render_widget()
224+
@output(id="ipyvolume")
225+
@render_widget
226226
def _():
227227
from ipyvolume import quickquiver
228228

229229
x, y, z, u, v, w = np.random.random((6, 1000)) * 2 - 1
230230
return quickquiver(x, y, z, u, v, w, size=5)
231231

232-
@output(name="pydeck")
233-
@render_widget()
232+
@output(id="pydeck")
233+
@render_widget
234234
def _():
235235
import pydeck as pdk
236236

@@ -262,8 +262,8 @@ def _():
262262
# Combined all of it and render a viewport
263263
return pdk.Deck(layers=[layer], initial_view_state=view_state)
264264

265-
@output(name="bokeh")
266-
@render_widget()
265+
@output(id="bokeh")
266+
@render_widget
267267
def _():
268268
from bokeh.plotting import figure
269269

examples/plotly/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def server(input, output, session):
4141

4242
register_widget("scatterplot", scatterplot)
4343

44-
@reactive.Effect()
44+
@reactive.Effect
4545
def _():
4646
scatterplot.data[1].visible = input.show_fit()
4747

0 commit comments

Comments
 (0)