ui.update_code_editor

ui.update_code_editor(
    id,
    *,
    label=None,
    value=None,
    language=None,
    theme_light=None,
    theme_dark=None,
    read_only=None,
    line_numbers=None,
    word_wrap=None,
    tab_size=None,
    indentation=None,
    session=None,
)

Update a code editor input on the client.

Parameters

id : str

The input ID.

label : Optional[TagChild] = None

An input label.

value : Optional[str | Sequence[str]] = None

New code content. Can be a string or sequence of strings (lines).

language : Optional[CodeEditorLanguage] = None

New programming language for syntax highlighting.

theme_light : Optional[str] = None

New theme for light mode.

theme_dark : Optional[str] = None

New theme for dark mode.

read_only : Optional[bool] = None

Whether the editor should be read-only.

line_numbers : Optional[bool] = None

Whether to show line numbers.

word_wrap : Optional[bool] = None

Whether to wrap long lines.

tab_size : Optional[int] = None

Number of spaces per tab.

indentation : Optional[CodeEditorIndentation] = None

Type of indentation: "space" or "tab".

session : Optional[Session] = None

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

Note

The input updater functions send a message to the client, telling it to change the settings of an input object. The messages are collected and sent after all the observers (including outputs) have finished running.

The syntax of these functions is similar to the functions that created the inputs in the first place. For example, input_code_editor and update_code_editor take a similar set of arguments.

Any arguments with None values will be ignored; they will not result in any changes to the input object on the client.

See Also

Examples

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

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

code_samples = {
    "python": "def greet(name):\n    return f'Hello, {name}!'\n\nprint(greet('World'))",
    "r": "greet <- function(name) {\n  paste0('Hello, ', name, '!')\n}\n\nprint(greet('World'))",
    "javascript": "function greet(name) {\n  return `Hello, ${name}!`;\n}\n\nconsole.log(greet('World'));",
}

app_ui = ui.page_fillable(
    ui.layout_sidebar(
        ui.sidebar(
            ui.input_select(
                "language",
                "Language:",
                choices=["python", "r", "javascript"],
                selected="python",
            ),
            ui.input_switch("read_only", "Read only", value=False),
            ui.input_switch("line_numbers", "Line numbers", value=True),
        ),
        ui.input_code_editor(
            "code",
            label="Code editor:",
            value=code_samples["python"],
            language="python",
        ),
    )
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.effect
    @reactive.event(input.language)
    def _():
        ui.update_code_editor(
            "code",
            value=code_samples[input.language()],
            language=input.language(),
        )

    @reactive.effect
    @reactive.event(input.read_only)
    def _():
        ui.update_code_editor("code", read_only=input.read_only())

    @reactive.effect
    @reactive.event(input.line_numbers)
    def _():
        ui.update_code_editor("code", line_numbers=input.line_numbers())


app = App(app_ui, server)