Ë
    úmxiûY  ã                  óz  — U d dl mZ d dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
mZmZmZmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lm Z m!Z! erd dl"m#Z# ejH                  ejJ                  ejL                  ejN                  ejP                  fZ)de*d<    G d„ de+e   «      Z, G d„ d«      Z-y)é    )ÚannotationsN)ÚChainMapÚUserDictÚUserList)ÚAsyncGeneratorÚCallableÚ	GeneratorÚ	ItemsViewÚIterableÚKeysViewÚ
ValuesView)ÚStringIO)ÚTYPE_CHECKINGÚAnyÚFinalÚcast)Údataframe_utilÚ	type_util)ÚStreamlitAPIException)Úgather_metrics)Úis_mem_address_strÚmax_char_sequence)ÚDeltaGeneratorzFinal[tuple[type[Any], ...]]Ú
HELP_TYPESc                  ó   — e Zd Zy)ÚStreamingOutputN)Ú__name__Ú
__module__Ú__qualname__© ó    úO/home/htdocs/ttos/venv/lib/python3.12/site-packages/streamlit/elements/write.pyr   r   ;   s   „ Ør!   r   c                  óp   — e Zd Z ed«      ddœ	 	 	 	 	 d
d„«       Z ed«      ddœdd„«       Zedd	„«       Zy)Ú
WriteMixinÚwrite_streamN)Úcursorc               ó   ‡	‡
‡— t        |t        «      st        j                  |«      rt	        dt        |«      › d�«      ‚|xs d}dŠ	dŠ
t        «       Šdˆ	ˆ
ˆfd„}t        j                  |«      st        j                  |«      r |«       }t        j                  |«      rt        j                  |«      }	 t        |«       |D �]0  }t        j                  |«      r\	 t!        |j"                  «      dk(  s|j"                  d   j$                  €d}n'|j"                  d   j$                  j&                  xs d}t        j*                  |d
«      r	 |j&                  xs d}t        |t        «      rA|sŒ¯d}‰	s| j,                  j/                  «       Š	d}‰
|z  Š
‰	j1                  ‰
|rdn|z   «       Œít3        |«      r |«         |«        �Œ |«        | j5                  |«       ‰j7                  |«       �Œ3  |«        ‰syt!        ‰«      dk(  rt        ‰d   t        «      r‰d   S ‰S # t        $ r}t	        dt        |«      › d�«      |‚d}~ww xY w# t(        $ r}t	        d	«      |‚d}~ww xY w# t(        $ r}t	        d«      |‚d}~ww xY w)a¸  Stream a generator, iterable, or stream-like sequence to the app.

        ``st.write_stream`` iterates through the given sequences and writes all
        chunks to the app. String chunks will be written using a typewriter effect.
        Other data types will be written using ``st.write``.

        Parameters
        ----------
        stream : Callable, Generator, Iterable, OpenAI Stream, or LangChain Stream
            The generator or iterable to stream.

            If you pass an async generator, Streamlit will internally convert
            it to a sync generator. If the generator depends on a cached object
            with async references, this can raise an error.

            .. note::
                To use additional LLM libraries, you can create a wrapper to
                manually define a generator function and include custom output
                parsing.

        cursor : str or None
            A string to append to text as it's being written. If this is
            ``None`` (default), no cursor is shown. Otherwise, the string is
            rendered as Markdown and appears as a cursor at the end of the
            streamed text. For example, you can use an emoji, emoji shortcode,
            or Material icon.

            The first line of the cursor string can contain GitHub-flavored
            Markdown of the following types: Bold, Italics, Strikethroughs,
            Inline Code, Links, and Images. Images display like icons, with a
            max height equal to the font height. If you pass a multiline
            string, additional lines display after the text with the full
            Markdown rendering capabilities of ``st.markdown``.

            See the ``body`` parameter of |st.markdown|_ for additional,
            supported Markdown directives.

            .. |st.markdown| replace:: ``st.markdown``
            .. _st.markdown: https://docs.streamlit.io/develop/api-reference/text/st.markdown


        Returns
        -------
        str or list
            The full response. If the streamed output only contains text, this
            is a string. Otherwise, this is a list of all the streamed objects.
            The return value is fully compatible as input for ``st.write``.

        Example
        -------
        You can pass an OpenAI stream as shown in our tutorial, `Build a \
        basic LLM chat app <https://docs.streamlit.io/develop/tutorials/llms\
        /build-conversational-apps#build-a-chatgpt-like-app>`_. Alternatively,
        you can pass a generic generator function as input:

        >>> import time
        >>> import numpy as np
        >>> import pandas as pd
        >>> import streamlit as st
        >>>
        >>> _LOREM_IPSUM = \"\"\"
        >>> Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do eiusmod tempor
        >>> incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
        >>> nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        >>> \"\"\"
        >>>
        >>>
        >>> def stream_data():
        >>>     for word in _LOREM_IPSUM.split(" "):
        >>>         yield word + " "
        >>>         time.sleep(0.02)
        >>>
        >>>     yield pd.DataFrame(
        >>>         np.random.randn(5, 10),
        >>>         columns=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
        >>>     )
        >>>
        >>>     for word in _LOREM_IPSUM.split(" "):
        >>>         yield word + " "
        >>>         time.sleep(0.02)
        >>>
        >>>
        >>> if st.button("Stream data"):
        >>>     st.write_stream(stream_data)

        ..  output::
            https://doc-write-stream-data.streamlit.app/
            height: 550px

        zI`st.write_stream` expects a generator or stream-like object as input not z3. Please use `st.write` instead for this data type.Ú Nc                 ó^   •— ‰r*‰ r'‰ j                  ‰«       ‰j                  ‰«       dŠ dŠyyy)z#Write the full response to the app.Nr(   )ÚmarkdownÚappend)Ústream_containerÚstreamed_responseÚwritten_contents   €€€r"   Úflush_stream_responsez6WriteMixin.write_stream.<locals>.flush_stream_response³   s=   ø€ ñ
 !Ñ%5à ×)Ñ)Ð*;Ô<Ø×&Ñ&Ð'8Ô9Ø#'Ð Ø$&Ñ!ð &6Ð r!   zThe provided input (type: z^) cannot be iterated. Please make sure that it is a generator, generator function or iterable.r   a8  Failed to parse the OpenAI ChatCompletionChunk. The most likely cause is a change of the chunk object structure due to a recent OpenAI update. You might be able to fix this by downgrading the OpenAI library or upgrading Streamlit. Also, please report this issue to: https://github.com/streamlit/streamlit/issues.z)langchain_core.messages.ai.AIMessageChunka9  Failed to parse the LangChain AIMessageChunk. The most likely cause is a change of the chunk object structure due to a recent LangChain update. You might be able to fix this by downgrading the OpenAI library or upgrading Streamlit. Also, please report this issue to: https://github.com/streamlit/streamlit/issues.FTé   ©ÚreturnÚNone)Ú
