shiny.ui.insert_ui#

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

Insert UI objects

Parameters
  • ui (Union[Tagifiable, Tag, MetadataNode, str, TagList, float, None, Dict[str, Union[str, float, bool, None]], Sequence[Union[Tagifiable, Tag, MetadataNode, str, TagList, float, None, Dict[str, Union[str, float, bool, None]], Sequence[ForwardRef]]]]) – The UI object you want to insert. This can be anything that you usually put inside your apps’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’]) –

    Where your UI object should go relative to the selector:

    • beforeBegin: Before the selector element itself

    • afterBegin: Just inside the selector element, before its first child

    • 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) – 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) – 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]) – 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

remove_ui, ui

Example

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

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)
Return type

None