shiny.reactive.isolate#

shiny.reactive.isolate()#

Create a non-reactive scope within a reactive scope.

Ordinarily, the simple act of reading a reactive value causes a relationship to be established between the caller and the reactive value, where a change to the reactive value will cause the caller to re-execute. (The same applies for the act of getting a reactive expression’s value.) with isolate() lets you read a reactive value or expression without establishing this relationship.

with isolate() can also be useful for calling reactive expression at the console, which can be useful for debugging. To do so, wrap the calls to the reactive expression with with isolate().

Returns

  • A context manager that executes the given expression in a scope where reactive

  • values can be read, but do not cause the reactive scope of the caller to be

  • re-evaluated when they change.

See also

event

Example

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
from shiny import *
import numpy as np
import matplotlib.pyplot as plt

app_ui = ui.page_fluid(
    ui.input_slider("n", "Number of observations", min=0, max=1000, value=500),
    ui.input_action_button("go", "Go!", class_="btn-success"),
    ui.output_plot("plot"),
)

def server(input: Inputs, output: Outputs, session: Session):
    @output
    @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


app = App(app_ui, server)