isinstanceÚstrr   Úis_dataframe_liker   Útyper   ÚinspectÚisgeneratorfunctionÚisasyncgenfunctionÚ
isasyncgenr   Úasync_generator_to_syncÚiterÚ	TypeErrorÚis_openai_chunkÚlenÚchoicesÚdeltaÚcontentÚAttributeErrorÚis_typeÚdgÚemptyr*   ÚcallableÚwriter+   )ÚselfÚstreamr&   Ú
cursor_strr/   ÚexcÚchunkÚerrÚ
first_textr,   r-   r.   s            @@@r"   r%   zWriteMixin.write_stream@   sŸ  ú€ ôN �fœcÔ"¤n×&FÑ&FÀvÔ&NÜ'ðÜ˜F“|�nð %"ð"óð ð ’\˜rˆ
Ø26ÐØ!#ÐÜ%4Ó%6ˆ÷
	'ô ×&Ñ& vÔ.´'×2LÑ2LÈVÔ2TÙ“XˆFô ×Ñ˜fÔ%Ü×6Ñ6°vÓ>ˆFð	Ü�ŒLð ó 4	.ˆEÜ×(Ñ(¨Ô/ðÜ˜5Ÿ=™=Ó)¨QÒ.°%·-±-ÀÑ2B×2HÑ2HÐ2Pð !#™à %§¡¨aÑ 0× 6Ñ 6× >Ñ >Ò DÀ"˜ô × Ñ  Ð(SÔTð	Ø!ŸM™MÒ/¨R�Eô ˜%¤Ô%Ùàà"�
Ù'Ø'+§w¡w§}¡}£Ð$Ø!%�JØ! UÑ*Ð!à ×)Ñ)Ø%©z©¸zÑJõô ˜%”Ù%Ô'Ù–á%Ô'Ø—
‘
˜5Ô!Ø×&Ñ& uÖ-ði4	.ñl 	ÔáàÜˆÓ 1Ò$¬°OÀAÑ4FÌÔ)Là" 1Ñ%Ð%ð ÐøôQ ò 	Ü'Ø,¬T°&«\¨Nð ;[ð [óð ðûð	ûô$ &ò Ü/ðfóð ðûðûô &ò Ü/ðfóð ðûðúsI   Â9H, Ã AIÅI3È,	IÈ5IÉIÉ	I0ÉI+É+I0É3	JÉ<JÊJrI   F©Úunsafe_allow_htmlc               ó  ‡ ‡‡— t        |«      dk(  r4t        |d   t        «      r!‰ j                  j	                  |d   ‰¬«       yg Š‰ j                  j
                  st        |«      dkD  rt        d«      ‚dˆ ˆˆfd„}|D �]t  }t        |t        «      r‰j                  |«       Œ&t        |t        «      r< |«        |D ]/  }t        |«      r |«         |«        Œ‰ j                  |‰¬«       Œ1 Œrt        |t        «      r# |«        ‰ j                  j                  |«       Œ¥t        j                  |«      r# |«        ‰ j                  j                  |«       ŒÝt!        j"                  |«      r$ |«        ‰ j                  j%                  |«       �Œt        j&                  |«      r$ |«        ‰ j                  j)                  |«       �ŒOt        j*                  |d«      r$ |«        ‰ j                  j-                  |«       �Œ‰t        j.                  |«      r$ |«        ‰ j                  j1                  |«       �ŒÂt        j*                  |d«      r$ |«        ‰ j                  j3                  |«       �Œüt        j4                  |«      r$ |«        ‰ j                  j7                  |«       �Œ5t        j8                  |«      r$ |«        ‰ j                  j;                  |«       �Œnt        j<                  |«      r$ |«        ‰ j                  j?                  |«       �Œ§t        j@                  |«      rIdd	l!m"}  |«        |jG                  |«      }‰ j                  j7                  |jI                  «       «       �Œt        |tJ        tL        tN        tP        tR        jT                  tV        tX        tZ        t\        t^        t`        f«      s?t        jb                  |«      s*t        jd                  |«      st        jf                  |«      r$ |«        ‰ j                  ji                  |«       �Œµt        jj                  |«      r$ |«        ‰ j                  jm                  |«       �Œît        |tn        «      r2 |«        ‰ j                  j	                  |jq                  «       «       �Œ0ts        jt                  |«      sUts        jv                  |«      s@ts        jx                  |«      s+ts        jz                  |«      st        j*                  |d
«      r |«        ‰ j}                  |«       �Œ´t        |t~        «      st�        j‚                  |«      r$ |«        ‰ j                  j                  |«       �Œýts        j„                  |«      r. |«        ‰ j                  j                  t‡        d|«      «       �Œ@‰rAt        jˆ                  |d«      r+‰ j                  j‹                  |j�                  «       «       �Œƒt        jˆ                  |d«      st        jˆ                  |d«      r$ |«        ‰ j                  j%                  |«       �ŒÓt        |«      }t�        |«      r$ |«        ‰ j                  j                  |«       �Œd|v r9t‘        dt“        |d«      dz   «      }	d|	z  }
‰j                  |
› d|› d|
› �«       �ŒJt“        |d«      dz   }	d|	z  }
‰j                  |
› |› |
› �«       �Œw  |«        y)a£  Displays arguments in the app.

        This is the Swiss Army knife of Streamlit commands: it does different
        things depending on what you throw at it. Unlike other Streamlit
        commands, ``st.write()`` has some unique properties:

        - You can pass in multiple arguments, all of which will be displayed.
        - Its behavior depends on the input type(s).

        Parameters
        ----------
        *args : any
            One or many objects to display in the app.

            .. list-table:: Each type of argument is handled as follows:
                :header-rows: 1

                * - Type
                  - Handling
                * - ``str``
                  - Uses ``st.markdown()``.
                * - dataframe-like, ``dict``, or ``list``
                  - Uses ``st.dataframe()``.
                * - ``Exception``
                  - Uses ``st.exception()``.
                * - function, module, or class
                  - Uses ``st.help()``.
                * - ``DeltaGenerator``
                  - Uses ``st.help()``.
                * - Altair chart
                  - Uses ``st.altair_chart()``.
                * - Bokeh figure
                  - Uses ``st.bokeh_chart()``.
                * - Graphviz graph
                  - Uses ``st.graphviz_chart()``.
                * - Keras model
                  - Converts model and uses ``st.graphviz_chart()``.
                * - Matplotlib figure
                  - Uses ``st.pyplot()``.
                * - Plotly figure
                  - Uses ``st.plotly_chart()``.
                * - ``PIL.Image``
                  - Uses ``st.image()``.
                * - generator or stream (like ``openai.Stream``)
                  - Uses ``st.write_stream()``.
                * - SymPy expression
                  - Uses ``st.latex()``.
                * - An object with ``._repr_html()``
                  - Uses ``st.html()``.
                * - Database cursor
                  - Displays DB API 2.0 cursor results in a table.
                * - Any
                  - Displays ``str(arg)`` as inline code.

        unsafe_allow_html : bool
            Whether to render HTML within ``*args``. This only applies to
            strings or objects falling back on ``_repr_html_()``. If this is
            ``False`` (default), any HTML tags found in ``body`` will be
            escaped and therefore treated as raw text. If this is ``True``, any
            HTML expressions within ``body`` will be rendered.

            Adding custom HTML to your app impacts safety, styling, and
            maintainability.

            .. note::
                If you only want to insert HTML or CSS without Markdown text,
                we recommend using ``st.html`` instead.


        Returns
        -------
        None

        Examples
        --------
        Its basic use case is to draw Markdown-formatted text, whenever the
        input is a string:

        >>> import streamlit as st
        >>>
        >>> st.write("Hello, *World!* :sunglasses:")

        ..  output::
            https://doc-write1.streamlit.app/
            height: 150px

        As mentioned earlier, ``st.write()`` also accepts other data formats, such as
        numbers, data frames, styled data frames, and assorted objects:

        >>> import streamlit as st
        >>> import pandas as pd
        >>>
        >>> st.write(1234)
        >>> st.write(
        ...     pd.DataFrame(
        ...         {
        ...             "first column": [1, 2, 3, 4],
        ...             "second column": [10, 20, 30, 40],
        ...         }
        ...     )
        ... )

        ..  output::
            https://doc-write2.streamlit.app/
            height: 350px

        Finally, you can pass in multiple arguments to do things like:

        >>> import streamlit as st
        >>>
        >>> st.write("1 + 1 = ", 2)
        >>> st.write("Below is a DataFrame:", data_frame, "Above is a dataframe.")

        ..  output::
            https://doc-write3.streamlit.app/
            height: 410px

        Oh, one more thing: ``st.write`` accepts chart objects too! For example:

        >>> import altair as alt
        >>> import pandas as pd
        >>> import streamlit as st
        >>> from numpy.random import default_rng as rng
        >>>
        >>> df = pd.DataFrame(rng(0).standard_normal((200, 3)), columns=["a", "b", "c"])
        >>> chart = (
        ...     alt.Chart(df)
        ...     .mark_circle()
        ...     .encode(x="a", y="b", size="c", color="c", tooltip=["a", "b", "c"])
        ... )
        >>>
        >>> st.write(chart)

        ..  output::
            https://doc-vega-lite-chart.streamlit.app/
            height: 300px

        r0   r   rQ   NzßCannot replace a single element with multiple elements.

The `write()` method only supports multiple elements when inserting elements rather than replacing. That is, only when called as `st.write()` or `st.sidebar.write()`.c                 ó’   •— ‰rDdj                  ‰«      } ‰j                  j                  «       }|j                  | ‰¬«       g ‰d d  y y )Nú rQ   )ÚjoinrF   rG   r*   )Útext_contentÚtext_containerrJ   Ústring_bufferrR   s     €€€r"   Úflush_bufferz&WriteMixin.write.<locals>.flush_bufferµ  sO   ø€ ÙØ"Ÿx™x¨Ó6�ð "&§¡§¡£�Ø×'Ñ'Ø Ø&7ð (ô ð $&�™aÑ ð r!   zmatplotlib.figure.Figurezbokeh.plotting.figure.Figure)Ú	vis_utilszopenai.Streamr7   Ú_repr_html_Ú	to_pandasÚ__dataframe__ú
é   ú`r1   )Jr@   r4   r5   rF   r*   Ú_is_top_levelr   r+   r   rH   rI   Ú	ExceptionÚ	exceptionr   Úis_delta_generatorÚhelpr   r6   Ú	dataframeÚis_altair_chartÚaltair_chartrE   ÚpyplotÚis_plotly_chartÚplotly_chartÚbokeh_chartÚis_graphviz_chartÚgraphviz_chartÚis_sympy_expressionÚlatexÚis_pillow_imageÚimageÚis_keras_modelÚtensorflow.python.keras.utilsr[   Úmodel_to_dotÚ	to_stringÚdictÚlistÚmapÚ	enumerateÚtypesÚMappingProxyTyper   r   r   r
   r   r   Úis_custom_dictÚis_namedtupleÚis_pydantic_modelÚjsonÚ	is_pydeckÚpydeck_chartr   Úgetvaluer8   Úisgeneratorr9   r:   r;   r%   r   ÚdataclassesÚis_dataclassÚisclassr   Úhas_callable_attrÚhtmlr\   r   Úmaxr   )rJ   rR   ÚargsrZ   ÚargÚitemr[   ÚdotÚstringified_argÚbacktick_countÚbacktick_wrapperrY   s   ``         @r"   rI   zWriteMixin.write  s0  ú€ ôZ ˆt‹9˜Š>œj¨¨a©´#Ô6ð
 �G‰G×Ñ˜T !™WÐ8IÐÔJØà#%ˆð
 �w‰w×$Ò$¬¨T«°QªÜ'ðGóð ÷
	&ð ó B	ˆCä˜#œsÔ#Ø×$Ñ$ SÕ)Ü˜C¤Ô1Ù”Øò N�DÜ ”~Ù$œÙ�àŸ
™
 4Ð;L˜
ÕMñNô ˜C¤Ô+Ù”Ø—‘×!Ñ! #Õ&Ü×-Ñ-¨cÔ2Ù”Ø—‘—‘˜SÕ!Ü×1Ñ1°#Ô6Ù”Ø—‘×!Ñ! #Ö&Ü×*Ñ*¨3Ô/Ù”Ø—‘×$Ñ$ SÖ)Ü×"Ñ" 3Ð(BÔCÙ”Ø—‘—‘˜sÖ#Ü×*Ñ*¨3Ô/Ù”Ø—‘×$Ñ$ SÖ)Ü×"Ñ" 3Ð(FÔGÙ”Ø—‘×#Ñ# CÖ(Ü×,Ñ,¨SÔ1Ù”Ø—‘×&Ñ& sÖ+Ü×.Ñ.¨sÔ3Ù”Ø—‘—‘˜cÖ"Ü×*Ñ*¨3Ô/Ù”Ø—‘—‘˜cÖ"Ü×)Ñ)¨#Ô.õñ ”Ø×,Ñ,¨SÓ1�Ø—‘×&Ñ& s§}¡}£Ö7äØäÜÜÜ!Ü×.Ñ.Ü Ü Ü Ü!Ü Ü"ðôô  ×+Ñ+¨CÔ0Ü×*Ñ*¨3Ô/Ü×.Ñ.¨sÔ3á”Ø—‘—‘˜SÖ!Ü×$Ñ$ SÔ)Ù”Ø—‘×$Ñ$ SÖ)Ü˜C¤Ô*Ù”Ø—‘× Ñ  §¡£Ö0ä×#Ñ# CÔ(Ü×.Ñ.¨sÔ3Ü×-Ñ-¨cÔ2Ü×%Ñ% cÔ*Ü×$Ñ$ S¨/Ô:á”Ø×!Ñ! #Ö&Ü˜C¤Ô,´×0HÑ0HÈÔ0MÙ”Ø—‘—‘˜SÖ!Ü—‘ Ô%Ù”ð —‘—‘œT &¨#Ó.Ö/Ù"¤y×'BÑ'BÀ3ÈÔ'VØ—‘—‘˜SŸ_™_Ó.Ö/Ü×,Ñ,Ø�[ôä×,Ñ,¨S°/ÔBñ ”Ø—‘×!Ñ! #Ö&ä"% c£(�ä% oÔ6Ù ”NØ—G‘G—L‘L Ö%à˜_Ñ,ô &)¨Ô,=¸oÈsÓ,SÐVWÑ,WÓ%X�NØ'*¨^Ñ';Ð$Ø!×(Ñ(Ø+Ð,¨B¨Ð.?¸rÐBRÐASÐTöô &7°ÈÓ%LÈqÑ%P�NØ'*¨^Ñ';Ð$Ø!×(Ñ(Ø+Ð,¨_Ð,=Ð>NÐ=OÐPöðAB	ñH 	�r!   c                ó   — t        d| «      S )zGet our DeltaGenerator.r   )r   )rJ   s    r"   rF   zWriteMixin.dgG  s   € ô Ð$ dÓ+Ð+r!   )rK   zXCallable[..., Any] | Generator[Any, Any, Any] | Iterable[Any] | AsyncGenerator[Any, Any]r&   z
str | Noner2   zlist[Any] | str)rŒ   r   rR   Úboolr2   r3   )r2   r   )r   r   r   r   r%   rI   ÚpropertyrF   r    r!   r"   r$   r$   ?   su   „ Ù�NÓ#ð "ñPð#ðPð ðPð 
òPó $ðPñd �GÓØ:?ô qó ðqðf	 ò,ó ñ,r!   r$   ).Ú
__future__r   r†   r8   r|   Úcollectionsr   r   r   Úcollections.abcr   r   r	   r
   r   r   r   Úior   Útypingr   r   r   r   Ú	streamlitr   r   Ústreamlit.errorsr   Ústreamlit.runtime.metrics_utilr   Ústreamlit.string_utilr   r   Ústreamlit.delta_generatorr   ÚBuiltinFunctionTypeÚBuiltinMethodTypeÚFunctionTypeÚ
MethodTypeÚ
ModuleTyper   Ú__annotations__ry   r   r$   r    r!   r"   ú<module>r¦      s©   ðö #ã Û Û ß 4Ñ 4÷÷ ñ õ ÷ó ÷ 0Ý 2Ý 9÷ñ
 Ý8ð 
×ÑØ	×ÑØ	×ÑØ	×ÑØ	×Ñð,€
Ð(ó ô	�d˜3‘iô 	÷K,ò K,r!   