Skip to content

Commit 27cb388

Browse files
committed
Add output_code
1 parent 1a8f1ff commit 27cb388

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

docs/_quartodoc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ quartodoc:
158158
- ui.output_table
159159
- ui.output_data_frame
160160
- ui.output_text
161+
- ui.output_code
161162
- ui.output_text_verbatim
162163
- ui.output_ui
163164
- render.plot

shiny/render/_render.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ class code(Renderer[str]):
126126
127127
When used in Shiny Express applications, this defaults to displaying the text in a
128128
monospace font in a code block. When used in Shiny Core applications, this should be
129-
paired with :func:`~shiny.ui.output_text_verbatim` in the UI.
129+
paired with :func:`~shiny.ui.output_code` in the UI.
130130
131131
Parameters
132132
----------
133133
placeholder
134134
Used in Shiny Express only. If the output is empty or ``None``, should an empty
135135
rectangle be displayed to serve as a placeholder? This does not affect behavior
136136
when the output is nonempty. (This argument is passed to
137-
:func:`~shiny.ui.output_text_verbatim`.)
137+
:func:`~shiny.ui.output_code`.)
138138
139139
140140
Returns
@@ -145,13 +145,13 @@ class code(Renderer[str]):
145145
Tip
146146
----
147147
The name of the decorated function (or ``@output(id=...)``) should match the ``id``
148-
of a :func:`~shiny.ui.output_text_verbatim` container (see
149-
:func:`~shiny.ui.output_text_verbatim` for example usage).
148+
of a :func:`~shiny.ui.output_code` container (see :func:`~shiny.ui.output_code` for
149+
example usage).
150150
151151
See Also
152152
--------
153-
~shiny.render.text
154-
~shiny.ui.output_text_verbatim
153+
~shiny.render.code
154+
~shiny.ui.output_code
155155
"""
156156

157157
def default_ui(
@@ -162,7 +162,7 @@ def default_ui(
162162
) -> Tag:
163163
kwargs: dict[str, bool] = {}
164164
set_kwargs_value(kwargs, "placeholder", placeholder, self.placeholder)
165-
return _ui.output_text_verbatim(id, **kwargs)
165+
return _ui.output_code(id, **kwargs)
166166

167167
def __init__(
168168
self,

shiny/ui/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
output_plot,
111111
output_image,
112112
output_text,
113+
output_code,
113114
output_text_verbatim,
114115
output_table,
115116
output_ui,
@@ -296,6 +297,7 @@
296297
"output_plot",
297298
"output_image",
298299
"output_text",
300+
"output_code",
299301
"output_text_verbatim",
300302
"output_table",
301303
"output_ui",

shiny/ui/_output.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"output_plot",
55
"output_image",
66
"output_text",
7+
"output_code",
78
"output_text_verbatim",
89
"output_table",
910
"output_ui",
@@ -270,6 +271,47 @@ def output_text(
270271
return container(id=resolve_id(id), class_="shiny-text-output")
271272

272273

274+
def output_code(id: str, placeholder: bool = True) -> Tag:
275+
"""
276+
Create a output container for code (monospaced text).
277+
278+
This is similar to :func:`~shiny.ui.output_text`, except that it displays the text
279+
in a fixed-width container with a gray-ish background color and border.
280+
281+
Parameters
282+
----------
283+
id
284+
An output id.
285+
placeholder
286+
If the output is empty or ``None``, should an empty rectangle be displayed to
287+
serve as a placeholder? (This does not affect behavior when the output is
288+
nonempty.)
289+
290+
Returns
291+
-------
292+
:
293+
A UI element
294+
295+
Note
296+
----
297+
This function is currently the same as :func:`~shiny.ui.output_text_verbatim`, but
298+
this may change in future versions of Shiny.
299+
300+
See Also
301+
--------
302+
* :func:`~shiny.render.text`
303+
* :func:`~shiny.ui.output_text`
304+
* :func:`~shiny.ui.output_text_verbatim`
305+
306+
Example
307+
-------
308+
See :func:`~shiny.ui.output_text`
309+
"""
310+
311+
cls = "shiny-text-output" + (" noplaceholder" if not placeholder else "")
312+
return tags.pre(id=resolve_id(id), class_=cls)
313+
314+
273315
def output_text_verbatim(id: str, placeholder: bool = False) -> Tag:
274316
"""
275317
Create a output container for some text.

0 commit comments

Comments
 (0)