Skip to content

Commit 1a8f1ff

Browse files
committed
Add render.code
1 parent a4ab950 commit 1a8f1ff

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

shiny/render/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
display,
1616
)
1717
from ._render import (
18+
code,
1819
image,
1920
plot,
2021
table,
@@ -28,6 +29,7 @@
2829
"data_frame",
2930
"display",
3031
"text",
32+
"code",
3133
"plot",
3234
"image",
3335
"table",

shiny/render/_render.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Can use `dict` in python >= 3.9
1010
from typing import (
1111
TYPE_CHECKING,
12+
Any,
1213
Callable,
1314
Literal,
1415
Optional,
@@ -46,6 +47,7 @@
4647

4748
__all__ = (
4849
"text",
50+
"code",
4951
"plot",
5052
"image",
5153
"table",
@@ -61,6 +63,17 @@ class text(Renderer[str]):
6163
"""
6264
Reactively render text.
6365
66+
When used in Shiny Express applications, this defaults to displaying the text as
67+
normal text on the web page. When used in Shiny Core applications, this should be
68+
paired with :func:`~shiny.ui.output_text` in the UI.
69+
70+
71+
Parameters
72+
----------
73+
inline
74+
Used in Shiny Express only. If ``True``, the result is displayed inline. (This
75+
argument is passed to :func:`~shiny.ui.output_text`.)
76+
6477
Returns
6578
-------
6679
:
@@ -74,14 +87,92 @@ class text(Renderer[str]):
7487
7588
See Also
7689
--------
90+
~shiny.render.code
7791
~shiny.ui.output_text
7892
"""
7993

80-
def default_ui(self, id: str, placeholder: bool | MISSING_TYPE = MISSING) -> Tag:
94+
def default_ui(
95+
self,
96+
id: str,
97+
*,
98+
inline: bool | MISSING_TYPE = MISSING,
99+
) -> Tag:
100+
kwargs: dict[str, Any] = {}
101+
set_kwargs_value(kwargs, "inline", inline, self.inline)
102+
103+
return _ui.output_text(id, **kwargs)
104+
105+
def __init__(
106+
self,
107+
_fn: Optional[ValueFn[str]] = None,
108+
*,
109+
inline: bool = False,
110+
) -> None:
111+
super().__init__(_fn)
112+
self.inline = inline
113+
114+
async def transform(self, value: str) -> Jsonifiable:
115+
return str(value)
116+
117+
118+
# ======================================================================================
119+
# RenderCode
120+
# ======================================================================================
121+
122+
123+
class code(Renderer[str]):
124+
"""
125+
Reactively render text as code (monospaced).
126+
127+
When used in Shiny Express applications, this defaults to displaying the text in a
128+
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.
130+
131+
Parameters
132+
----------
133+
placeholder
134+
Used in Shiny Express only. If the output is empty or ``None``, should an empty
135+
rectangle be displayed to serve as a placeholder? This does not affect behavior
136+
when the output is nonempty. (This argument is passed to
137+
:func:`~shiny.ui.output_text_verbatim`.)
138+
139+
140+
Returns
141+
-------
142+
:
143+
A decorator for a function that returns a string.
144+
145+
Tip
146+
----
147+
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).
150+
151+
See Also
152+
--------
153+
~shiny.render.text
154+
~shiny.ui.output_text_verbatim
155+
"""
156+
157+
def default_ui(
158+
self,
159+
id: str,
160+
*,
161+
placeholder: bool | MISSING_TYPE = MISSING,
162+
) -> Tag:
81163
kwargs: dict[str, bool] = {}
82-
set_kwargs_value(kwargs, "placeholder", placeholder, None)
164+
set_kwargs_value(kwargs, "placeholder", placeholder, self.placeholder)
83165
return _ui.output_text_verbatim(id, **kwargs)
84166

167+
def __init__(
168+
self,
169+
_fn: Optional[ValueFn[str]] = None,
170+
*,
171+
placeholder: bool = False,
172+
) -> None:
173+
super().__init__(_fn)
174+
self.placeholder = placeholder
175+
85176
async def transform(self, value: str) -> Jsonifiable:
86177
return str(value)
87178

0 commit comments

Comments
 (0)