o
    h)                     @  s  U d dl mZ d dlZd dlZd dlZd dl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mZmZmZ d d	lmZ erd d
lmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, ee-Z.de/d< e0e1ee1e2df f Z3de/d< ej4dddZ5de/d< eG dd dZ6dZ7de/d< 	d0d1d$d%Z8d2d3d)d*Z9d4d.d/Z:dS )5    )annotationsN)Counter)	dataclassfield)TYPE_CHECKINGCallableFinalUnion)parse)	TypeAlias)NoSessionContextStreamlitAPIException-StreamlitSetPageConfigMustBeFirstCommandError)
get_logger)Path)RunningCursor)ContextInfo)
ForwardMsg)Command)FragmentStorage)PagesManager)ScriptRequests)SafeSessionState)UploadedFileManagerr   _LOGGERr   UserInfoin_cached_functionF)defaultzcontextvars.ContextVar[bool]c                   @  s  e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< ded< dZded< dZded< dZded< ee	dZ
ded< eejdZded< dZded< dZded < eedZd!ed"< eedZd!ed#< eedZd!ed$< eedZd%ed&< dZd'ed(< dZd)ed*< dZd+ed,< eedZd!ed-< d.Zded/< dZded0< dZdZed1d2 Zed3d4 Z edNd7d8Z!e"j#dOd:d;Z$dPd=d>Z%	.	.		dQdRd@dAZ&dSdBdCZ'dTdFdGZ(dHdI Z)dJdK Z*dLdM Z+dS )UScriptRunContextaG  A context object that contains data for a "script run" - that is,
    data that's scoped to a single ScriptRunner execution (and therefore also
    scoped to a single connected "session").

    ScriptRunContext is used internally by virtually every `st.foo()` function.
    It is accessed only from the script thread that's created by ScriptRunner,
    or from app-created helper threads that have been "attached" to the
    ScriptRunContext via `add_script_run_ctx`.

    Streamlit code typically retrieves the active ScriptRunContext via the
    `get_script_run_ctx` function.
    str
session_idzCallable[[ForwardMsg], None]_enqueuequery_stringr   session_stater   uploaded_file_mgrmain_script_pathr   	user_infor   fragment_storager   pages_managerNContextInfo | Nonecontext_infoFboolgather_usage_statscommand_tracking_deactivated)default_factoryzlist[Command]tracked_commandszCounter[str]tracked_commands_counterT_set_page_config_allowed_has_script_startedzset[str]widget_ids_this_runwidget_user_keys_this_runform_ids_this_runzdict[int, RunningCursor]cursorszScriptRequests | Nonescript_requestsz
str | Nonecurrent_fragment_idlist[str] | Nonefragment_ids_this_runnew_fragment_ids _active_script_hashhas_dialog_openedc                 C     | j jS N)r(   current_page_script_hashself rD   k/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/scriptrunner_utils/script_run_context.pypage_script_hashp      z!ScriptRunContext.page_script_hashc                 C  s   | j S r@   r=   rB   rD   rD   rE   active_script_hasht   s   z#ScriptRunContext.active_script_hashreturnr   c                 C  r?   r@   )r(   main_script_parentrB   rD   rD   rE   rK   x   rG   z#ScriptRunContext.main_script_parent	page_hashc                 c  s*    | j }|| _ z	d V  W || _ d S || _ w r@   rH   )rC   rL   original_page_hashrD   rD   rE   run_with_active_hash|   s   z%ScriptRunContext.run_with_active_hashrF   c                 C  s   | j j| _| j | d S r@   )r(   main_script_hashr=   set_current_page_script_hash)rC   rF   rD   rD   rE   set_mpa_v2_page   s   
z ScriptRunContext.set_mpa_v2_pageNonec           	      C  s,  i | _ t | _t | _t | _|| _|| _| j| | jj	| _
d| _d| _d| _g | _t | _d | _g | _|| _t | _d| _td tj|dd}| j :}|  | D ](\}}t|dkrm|j|dd q[t|dkr}|j||d d q[||| q[W d    d S 1 sw   Y  d S )	NTF)keep_blank_valuesr   r<   )val   ) r6   setr3   r4   r5   r"   r*   r(   rP   rO   r=   r1   r2   r-   r/   collectionsr   r0   r8   current_fragment_delta_pathr:   r;   r>   r   r
   parse_qsr#   query_paramsclear_with_no_forward_msgitemslenset_with_no_forward_msg)	rC   r"   rF   r:   r*   parsed_query_paramsqpkeyrT   rD   rD   rE   reset   s<   


