
    g(                       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r6d 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<   e.e/ee/e0df   f   Z1de-d<    ejd                  dd      Z3de-d<   e G d d             Z4dZ5de-d<   	 d"	 	 	 d#dZ6d$d%d Z7d&d!Z8y)'    )annotationsN)Counter)	dataclassfield)TYPE_CHECKINGCallableFinalUnion)parse)	TypeAlias)NoSessionContextStreamlitAPIException-StreamlitSetPageConfigMustBeFirstCommandError)
get_logger)Path)RunningCursor)
ForwardMsg)Command)FragmentStorage)PagesManager)ScriptRequests)SafeSessionState)UploadedFileManagerr	   _LOGGERr   UserInfoin_cached_functionF)defaultzcontextvars.ContextVar[bool]c                  j   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<    ee      Z	ded<    ee
j                        Zded<   dZded<   dZded<    ee      Zded<    ee      Zded <    ee      Zded!<    ee      Zd"ed#<   d$Zd%ed&<   d$Zd'ed(<   d$Zd)ed*<    ee      Zded+<   d,Zded-<   dZded.<   dZdZed/        Zed0        Zed:d1       Z e!jD                  d;d2       Z#d<d3Z$	 	 	 d=	 	 	 	 	 	 	 d>d4Z%d?d5Z&d@d6Z'd7 Z(d8 Z)d9 Z*y$)A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_managerF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]cursorsNz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                .    | j                   j                  S N)r)   current_page_script_hashselfs    l/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner_utils/script_run_context.pypage_script_hashz!ScriptRunContext.page_script_hashn   s    !!:::    c                    | j                   S r?   r<   rA   s    rC   active_script_hashz#ScriptRunContext.active_script_hashr   s    '''rE   c                .    | j                   j                  S r?   )r)   main_script_parentrA   s    rC   rJ   z#ScriptRunContext.main_script_parentv   s    !!444rE   c              #  b   K   | j                   }|| _         	 d  || _         y # || _         w xY wwr?   rG   )rB   	page_hashoriginal_page_hashs      rC   run_with_active_hashz%ScriptRunContext.run_with_active_hashz   s5     !55#, 	: (:D$'9D$s   /# /	,/c                p    | j                   j                  | _        | j                   j                  |       y r?   )r)   main_script_hashr<   set_current_page_script_hash)rB   rD   s     rC   set_mpa_v2_pagez ScriptRunContext.set_mpa_v2_page   s+    #'#5#5#F#F 778HIrE   c                `   i | _         t               | _        t               | _        t               | _        || _        | j                  j                  |       | j                  j                  | _	        d| _
        d| _        d| _        g | _        t        j                         | _        d | _        g | _        || _        t               | _        d| _        t,        j                  d       t/        j0                  |d      }| j2                  j5                         5 }|j7                          |j9                         D ]^  \  }}t;        |      dk(  r|j=                  |d       (t;        |      dk(  r|j=                  ||d          M|j=                  ||       ` 	 d d d        y # 1 sw Y   y xY w)	NTF)keep_blank_valuesr   r;   )val   )r5   setr2   r3   r4   r#   r)   rQ   initial_active_script_hashr<   r0   r1   r,   r.   collectionsr   r/   r7   current_fragment_delta_pathr9   r:   r=   r   r   parse_qsr$   query_paramsclear_with_no_forward_msgitemslenset_with_no_forward_msg)rB   r#   rD   r9   parsed_query_paramsqpkeyrU   s           rC   resetzScriptRunContext.reset   sg    #&5 ),&!$(778HI#'#5#5#P#P (,%#( 27) "(3(;(;(=%#' 68(%:" #!&u%#nn\TR,,."((*/557Ss8q=..s.;X]..sB.@..sC8 8 /..s   BF$$F-c                    d| _         y NT)r1   rA   s    rC   on_script_startz ScriptRunContext.on_script_start   s
    #' rE   c                   |j                  d      r| j                  s
t               |j                  d      s|j                  d      r| j                  rd| _        | j                  |j
                  _        | j                  |       y)z0Enqueue a ForwardMsg for this context's session.page_config_changeddeltaFN)HasFieldr0   r   r1   rH   metadatar"   )rB   msgs     rC   enqueuezScriptRunContext.enqueue   sk    <<-.t7T7T?AA
 <<-.LL!d&>&>,1D)*.*A*A' 	crE   c                L    | j                   r| j                  rt        d      y y )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   rA   s    rC   ensure_single_query_api_usedz-ScriptRunContext.ensure_single_query_api_used   s.    //D4V4V'0  5W/rE   c                2    d| _         | j                          y rg   )rq   rs   rA   s    rC   #mark_experimental_query_params_usedz4ScriptRunContext.mark_experimental_query_params_used   s    /3,))+rE   c                2    d| _         | j                          y rg   )rr   rs   rA   s    rC   !mark_production_query_params_usedz2ScriptRunContext.mark_production_query_params_used   s    -1*))+rE   )returnr   )rL   r    )rD   r    )r;   r;   N)r#   r    rD   r    r9   r8   rx   None)rx   ry   rn   r   rx   ry   )+__name__
__module____qualname____doc____annotations__r+   r,   r   listr.   rZ   r   r/   r0   r1   rX   r2   r3   r4   dictr5   r6   r7   r9   r:   r<   r=   rq   rr   propertyrD   rH   rJ   
contextlibcontextmanagerrN   rR   re   rh   ro   rs   ru   rw    rE   rC   r   r   ?   s    O**##**%%$$). $.&+D&AmA-2;CVCV-WlW%)d) %%$)#$>>*/*DxD"'"<x<(-d(CG%C-1O*1&**.2+2!&s!;h;!!#t# ',#$)!; ; ( ( 5 5 : :J  "26	#9#9 #9  0	#9
 
#9J($,,rE   r   streamlit_script_run_ctxSCRIPT_RUN_CONTEXT_ATTR_NAMEc                p    | t        j                         } |
t               }|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.

    )	threadingcurrent_threadget_script_run_ctxsetattrr   )threadctxs     rC   add_script_run_ctxr      s;    , ~))+
{ "
4c:MrE   c                    t        j                         }t        |t        d      }|"| s t        j                  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)suppress_warningr   r   s      rC   r   r      sK     %%'F#*63OQU#VC
{+
 	$KK	
 JrE   c                    t               }|
t               |j                  r/| j                  d      dk(  r|j                  | j                  _        |j                  |        y)z/Enqueues a ForwardMsg proto to send to the app.Ntyperk   )r   r   r7   
WhichOneofrk   fragment_idro   )rn   r   s     rC   enqueue_messager     sO    

C
{  
3>>&#9W#D # 7 7		KKrE   )NN)r   zthreading.Thread | Noner   ScriptRunContext | None)F)r   r*   rx   r   rz   )9
__future__r   rZ   r   contextvarsr   r   dataclassesr   r   typingr   r   r	   r
   urllibr   typing_extensionsr   streamlit.errorsr   r   r   streamlit.loggerr   pathlibr   streamlit.cursorr   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   r{   r   r   r   r    r*   r   
ContextVarr   r   r   r   r   r   r   rE   rC   <module>r      s    #      (   ' 
 (.97:<S8KH% %3c4o 667) 7
 4J;3I3I%4 0 
 P, P, P,f 'A e @ LP#1H>8
rE   