Text
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
from shiny import App, render, ui
app_ui = ui.page_fluid(
ui.input_text("Text", "Enter text", "Hello Shiny"),
"You entered:",
ui.output_text_verbatim("text"),
)
def server(input, output, session):
@render.text
def text():
return input.Text()
app = App(app_ui, server)
Relevant Functions
-
ui.output_text
ui.output_text(id, inline=False, container=None)
-
@render.text
render.text(fn=None)
Details
Text displays a character string as normal text.
To make reactive text, follow three steps:
Call
ui.output_text()
in the UI of your app to create a div in which to display the text. Where you call this function within the UI functions will determine where the text will appear within the layout of the app. Set the id argument ofui.output_text()
to a unique value.Within the server function, define a new function whose name matches the id used above. The function should return the text to display. Shiny will rerun this function whenever it needs to build or update the output that has the matching id.
Decorate the function with
@render.text
See Verbatim Text to display string values as they would appear in a computer console, in monospaced font on a shaded background.
Variations
Inline text
Set inline=True
within ui.output_text()
to have text appear inline with the text that preceeds it.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 300
from shiny import App, render, ui
app_ui = ui.page_fluid(
ui.input_text("Text", "Enter text", "Hello Shiny"),
"You entered: ",
ui.output_text("text", inline=True),
)
def server(input, output, session):
@render.text
def text():
return input.Text()
app = App(app_ui, server)