"zScriptRunContext.resetc                 C  s
   d| _ d S NT)r2   rB   rD   rD   rE   on_script_start   s   
z ScriptRunContext.on_script_startmsgr   c                 C  sN   | dr| jst | ds| dr| jrd| _| j|j_| | dS )z0Enqueue a ForwardMsg for this context's session.page_config_changeddeltaFN)HasFieldr1   r   r2   rI   metadatar!   )rC   rf   rD   rD   rE   enqueue   s   

zScriptRunContext.enqueuec                 C  s   | j r
| jrtdd S d S )NzUsing `st.query_params` together with either `st.experimental_get_query_params` or `st.experimental_set_query_params` is not supported. Please convert your app to only use `st.query_params`)_experimental_query_params_used_production_query_params_usedr   rB   rD   rD   rE   ensure_single_query_api_used   s
   z-ScriptRunContext.ensure_single_query_api_usedc                 C     d| _ |   d S rd   )rl   rn   rB   rD   rD   rE   #mark_experimental_query_params_used      z4ScriptRunContext.mark_experimental_query_params_usedc                 C  ro   rd   )rm   rn   rB   rD   rD   rE   !mark_production_query_params_used   rq   z2ScriptRunContext.mark_production_query_params_used)rJ   r   )rL   r   )rF   r   )r<   r<   NN)
r"   r   rF   r   r:   r9   r*   r)   rJ   rR   )rJ   rR   rf   r   rJ   rR   ),__name__
__module____qualname____doc____annotations__r*   r,   r-   r   listr/   rX   r   r0   r1   r2   rW   r3   r4   r5   dictr6   r7   r8   r:   r;   r=   r>   rl   rm   propertyrF   rI   rK   
contextlibcontextmanagerrN   rQ   rc   re   rk   rn   rp   rr   rD   rD   rD   rE   r   @   sb   
 


	
'
r   streamlit_script_run_ctxSCRIPT_RUN_CONTEXT_ATTR_NAMEthreadthreading.Thread | NonectxScriptRunContext | Nonec                 C  s6   | du rt  } |du rt }|durt| t| | S )a
  Adds the current ScriptRunContext to a newly-created thread.

    This should be called from this thread's parent thread,
    before the new thread starts.

    Parameters
    ----------
    thread : threading.Thread
        The thread to attach the current ScriptRunContext to.
    ctx : ScriptRunContext or None
        The ScriptRunContext to add, or None to use the current thread's
        ScriptRunContext.

    Returns
    -------
    threading.Thread
        The same thread that was passed in, for chaining.

    N)	threadingcurrent_threadget_script_run_ctxsetattrr   )r   r   rD   rD   rE   add_script_run_ctx   s   r   suppress_warningr+   rJ   c                 C  s2   t  }t|td}|du r| std|j |S )a	  
    Parameters
    ----------
    suppress_warning : bool
        If True, don't log a warning if there's no ScriptRunContext.

    Returns
    -------
    ScriptRunContext | None
        The current thread's ScriptRunContext, or None if it doesn't have one.

    Nz]Thread '%s': missing ScriptRunContext! This warning can be ignored when running in bare mode.)r   r   getattrr   r   warningname)r   r   r   rD   rD   rE   r      s   r   rf   r   rR   c                 C  s@   t  }|du r
t |jr| ddkr|j| j_||  dS )z/Enqueues a ForwardMsg proto to send to the app.Ntyperh   )r   r   r8   
WhichOneofrh   fragment_idrk   )rf   r   rD   rD   rE   enqueue_message  s   
r   )NN)r   r   r   r   )F)r   r+   rJ   r   rs   );
__future__r   rX   r|   contextvarsr   r   dataclassesr   r   typingr   r   r   r	   urllibr
   typing_extensionsr   streamlit.errorsr   r   r   streamlit.loggerr   pathlibr   streamlit.cursorr   streamlit.proto.ClientState_pb2r   streamlit.proto.ForwardMsg_pb2r   streamlit.proto.PageProfile_pb2r   streamlit.runtime.fragmentr   streamlit.runtime.pages_managerr   4streamlit.runtime.scriptrunner_utils.script_requestsr   streamlit.runtime.stater   'streamlit.runtime.uploaded_file_managerr   rt   r   rx   rz   r   r+   r   
ContextVarr   r   r   r   r   r   rD   rD   rD   rE   <module>   sH    