ui.output_code

ui.output_code(id, placeholder=True)

Create a output container for code (monospaced text).

This is similar to output_text, except that it displays the text in a fixed-width container with a gray-ish background color and border.

Parameters

id : str

An output id.

placeholder : bool = True

If the output is empty or None, should an empty rectangle be displayed to serve as a placeholder? (This does not affect behavior when the output is nonempty.)

Returns

: Tag

A UI element

Note

This function is currently the same as output_text_verbatim, but this may change in future versions of Shiny.

See Also

Examples

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

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

app_ui = ui.page_fluid(
    ui.input_text_area(
        "source",
        "Enter code to display below:",
        "print('Hello, Shiny!')\nfor i in range(3):\n    print(i)",
        rows=8,
    ),
    ui.card(
        ui.output_code("code_default"),
    ),
    ui.card(
        ui.output_code("code_no_placeholder", placeholder=False),
    ),
)


def server(input: Inputs, output: Outputs, session: Session):
    @render.code
    def code_default():
        return input.source()

    @render.code
    def code_no_placeholder():
        return input.source()


app = App(app_ui, server)