ui.insert_ui

ui.insert_ui(ui, selector, where='beforeEnd', multiple=False, immediate=False, session=None)

Insert UI objects.

Parameters

ui: TagChild

The UI object you want to insert. This can be anything that you usually put inside your app’s UI function. If you’re inserting multiple elements in one call, make sure to wrap them in either a TagList or a div (the latter option has the advantage that you can give it an id to make it easier to reference or remove it later on). If you want to insert raw HTML, use HTML.

selector: str

A string that is accepted by jQuery’s selector (i.e. the string s to be placed in a $(s) jQuery call) which determines the element(s) relative to which you want to insert your UI object.

where: Literal[‘beforeBegin’, ‘afterBegin’, ‘beforeEnd’, ‘afterEnd’] = ‘beforeEnd’

Where your UI object should go relative to the selector: “beforeBegin”: before the selector element itself; “beforeEnd”: just inside the selector element, after its last child (default); “afterEnd”: after the selector element itself. Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML.

multiple: bool = False

In case your selector matches more than one element, multiple determines whether Shiny should insert the UI object relative to all matched elements or just relative to the first matched element (default).

immediate: bool = False

Whether the UI object should be immediately inserted or removed, or whether Shiny should wait until all outputs have been updated and all effects have been run (default).

session: Optional[Session] = None

A Session instance. If not provided, it is inferred via get_current_session.

Note

This function allows you to dynamically add arbitrary UI into your app, whenever you want, as many times as you want. Unlike ui, the UI generated with insert_ui is persistent: once it’s created, it stays there until removed by remove_ui. Each new call to insert_ui creates more UI objects, in addition to the ones already there (all independent from one another). To update a part of the UI (ex: an input object), you must use the appropriate render function or a customized reactive function.

See Also

Examples

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

## file: app.py
from shiny import App, Inputs, Outputs, Session, reactive, ui

app_ui = ui.page_fluid(
    ui.input_action_button("add", "Add UI"),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.effect
    @reactive.event(input.add)
    def _():
        ui.insert_ui(
            ui.input_text("txt" + str(input.add()), "Enter some text"),
            selector="#add",
            where="afterEnd",
        )


app = App(app_ui, server)