11from  contextlib  import  contextmanager 
22from  typing  import  Iterable , Literal , Union 
33
4+ from  htmltools  import  css 
5+ 
46from  .. import  reactive 
57from  .._docstring  import  add_example 
68from  .._typing_extensions  import  TypedDict 
79from  ..session  import  require_active_session 
810from  ..types  import  NotifyException 
11+ from  ..ui .css  import  CssUnit , as_css_unit 
912from  . import  Tag 
1013from  ._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