
    4iaT                       d dl mZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZmZ d dlmZ d d	l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! d dl"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z) d dl*m+Z, d dl-m.Z/ d dl-m0Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 erd dl:m;Z;m<Z<m=Z=m>Z> erd dl?m@Z@ d dlAmBZB ddZC G d d      ZDy)    )annotationsN)Mapping)TYPE_CHECKINGAnycast)EVENT_DELIMINTERNAL_COMPONENT_NAME)BidiComponentSerdedeserialize_trigger_listserialize_mixed_data)BidiComponentResultunwrap_component_state)make_bidi_component_presenter)
DataFormatconvert_anything_to_arrow_bytesdetermine_data_format)current_form_id)HeightLayoutConfigWidthvalidate_width)check_cache_replay_rules)compute_and_register_element_idto_key)%BidiComponentInvalidCallbackNameError#BidiComponentInvalidDefaultKeyErrorBidiComponentInvalidIdError$BidiComponentUnserializableDataError)	ArrowData)BidiComponent)	MixedData)gather_metrics)get_script_run_ctx)register_widget)calc_md5)BidiComponentDataBidiComponentDefaultsBidiComponentKeyComponentIsolateStyles)DeltaGenerator)WidgetCallbackc                    ddl m} t        | v rt        dt              t        |v rt        dt              | d|  t         | S )a  Construct the per-event *trigger widget* identifier.

    The widget ID for a trigger is derived from the *base* component ID plus
    an *event* name. We join those two parts with :py:const:`EVENT_DELIM` and
    perform a couple of validations so that downstream logic can always split
    the identifier unambiguously.

    Trigger widgets are marked as internal by prefixing with an internal key prefix,
    so they won't be exposed in `st.session_state` to end users.

    Parameters
    ----------
    base
        The unique, framework-assigned ID of the component instance.
    event
        The event name as provided by either the frontend or the developer
        (e.g., "click", "change").

    Returns
    -------
    str
        The composite widget ID in the form ``"$$STREAMLIT_INTERNAL_KEY_{base}__{event}"``
        where ``__`` is the delimiter.

    Raises
    ------
    StreamlitAPIException
        If either `base` or `event` already contains the delimiter sequence.

    r   )STREAMLIT_INTERNAL_KEY_PREFIXbaseevent_)%streamlit.runtime.state.session_stater-   r   r   )r.   r/   r-   s      u/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/components/v2/bidi_component/main.py_make_trigger_idr3   M   sL    > Td)&+>>e)';??+,AdVK=HH    c                      e Zd ZdZd
dZd
dZddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ ed      	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	dd	       Z
y)BidiComponentMixinz9Mixin class for the bidi_component DeltaGenerator method.c                    |s|S 	 t        j                  |      }	 t        j                  |d      S # t        t        f$ r |cY S w xY w# t        t        f$ r |cY S w xY w)zReturn a deterministic JSON string for identity comparisons.

        Payloads that cannot be parsed (or re-serialized) are returned as-is to
        avoid mutating developer data.
        T	sort_keys)jsonloads	TypeError
ValueErrordumps)selfpayloadparseds      r2   _canonicalize_json_for_identityz2BidiComponentMixin._canonicalize_json_for_identityy   sj     N	ZZ(F	::f55	 :& 	N	
 :& 	N	s    3 A
 AA
AAc                :    | j                  |      }t        |      S )a+  Return the hash of the canonicalized JSON payload for identity use.

        Hashing keeps the kwargs passed to ``compute_and_register_element_id``
        small even when the JSON payload is very large, while still changing the
        identity whenever the canonical JSON content changes.
        )rB   r%   )r?   r@   	canonicals      r2   #_canonical_json_digest_for_identityz6BidiComponentMixin._canonical_json_digest_for_identity   s     88A		""r4   N)datadefaultc               ~   |||||d}|j                  d      }	|	|S |	dk(  rKd}
|#	 t        j                  |d      }t        |      }
|
| j                  |j                        }
|
|d<   |S |	dk(  r$t        |j                  j                        |d<   |S |	dk(  rt        |j                        |d<   |S |	d	k(  ra|j                  }| j                  |j                        |d
<   dj                  t        |j                  j                                     |d<   |S t        d|	       # t        t
        f$ r Y w xY w)a  Build deterministic identity kwargs for ID computation.

        Construct a stable mapping of identity-relevant properties for
        ``compute_and_register_element_id``. This includes structural
        properties (name, style isolation, layout), default state values, and
        an explicit, typed handling of the ``BidiComponent`` ``oneof data``
        field to ensure unkeyed components change identity when their
        serialized payload or defaults change.

        Parameters
        ----------
        component_name : str
            The registered component name.
        isolate_styles : bool
            Whether the component styles are rendered in a Shadow DOM.
        width : Width
            Desired width configuration passed to the component.
        height : Height
            Desired height configuration passed to the component.
        proto : BidiComponentProto
            The populated component protobuf. Its ``data`` oneof determines
            which serialized payload (JSON, Arrow, bytes, or Mixed) contributes
            to identity.
        data : BidiComponentData, optional
            The raw data passed to the component. Used to optimize identity
            calculation for JSON payloads by avoiding a parse/serialize cycle.
            When omitted, the helper falls back to canonicalizing the JSON
            content stored on the protobuf.
        default : BidiComponentDefaults, optional
            The default state mapping for the component instance. Defaults are
            included in the identity for unkeyed components so that changing
            default values produces a new backend identity. When a user key is
            provided with ``key_as_main_identity=True``, these defaults are
            ignored by :func:`compute_and_register_element_id`.

        Returns
        -------
        dict[str, Any]
            A mapping of deterministic values to be forwarded into
            ``compute_and_register_element_id``.

        Raises
        ------
        RuntimeError
            If an unhandled ``oneof data`` variant is encountered (guards
            against adding new fields without updating identity computation).
        )component_nameisolate_styleswidthheightrG   rF   Nr:   Tr8   
