-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expressify reactive examples (#1078)
Co-authored-by: Garrick Aden-Buie <garrick@adenbuie.com>
- Loading branch information
Showing
10 changed files
with
159 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import asyncio | ||
|
||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_action_button("button", "Compute") | ||
|
||
|
||
@render.text | ||
@reactive.event(input.button) | ||
async def compute(): | ||
with ui.Progress(min=1, max=15) as p: | ||
p.set(message="Calculation in progress", detail="This may take a while...") | ||
|
||
for i in range(1, 15): | ||
p.set(i, message="Computing") | ||
await asyncio.sleep(0.1) | ||
# Normally use time.sleep() instead, but it doesn't yet work in Pyodide. | ||
# https://github.com/pyodide/pyodide/issues/2354 | ||
|
||
return "Done computing!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
val = reactive.Value(0) | ||
|
||
|
||
@reactive.effect | ||
@reactive.event(input.minus) | ||
def _(): | ||
newVal = val.get() - 1 | ||
val.set(newVal) | ||
|
||
|
||
@reactive.effect | ||
@reactive.event(input.plus) | ||
def _(): | ||
newVal = val.get() + 1 | ||
val.set(newVal) | ||
|
||
|
||
with ui.sidebar(): | ||
ui.input_action_button("minus", "-1") | ||
ui.input_action_button("plus", "+1") | ||
|
||
|
||
@render.text | ||
def value(): | ||
return str(val.get()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import random | ||
import time | ||
|
||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
|
||
@reactive.calc | ||
def first(): | ||
input.first() | ||
p = ui.Progress() | ||
for i in range(30): | ||
p.set(i / 30, message="Computing, please wait...") | ||
time.sleep(0.1) | ||
p.close() | ||
return random.randint(1, 1000) | ||
|
||
|
||
@reactive.calc | ||
def second(): | ||
input.second() | ||
return random.randint(1, 1000) | ||
|
||
|
||
with ui.card(): | ||
with ui.layout_columns(): | ||
ui.input_action_button("first", "Invalidate first (slow) computation") | ||
ui.input_action_button("second", "Invalidate second (fast) computation") | ||
|
||
@render.text | ||
def result(): | ||
return first() + second() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from shiny import reactive | ||
from shiny.express import input, ui | ||
|
||
ui.input_action_button("show", "Show modal dialog") | ||
|
||
|
||
@reactive.effect | ||
@reactive.event(input.show) | ||
def show_important_message(): | ||
m = ui.modal( | ||
"This is a somewhat important message.", | ||
easy_close=True, | ||
footer=None, | ||
) | ||
ui.modal_show(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import random | ||
|
||
from shiny import reactive | ||
from shiny.express import render | ||
|
||
|
||
@render.text | ||
def value(): | ||
reactive.invalidate_later(0.5) | ||
return "Random int: " + str(random.randint(0, 10000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from shiny import reactive | ||
from shiny.express import input, render, ui | ||
|
||
ui.input_slider("n", "Number of observations", min=0, max=1000, value=500) | ||
ui.input_action_button("go", "Go!", class_="btn-success") | ||
|
||
|
||
@render.plot(alt="A histogram") | ||
def plot(): | ||
# Take a reactive dependency on the action button... | ||
input.go() | ||
|
||
# ...but don't take a reactive dependency on the slider | ||
with reactive.isolate(): | ||
np.random.seed(19680801) | ||
x = 100 + 15 * np.random.randn(input.n()) | ||
|
||
fig, ax = plt.subplots() | ||
ax.hist(x, bins=30, density=True) | ||
return fig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters