o
    h                     @  s  U d dl mZ d dlmZ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mZmZmZ d dlmZmZ d dlmZmZ erMd dlmZ d d	lmZ e	eZd
ed< d/ddZ da!ded< 	d0d1ddZ"G dd  d eZ#d2d!d"Z$d3d#d$Z%	%d4d%ddd&d5d)d*Z&d6d-d.Z'd%S )7    )annotations)TYPE_CHECKINGAnyFinal)configerrorsloggerruntime)
is_in_form)StreamlitAPIWarning.StreamlitFragmentWidgetsNotAllowedOutsideError!StreamlitInvalidFormCallbackError'StreamlitValueAssignmentNotAllowedError)get_script_run_ctxin_cached_function)WidgetCallbackget_session_state)Sequence)DeltaGeneratorr   _LOGGERdgr   	on_changeWidgetCallback | NonereturnNonec                 C  s*   t  rt| r|durt dS dS dS )zEnsures that widgets other than `st.form_submit_button` within a form don't have
    an on_change callback set.

    Raises
    ------
    StreamlitInvalidFormCallbackError:
        Raised when the described rule is violated.
    N)r	   existsr
   r   )r   r    r   S/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/elements/lib/policies.pycheck_callback_rules*   s   
r   Fbool_shown_default_value_warningTdefault_valuer   key
str | Nonewrites_allowedc                 C  s|   |du st  s
dS t }||sdS |st|d| dur8ts:tds<ddlm	} |d| d dadS dS dS dS )	a|  Ensures that no values are set for widgets with the given key when writing
    is not allowed.

    Additionally, if `global.disableWidgetStateDuplicationWarning` is False a warning is
    shown when a widget has a default value but its value is also set via session state.

    Raises
    ------
    StreamlitAPIException:
        Raised when the described rule is violated.
    N)r"   z+global.disableWidgetStateDuplicationWarningr   )warningzThe widget with key "zX" was created with a default value but also had its value set via the Session State API.T)
r	   r   r   is_new_state_valuer   r    r   
get_option	streamlitr%   )r!   r"   r$   session_stater%   r   r   r   check_session_state_rules;   s$   


r*   c                      s   e Zd Z fddZ  ZS )CachedWidgetWarningc                   s   t  d d S )Na#  
Your script uses a widget command in a cached function
(function decorated with `@st.cache_data` or `@st.cache_resource`).
This code will only be called when we detect a cache "miss",
which can lead to unexpected results.

To fix this, move all widget commands outside the cached function.
)super__init__)self	__class__r   r   r-   d   s   zCachedWidgetWarning.__init__)__name__
__module____qualname__r-   __classcell__r   r   r/   r   r+   c   s    r+   c                  C  s&   t  rddlm}  | t  dS dS )av  Check if a widget is allowed to be used in the current context.
    More specifically, this checks if the current context is inside a
    cached function that disallows widget usage. If so, it raises a warning.

    If there are other similar checks in the future, we could extend this
    function to check for those as well. And rename it to check_widget_usage_rules.
    r   	exceptionN)r   getr(   r6   r+   r5   r   r   r   check_cache_replay_rulesq   s   r8   c                 C  sz   t  }|du s|jdu rdS |j}| jj}|du rdS |j}t|t|k r)t t|D ]\}}|| |kr:t q-dS )a[  Ensures that the current widget is not written outside of the
    fragment's delta path.

    Should be called by ever element that acts as a widget.
    We don't allow writing widgets from within a widget to the outside path
    because it can lead to unexpected behavior. For elements, this is okay
    because they do not trigger a re-run.
    N)	r   current_fragment_idcurrent_fragment_delta_path
_active_dg_cursor
delta_pathlenr   	enumerate)r   ctxr:   current_cursorcurrent_cursor_delta_pathindex
path_indexr   r   r   check_fragment_path_policy   s   
rE   N)r!   r$   enable_check_callback_rulesSequence[Any] | Any | NonerF   c                C  s.   t |  t  |rt| | t|||d dS )z7Check all widget policies for the given DeltaGenerator.)r!   r"   r$   N)rE   r8   r   r*   )r   r"   r   r!   r$   rF   r   r   r   check_widget_policies   s   


rH   labellabel_visibilityc                 C  s,   | st d |dvrtd| dd S )Nz`label` got an empty value. This is discouraged for accessibility reasons and may be disallowed in the future by raising an exception. Please provide a non-empty label and hide it with label_visibility if needed.)visiblehidden	collapsedz%Unsupported label_visibility option 'z7'. Valid values are 'visible', 'hidden' or 'collapsed'.)r   r%   r   StreamlitAPIException)rI   rJ   r   r   r   maybe_raise_label_warnings   s   
rO   )r   r   r   r   r   r   )T)r!   r   r"   r#   r$   r   r   r   )r   r   )r   r   )N)r   r   r"   r#   r   r   r!   rG   r$   r   rF   r   )rI   r#   rJ   r#   )(
__future__r   typingr   r   r   r(   r   r   r   r	   !streamlit.elements.lib.form_utilsr
   streamlit.errorsr   r   r   r   7streamlit.runtime.scriptrunner_utils.script_run_contextr   r   streamlit.runtime.stater   r   collections.abcr   streamlit.delta_generatorr   
get_loggerr1   r   __annotations__r   r    r*   r+   r8   rE   rH   rO   r   r   r   r   <module>   s2   
(

%