arrow_databytesmixed
mixed_json,mixed_arrow_blobsz*Unhandled BidiComponent.data oneof field: )
WhichOneofr:   r>   r%   r<   r=   rE   rM   rF   rN   rO   joinsortedarrow_blobskeysRuntimeError)r?   rI   rJ   rK   rL   protorF   rG   identity
data_fieldcanonical_digestrD   rO   s                r2   _build_bidi_identity_kwargsz.BidiComponentMixin._build_bidi_identity_kwargs   s   v -,$
 %%f-
O  $ $

44 @I'/	':$
  '#'#K#KEJJ#W /HV4 3 <' &.e.>.>.C.C%DH\", + 7" !) 5HW" ! 7"$)KKE%)%M%M

&H\" -0HHVE<M<M<R<R<T5U,VH() 	 <ZLI ; ":. s   "D* *D<;D<_bidi_componentc           	        t                t        |      }t               }	|	t        i i       S ddlm}
 |
j                         j                  }|j                  |      }|t        d| d      i }t        |j                               D ]Q  \  }}t        |      s|j                  d      r|j                  d      r|dd	 }n;|r|d
k(  rt        |      |||<   S |.|D ])  }||vst!        |t        |j#                                      t%               }||_        ||_        |j*                  xs d|_        |j,                  xs d|_        |j0                  xs d|_        |j2                  xs d|_        |j4                  xs d|_        t9        |d       t;        ||      }|	 t=        |t>        t@        f      rt?        |      |_        nt=        |tB        t        tD        f      rtG        ||       nptI        |      }|tJ        jL                  k7  r8tO        |      }tQ               }||_)        |jT                  jW                  |       ntY        jZ                  |      |_,        ta        | jb                        |_2        | jg                  |||||||      }ti        	 d|d| jb                  d|}||_5        tm        |      }dd} ||      }|rto        |j#                               nd}tq        |||      }ts        |jj                  |jt                  |jv                  |	|r|ndd|      }i }ts        |tx        d |	|r|ndd      } | jz                  xs g }!i }"|!D ]I  }#t=        |#t|              s|#j                  d      }$t=        |$t~              s6|#j                  d      |"|$<   K |D ]  }%|"j                  |%      ||%<    | jb                  j                  t        ||       t        |jz                        }&t        |&|      S # t\        $ r7 	 tY        jZ                  |      |_,        n# t\        $ r t_               w xY wY w xY w)al  Add a bidirectional component instance to the app.

        This method uses a component that has already been registered with the
        application.

        Parameters
        ----------
        component_name
            The name of the registered component to use. The component's HTML,
            CSS, and JavaScript will be loaded from the registry.
        key
            An optional string to use as the unique key for the component.
            If this is omitted, a key will be generated based on the
            component's execution sequence.
        isolate_styles
            Whether to sandbox the component's styles in a shadow root.
            Defaults to True.
        data
            Data to pass to the component. This can be any JSON-serializable
            data, or a pandas DataFrame, NumPy array, or other dataframe-like
            object that can be serialized to Arrow.
        default
            A dictionary of default values for the component's state properties.
            These defaults are applied only when the state key doesn't exist
            in session state. Keys must correspond to valid state names (those
            with `on_*_change` callbacks). Trigger values do not support
            defaults.
        width
            The desired width of the component. This can be one of "stretch",
            "content", or a number of pixels.
        height
            The desired height of the component. This can be one of "stretch",
            "content", or a number of pixels.
        **kwargs
            Keyword arguments to pass to the component. Callbacks can be passed
            here, with the naming convention `on_{event_name}_change`.

        Returns
        -------
        BidiComponentResult
            A dictionary-like object that holds the component's state and
            trigger values.

        Raises
        ------
        ValueError
            If the component name is not found in the registry.
        StreamlitAPIException
            If the component does not have the required JavaScript or HTML
            content, or if the provided data cannot be serialized.

        Nr   )RuntimezComponent 'z' is not registeredon__change   ir0    T)allow_content)rK   rL   )rI   rJ   rK   rL   rY   rF   rG   )user_keykey_as_main_identitydg)rG   c                    t        | d      S )Nevents)r3   )r.   s    r2   _make_trigger_aggregator_idzGBidiComponentMixin._bidi_component.<locals>._make_trigger_aggregator_id  s    #D(33r4   
