Skip to content

Commit 4abf8f7

Browse files
Gordon Shotwellgadenbuie
andauthored
Expressify reactive examples (#1078)
Co-authored-by: Garrick Aden-Buie <[email protected]>
1 parent f9c9ddd commit 4abf8f7

File tree

10 files changed

+159
-35
lines changed

10 files changed

+159
-35
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
3+
from shiny import reactive
4+
from shiny.express import input, render, ui
5+
6+
ui.input_action_button("button", "Compute")
7+
8+
9+
@render.text
10+
@reactive.event(input.button)
11+
async def compute():
12+
with ui.Progress(min=1, max=15) as p:
13+
p.set(message="Calculation in progress", detail="This may take a while...")
14+
15+
for i in range(1, 15):
16+
p.set(i, message="Computing")
17+
await asyncio.sleep(0.1)
18+
# Normally use time.sleep() instead, but it doesn't yet work in Pyodide.
19+
# https://github.com/pyodide/pyodide/issues/2354
20+
21+
return "Done computing!"

shiny/api-examples/Value/app-core.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
22

3-
app_ui = ui.page_fluid(
4-
ui.input_action_button("minus", "-1"),
5-
" ",
6-
ui.input_action_button("plus", "+1"),
7-
ui.br(),
3+
app_ui = ui.page_sidebar(
4+
ui.sidebar(
5+
ui.input_action_button("minus", "-1"),
6+
ui.input_action_button("plus", "+1"),
7+
),
88
ui.output_text("value"),
99
)
1010

1111

1212
def server(input: Inputs, output: Outputs, session: Session):
1313
val = reactive.Value(0)
1414

15-
@reactive.Effect
15+
@reactive.effect
1616
@reactive.event(input.minus)
1717
def _():
1818
newVal = val.get() - 1
1919
val.set(newVal)
2020

21-
@reactive.Effect
21+
@reactive.effect
2222
@reactive.event(input.plus)
2323
def _():
2424
newVal = val.get() + 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from shiny import reactive
2+
from shiny.express import input, render, ui
3+
4+
val = reactive.Value(0)
5+
6+
7+
@reactive.effect
8+
@reactive.event(input.minus)
9+
def _():
10+
newVal = val.get() - 1
11+
val.set(newVal)
12+
13+
14+
@reactive.effect
15+
@reactive.event(input.plus)
16+
def _():
17+
newVal = val.get() + 1
18+
val.set(newVal)
19+
20+
21+
with ui.sidebar():
22+
ui.input_action_button("minus", "-1")
23+
ui.input_action_button("plus", "+1")
24+
25+
26+
@render.text
27+
def value():
28+
return str(val.get())

shiny/api-examples/calc/app-core.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
55

66
app_ui = ui.page_fluid(
7-
ui.input_action_button("first", "Invalidate first (slow) computation"),
8-
" ",
9-
ui.input_action_button("second", "Invalidate second (fast) computation"),
10-
ui.br(),
11-
ui.output_ui("result"),
7+
ui.card(
8+
ui.layout_columns(
9+
ui.input_action_button("first", "Invalidate first (slow) computation"),
10+
ui.input_action_button("second", "Invalidate second (fast) computation"),
11+
),
12+
ui.output_text_verbatim("result"),
13+
)
1214
)
1315

1416

1517
def server(input: Inputs, output: Outputs, session: Session):
16-
@reactive.Calc
18+
@reactive.calc
1719
def first():
1820
input.first()
1921
p = ui.Progress()
@@ -23,12 +25,12 @@ def first():
2325
p.close()
2426
return random.randint(1, 1000)
2527

26-
@reactive.Calc
28+
@reactive.calc
2729
def second():
2830
input.second()
2931
return random.randint(1, 1000)
3032

31-
@render.ui
33+
@render.text
3234
def result():
3335
return first() + second()
3436

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import random
2+
import time
3+
4+
from shiny import reactive
5+
from shiny.express import input, render, ui
6+
7+
8+
@reactive.calc
9+
def first():
10+
input.first()
11+
p = ui.Progress()
12+
for i in range(30):
13+
p.set(i / 30, message="Computing, please wait...")
14+
time.sleep(0.1)
15+
p.close()
16+
return random.randint(1, 1000)
17+
18+
19+
@reactive.calc
20+
def second():
21+
input.second()
22+
return random.randint(1, 1000)
23+
24+
25+
with ui.card():
26+
with ui.layout_columns():
27+
ui.input_action_button("first", "Invalidate first (slow) computation")
28+
ui.input_action_button("second", "Invalidate second (fast) computation")
29+
30+
@render.text
31+
def result():
32+
return first() + second()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui
3+
4+
ui.input_action_button("show", "Show modal dialog")
5+
6+
7+
@reactive.effect
8+
@reactive.event(input.show)
9+
def show_important_message():
10+
m = ui.modal(
11+
"This is a somewhat important message.",
12+
easy_close=True,
13+
footer=None,
14+
)
15+
ui.modal_show(m)

shiny/api-examples/invalidate_later/app-core.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
44

5-
app_ui = ui.page_fluid(ui.output_ui("value"))
5+
app_ui = ui.page_fluid(ui.output_text("value"))
66

77

88
def server(input: Inputs, output: Outputs, session: Session):
9-
@reactive.Effect
10-
def _():
11-
reactive.invalidate_later(0.5)
12-
print("Random int: ", random.randint(0, 10000))
13-
14-
@render.ui
9+
@render.text
1510
def value():
1611
reactive.invalidate_later(0.5)
1712
return "Random int: " + str(random.randint(0, 10000))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import random
2+
3+
from shiny import reactive
4+
from shiny.express import render
5+
6+
7+
@render.text
8+
def value():
9+
reactive.invalidate_later(0.5)
10+
return "Random int: " + str(random.randint(0, 10000))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
from shiny import reactive
5+
from shiny.express import input, render, ui
6+
7+
ui.input_slider("n", "Number of observations", min=0, max=1000, value=500)
8+
ui.input_action_button("go", "Go!", class_="btn-success")
9+
10+
11+
@render.plot(alt="A histogram")
12+
def plot():
13+
# Take a reactive dependency on the action button...
14+
input.go()
15+
16+
# ...but don't take a reactive dependency on the slider
17+
with reactive.isolate():
18+
np.random.seed(19680801)
19+
x = 100 + 15 * np.random.randn(input.n())
20+
21+
fig, ax = plt.subplots()
22+
ax.hist(x, bins=30, density=True)
23+
return fig

shiny/api-examples/poll/app-core.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ def stock_quotes() -> pd.DataFrame:
8989
# === Define the Shiny UI and server ===============================
9090

9191
app_ui = ui.page_fluid(
92-
ui.row(
93-
ui.column(
94-
8,
92+
ui.layout_columns(
93+
ui.card(
9594
ui.markdown(
9695
"""
9796
# `shiny.reactive.poll` demo
@@ -100,11 +99,14 @@ def stock_quotes() -> pd.DataFrame:
10099
case, an in-memory sqlite3) with the help of `shiny.reactive.poll`.
101100
"""
102101
),
103-
class_="mb-3",
102+
ui.input_selectize(
103+
"symbols", "Filter by symbol", [""] + SYMBOLS, multiple=True
104+
),
105+
ui.output_data_frame("table"),
106+
fill=False,
104107
),
105-
),
106-
ui.input_selectize("symbols", "Filter by symbol", [""] + SYMBOLS, multiple=True),
107-
ui.output_ui("table"),
108+
col_widths=[8, 4],
109+
)
108110
)
109111

110112

@@ -115,13 +117,9 @@ def filtered_quotes():
115117
df = df[df["symbol"].isin(input.symbols())]
116118
return df
117119

118-
@render.ui
120+
@render.data_frame
119121
def table():
120-
return ui.HTML(
121-
filtered_quotes().to_html(
122-
index=False, classes="table font-monospace w-auto"
123-
)
124-
)
122+
return filtered_quotes()
125123

126124

127125
app = App(app_ui, server)

0 commit comments

Comments
 (0)