shiny.reactive.file_reader#

shiny.reactive.file_reader(filepath, interval_secs=1, *, priority=1, session=<shiny.types.MISSING_TYPE object>)#

Create a reactive file reader.

This is a decorator, meant to be applied to a no-argument function that reads data from a file on disk. Whenever the file changes (or to be precise, the file size or last modified time changes), past readers of the data are reactively invalidated. This makes it incredibly easy to write apps that automatically update all of their outputs as soon as files on disk change.

Note that file_reader works only on single files, not directories of files.

Both the filepath function and the decorated (file reading) function can read reactive values and ~shiny.reactive.Calc objects. Any invalidations triggered by reactive dependencies will apply to the reactive file reader object immediately (not waiting for the interval_secs delay to expire).

Parameters
  • filepath (Union[str, PathLike[str], Callable[[], str], Callable[[], PathLike[str]]]) –

    Either a string indicating the file path to be monitored, or, a no-argument function that returns such a string. The latter is useful if the file to be monitored depends on some user input, the current date/time, etc.

    The file path provided MUST exist, otherwise Shiny will treat it as an unhandled error and close the session.

    If a function is used, make sure it is high performance (or is cached, i.e. use a ~shiny.reactive.Calc), as it will be called very frequently.

  • interval_secs (float) – The number of seconds to wait after each time the file metadata is checked. Note: depending on what other tasks are executing, the actual wait time may far exceed this value.

  • equals – The function that will be used to compare each poll_func return value with its immediate predecessor.

  • priority (int) – Reactive polling is implemented using an ~shiny.reactive.Effect to call poll_func on a timer; use the priority argument to control the order of this Effect’s execution versus other Effects in your app. See ~shiny.reactive.Effect for more details.

  • session (Union[MISSING_TYPE, Session, None]) – A Session instance. If not provided, it is inferred via get_current_session(). If there is no current session (i.e. poll is being created outside of the server function), the lifetime of this reactive poll object will not be tied to any specific session.

Return type

Callable[[Callable[[], ~T]], Callable[[], ~T]]

Returns

  • A decorator that should be applied to a no-argument function that (expensively)

  • reads whatever data is desired. (This function may be a regular function or a

  • coroutine function.) The result of the decorator is a reactive ~shiny.reactive.Calc

  • that always returns up-to-date data, and invalidates callers when changes are

  • detected via polling.

See also

poll

Example

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
import pathlib

import pandas as pd
from shiny import *

dir = pathlib.Path(__file__).parent

app_ui = ui.page_fluid(ui.output_table("result"), class_="p-3")

@reactive.file_reader(dir / "mtcars.csv")
def read_file():
    return pd.read_csv(dir / "mtcars.csv")


def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.table
    def result():
        return read_file()


app = App(app_ui, server)