Skip to content

Commit

Permalink
Port 202-reactive-event to shiny express
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Jul 31, 2024
1 parent 859804b commit dc1fb77
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
16 changes: 0 additions & 16 deletions apps/core/2-reactivity/2.3-reactive-event/app.py

This file was deleted.

File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions docs/apps/202-reactive-event/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from shiny.express import ui, render, input
from shiny import reactive

ui.input_text("input_txt", "Enter text")
ui.input_action_button("send", "Enter")
@render.text
@reactive.event(input.send)
def output_txt():
return input.input_txt()


31 changes: 19 additions & 12 deletions docs/reactivity-slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ flowchart TD
- So far we've been working with shallow reactive graphs
- Each input is passed to a rendering function which produces an output
- Input -> Recipe -> Output can produce repetitive, inefficient applications
- `@reactive.Calc` creates calculations whose results can be used by one _or more_ outputs
- `@reactive.calc` creates calculations whose results can be used by one _or more_ outputs
- This adds intermediate nodes to the reactive graph

## Reactive Calc example
Expand Down Expand Up @@ -474,7 +474,7 @@ include_shiny_folder(

## Reactive Calculation to the rescue
```{.python code-line-numbers="1-4,9,15"}
@reactive.Calc
@reactive.calc
def sampled_df():
rand = np.random.rand(input.n_rows(), 1)
df = pd.DataFrame(rand, columns=["col_1"])
Expand All @@ -496,7 +496,7 @@ include_shiny_folder(
```

## Reactive calculations
- Defined with the `@reactive.Calc` decorator
- Defined with the `@reactive.calc` decorator
- Called like other inputs
- Can read inputs, reactive values, or other reactive calculations
- Caches its value, so it's cheap to call repeatedly
Expand Down Expand Up @@ -746,9 +746,9 @@ flowchart TD
# | echo: false
# | output: asis
include_shiny_folder(
"../apps/core/2-reactivity/2.3-reactive-event",
file_name="app-solution.py",
exclusions=["app.py"],
"apps/202-reactive-event",
file_name="app.py",
# exclusions=["app.py"],
components="viewer",
viewer_height=700,
)
Expand All @@ -757,15 +757,22 @@ include_shiny_folder(
## reactive.event

```{.python}
@output
from shiny.express import ui, render, input
from shiny import reactive
ui.input_text("input_txt", "Enter text")
ui.input_action_button("send", "Enter")
@render.text
@reactive.event(input.my_input)
def txt():
return "Here is my text"
@reactive.event(input.send)
def output_txt():
return input.input_txt()
```

- `@reactive.event` overrides the usual _implicit_ dependency detection with an _explicit_ trigger
- It can be applied to rendering functions or to `@reactive.Calc`
- It can be applied to rendering functions or to `@reactive.calc`
- It tells Shiny to invalidate the object whenever or more inputs change
- `@reactive.event` is often used with action buttons or action links

Expand All @@ -780,7 +787,7 @@ def txt():

- How Shiny re-renders elements
- How Shiny detects dependencies between elements (inputs and outputs)
- How to create reusable calculations with `@reactive.Calc`
- How to create reusable calculations with `@reactive.calc`
- How to explicitly control reactivity with `@reactive.event`

## Is that enough?
Expand Down

0 comments on commit dc1fb77

Please sign in to comment.