Skip to content

Commit 3b4a5a4

Browse files
committed
Add height/width params; rename to markdown_stream_ui()
1 parent 1d17a35 commit 3b4a5a4

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

shiny/api-examples/MarkdownStream/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
app_ui = ui.page_fluid(
88
ui.card(
99
ui.card_header("Shiny's README.md"),
10-
ui.output_markdown_stream("shiny-readme"),
10+
ui.markdown_stream_ui("shiny-readme"),
1111
height="400px",
1212
class_="mt-3",
1313
full_screen=True,

shiny/ui/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
from ._layout import layout_column_wrap
110110
from ._layout_columns import layout_columns
111111
from ._markdown import markdown
112-
from ._markdown_stream import MarkdownStream, output_markdown_stream
112+
from ._markdown_stream import MarkdownStream, markdown_stream_ui
113113
from ._modal import modal, modal_button, modal_remove, modal_show
114114
from ._navs import (
115115
nav_control,
@@ -274,7 +274,7 @@
274274
# _markdown
275275
"markdown",
276276
# _markdown_stream
277-
"output_markdown_stream",
277+
"markdown_stream_ui",
278278
"MarkdownStream",
279279
# _modal
280280
"modal_button",

shiny/ui/_markdown_stream.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from contextlib import contextmanager
22
from typing import Iterable, Literal, Union
33

4+
from htmltools import css
5+
46
from .. import reactive
57
from .._docstring import add_example
68
from .._typing_extensions import TypedDict
79
from ..session import require_active_session
810
from ..types import NotifyException
11+
from ..ui.css import CssUnit, as_css_unit
912
from . import Tag
1013
from ._html_deps_py_shiny import markdown_stream_dependency
1114

1215
__all__ = (
13-
"output_markdown_stream",
16+
"markdown_stream_ui",
1417
"MarkdownStream",
1518
)
1619

@@ -42,14 +45,6 @@ class MarkdownStream:
4245
----------
4346
id
4447
A unique identifier for this markdown stream.
45-
content
46-
Some content to display before any streaming occurs.
47-
content_type
48-
The content type. Default is "markdown" (specifically, CommonMark).
49-
Other supported options are:
50-
- `"html"`: for rendering HTML content.
51-
- `"text"`: for plain text.
52-
- `"semi-markdown"`: for rendering markdown, but with HTML tags escaped.
5348
on_error
5449
How to handle errors that occur while streaming. When `"unhandled"`,
5550
the app will stop running when an error occurs. Otherwise, a notification
@@ -71,14 +66,9 @@ def __init__(
7166
self,
7267
id: str,
7368
*,
74-
content: str = "",
75-
content_type: StreamingContentType = "markdown",
7669
on_error: Literal["auto", "actual", "sanitize", "unhandled"] = "auto",
7770
):
7871
self.id = id
79-
self._content = content
80-
self._content_type: StreamingContentType = content_type
81-
8272
# TODO: remove the `None` when this PR lands:
8373
# https://github.com/posit-dev/py-shiny/pull/793/files
8474
self._session = require_active_session(None)
@@ -92,20 +82,48 @@ def __init__(
9282

9383
self.on_error = on_error
9484

95-
def ui(self) -> Tag:
85+
def ui(
86+
self,
87+
*,
88+
content: str = "",
89+
content_type: StreamingContentType = "markdown",
90+
width: CssUnit = "100%",
91+
height: CssUnit = "auto",
92+
) -> Tag:
9693
"""
9794
Get the UI element for this markdown stream.
9895
9996
This method is only relevant for Shiny Express. In Shiny Core, use
10097
:func:`~shiny.ui.output_markdown_stream` for placing the markdown stream
10198
in the UI.
10299
100+
Parameters
101+
----------
102+
content
103+
Some content to display before any streaming occurs.
104+
content_type
105+
The content type. Default is "markdown" (specifically, CommonMark).
106+
Other supported options are:
107+
- `"html"`: for rendering HTML content.
108+
- `"text"`: for plain text.
109+
- `"semi-markdown"`: for rendering markdown, but with HTML tags escaped.
110+
width
111+
The width of the markdown stream container.
112+
height
113+
The height of the markdown stream container.
114+
103115
Returns
104116
-------
105117
Tag
106118
The UI element for this markdown stream.
107119
"""
108-
return output_markdown_stream(self.id, self._content, self._content_type)
120+
return markdown_stream_ui(
121+
self.id,
122+
content=content,
123+
content_type=content_type,
124+
width=width,
125+
height=height,
126+
)
109127

110128
def stream(self, content: Iterable[str], clear: bool = True):
111129
"""
@@ -191,10 +209,13 @@ def _send_custom_message(self, msg: Union[ContentMessage, isStreamingMessage]):
191209

192210

193211
@add_example()
194-
def output_markdown_stream(
212+
def markdown_stream_ui(
195213
id: str,
214+
*,
196215
content: str = "",
197216
content_type: StreamingContentType = "markdown",
217+
width: CssUnit = "100%",
218+
height: CssUnit = "auto",
198219
) -> Tag:
199220
"""
200221
Create a UI element for a markdown stream.
@@ -214,10 +235,20 @@ def output_markdown_stream(
214235
- `"html"`: for rendering HTML content.
215236
- `"text"`: for plain text.
216237
- `"semi-markdown"`: for rendering markdown, but with HTML tags escaped.
238+
width
239+
The width of the markdown stream container.
240+
height
241+
The height of the markdown stream container.
217242
"""
218243
return Tag(
219244
"shiny-markdown-stream",
220245
markdown_stream_dependency(),
246+
{
247+
"style": css(
248+
width=as_css_unit(width),
249+
height=as_css_unit(height),
250+
)
251+
},
221252
id=id,
222253
content=content,
223254
content_type=content_type,

0 commit comments

Comments
 (0)