json_value)deserializer
serializerctx	callbacks
value_type	presenterc                ,    t        j                  |       S )N)r:   r>   )vs    r2   <lambda>z4BidiComponentMixin._bidi_component.<locals>.<lambda>  s    A r4   json_trigger_value)rm   rn   ro   rp   rq   r/   value)layout_config)bidi_component)r.   strreturnrz   )Cr   r   r#   r   streamlit.runtimer`   instancebidi_component_registrygetr=   listitemscallable
startswithendswithr   r   rW   BidiComponentProtorI   rJ   
js_contentjs_urljs_source_pathhtml_contentcss_contentcss_urlcss_source_pathr   r   
isinstancerN   	bytearrayr   tupler   r   r   UNKNOWNr   ArrowDataProtorF   rM   CopyFromr:   r>   	Exceptionr   r   rh   form_idr]   r   idr
   setr   r$   deserialize	serializer   rw   dictrz   _enqueuer	   r   )'r?   rI   keyrJ   rF   rG   rK   rL   kwargsro   r`   registrycomponent_defcallbacks_by_event	kwarg_keykwarg_value
event_name	state_keybidi_component_protorx   data_formatarrow_bytesarrow_data_protoidentity_kwargscomputed_idserderk   aggregator_idallowed_state_keysrr   component_statetrigger_vals
trig_statepayloadsevent_to_valuer@   evevt_name
state_valss'                                          r2   r^   z"BidiComponentMixin._bidi_component  s   @ 	!"Sk ";&r2.. 	.##%== ^4 {>*::MNOO 9;&*6<<>&: 	9"I{K(##E*y/A/A)/L&q_
 s!2;IFF-8z*	9& $ 	$66=!4(:(?(?(A#B   23.<+.<+*7*B*B*Hb'.;.B.B.Hb+,9,F,F,L")+8+D+D+J(/</D/D/J,uD1$5@AdUI$6716t(.  we&<=(/CD #8"=K"j&8&88&Ed&K+9+;(0;(-,77@@AQR 59JJt4D,1 (7tww'?$ ::))& ; 
 6
!%ww	

 
 #. #73	4 4K@ /AC"'')*d 	 2
	 * ##**,>(D#
 (*$1.,>(D+

 ",!1!1!7R)+ 	>G'4([[)b#&)0W)=N2&		> + 	BH%3%7%7%AL"	B 	# ' 	 	
 ,O,A,AB
":|<<_  AA04

40@(-  A>@@A .	As+   -B?P 	QP43Q4QQQc                    t        d|       S )zGet our DeltaGenerator.r*   )r   )r?   s    r2   rh   zBidiComponentMixin.dg  s     $d++r4   )r@   rz   r{   rz   )rI   rz   rJ   boolrK   r   rL   r   rY   r   rF   r&   rG   r'   r{   zdict[str, Any])NTNNstretchcontent)rI   rz   r   r(   rJ   r)   rF   r&   rG   r'   rK   r   rL   r   r   zWidgetCallback | Noner{   r   )r{   r*   )__name__
__module____qualname____doc__rB   rE   r]   r"   r^   propertyrh    r4   r2   r6   r6   v   s   C(	#& #')-s s 	s
 s s "s  s 's 
sj %& !%15"&)- "F=F= F= /	F=
  F= 'F= F= F= (F= 
F= 'F=P , ,r4   r6   )r.   rz   r/   rz   r{   rz   )E
__future__r   r:   collections.abcr   typingr   r   r   0streamlit.components.v2.bidi_component.constantsr   r	   4streamlit.components.v2.bidi_component.serializationr
   r   r   ,streamlit.components.v2.bidi_component.stater   r   $streamlit.components.v2.presentationr   streamlit.dataframe_utilr   r   r   !streamlit.elements.lib.form_utilsr   #streamlit.elements.lib.layout_utilsr   r   r   r   streamlit.elements.lib.policiesr   streamlit.elements.lib.utilsr   r   streamlit.errorsr   r   r   r   streamlit.proto.ArrowData_pb2r   r   !streamlit.proto.BidiComponent_pb2r    r   r!   MixedDataProtostreamlit.runtime.metrics_utilr"   7streamlit.runtime.scriptrunner_utils.script_run_contextr#   streamlit.runtime.stater$   streamlit.utilr%   streamlit.components.v2.typesr&   r'   r(   r)   streamlit.delta_generatorr*   streamlit.runtime.state.commonr+   r3   r6   r   r4   r2   <module>r      s    #  # + + 
 O 
 >  E P  F Q I 9 V 3 #  8=&IRc, c,r4   