Skip to content

Commit

Permalink
Add output_code
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jan 12, 2024
1 parent 1a8f1ff commit 27cb388
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/_quartodoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ quartodoc:
- ui.output_table
- ui.output_data_frame
- ui.output_text
- ui.output_code
- ui.output_text_verbatim
- ui.output_ui
- render.plot
Expand Down
14 changes: 7 additions & 7 deletions shiny/render/_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ class code(Renderer[str]):
When used in Shiny Express applications, this defaults to displaying the text in a
monospace font in a code block. When used in Shiny Core applications, this should be
paired with :func:`~shiny.ui.output_text_verbatim` in the UI.
paired with :func:`~shiny.ui.output_code` in the UI.
Parameters
----------
placeholder
Used in Shiny Express only. 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. (This argument is passed to
:func:`~shiny.ui.output_text_verbatim`.)
:func:`~shiny.ui.output_code`.)
Returns
Expand All @@ -145,13 +145,13 @@ class code(Renderer[str]):
Tip
----
The name of the decorated function (or ``@output(id=...)``) should match the ``id``
of a :func:`~shiny.ui.output_text_verbatim` container (see
:func:`~shiny.ui.output_text_verbatim` for example usage).
of a :func:`~shiny.ui.output_code` container (see :func:`~shiny.ui.output_code` for
example usage).
See Also
--------
~shiny.render.text
~shiny.ui.output_text_verbatim
~shiny.render.code
~shiny.ui.output_code
"""

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

def __init__(
self,
Expand Down
2 changes: 2 additions & 0 deletions shiny/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
output_plot,
output_image,
output_text,
output_code,
output_text_verbatim,
output_table,
output_ui,
Expand Down Expand Up @@ -296,6 +297,7 @@
"output_plot",
"output_image",
"output_text",
"output_code",
"output_text_verbatim",
"output_table",
"output_ui",
Expand Down
42 changes: 42 additions & 0 deletions shiny/ui/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"output_plot",
"output_image",
"output_text",
"output_code",
"output_text_verbatim",
"output_table",
"output_ui",
Expand Down Expand Up @@ -270,6 +271,47 @@ def output_text(
return container(id=resolve_id(id), class_="shiny-text-output")


def output_code(id: str, placeholder: bool = True) -> Tag:
"""
Create a output container for code (monospaced text).
This is similar to :func:`~shiny.ui.output_text`, except that it displays the text
in a fixed-width container with a gray-ish background color and border.
Parameters
----------
id
An output id.
placeholder
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
-------
:
A UI element
Note
----
This function is currently the same as :func:`~shiny.ui.output_text_verbatim`, but
this may change in future versions of Shiny.
See Also
--------
* :func:`~shiny.render.text`
* :func:`~shiny.ui.output_text`
* :func:`~shiny.ui.output_text_verbatim`
Example
-------
See :func:`~shiny.ui.output_text`
"""

cls = "shiny-text-output" + (" noplaceholder" if not placeholder else "")
return tags.pre(id=resolve_id(id), class_=cls)


def output_text_verbatim(id: str, placeholder: bool = False) -> Tag:
"""
Create a output container for some text.
Expand Down

0 comments on commit 27cb388

Please sign in to comment.