
    &e;              
          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m	Z	 d dl
Z
d dlmZ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 d dlmZ d dlmZ d d	lmZmZm Z  d d
l!m"Z"m#Z#m$Z$  ee%          Z&d e
j'        ddd           e
j'        dd           e
j'        dd          dz  Z(dZ)de"ddfdZ*de+ddfdZ,d.dZ-d.dZ.de+de	e+         ddfdZ/de"ddfdZ0d.d Z1d! Z2d.d"Z3de+ddfd#Z4d$e5ddfd%Z6de+ddfd&Z7d'ee+ef         ddfd(Z8d'ee+ef         ddfd)Z9d*e+ddfd+Z:de+d,e5de	e+         d'ee+ef         ddf
d-Z;dS )/    N)Path)AnyDictList)configenv_util	file_utilnet_utilsecretsurl_utilutilversion)CONFIG_FILENAMES)MIN_GIT_VERSIONGitRepo)
get_loggerinvalidate_pages_cache)report_watchdog_availability	watch_dir
watch_file)Serverserver_address_is_unix_socketserver_utilz
  %(new_version)s

  See what's new at https://discuss.streamlit.io/c/announcements

  Enter the following command to upgrade:
  %(prompt)s %(command)s
z(A new version of Streamlit is available.blueTfgbold$r   zpip install streamlit --upgrader   )new_versionpromptcommandi   @serverreturnc                 b    t                               d            fd}t          j        t          j        |           t          j        t          j        |           t
          j        dk    r!t          j        t          j        |           d S t          j        t          j        |           d S )NzSetting up signal handlerc                 0                                      d S N)stop)signal_numberstack_framer%   s     7lib/python3.11/site-packages/streamlit/web/bootstrap.pysignal_handlerz._set_up_signal_handler.<locals>.signal_handlerC   s        win32)	LOGGERdebugsignalSIGTERMSIGINTsysplatformSIGBREAKSIGQUIT)r%   r.   s   ` r-   _set_up_signal_handlerr:   @   s    
LL,---     M&..111
M&-000
|wfo~66666fnn55555r/   main_script_pathc                     t           j                            dt          j                            |                      dS )zAdd the script's folder to the sys path.

    Python normally does this automatically, but since we exec the script
    ourselves we need to do it instead.
    r   N)r6   pathinsertosdirname)r;   s    r-   _fix_sys_pathrA   O   s/     HOOArw'78899999r/   c                      t          j        d          r-	 ddl} |                     d           dS # t          $ r Y dS w xY wdS )a  Set Matplotlib backend to avoid a crash.

    The default Matplotlib backend crashes Python on OSX when run on a thread
    that's not the main thread, so here we set a safer backend as a fix.
    Users can always disable this behavior by setting the config
    runner.fixMatplotlib = false.

    This fix is OS-independent. We didn't see a good reason to make this
    Mac-only. Consistency within Streamlit seemed more important.
    zrunner.fixMatplotlibr   NAgg)r   
get_option
matplotlibuseImportError)rE   s    r-   _fix_matplotlib_crashrH   X   so     /00 	 NN5!!!!! 	 	 	DD	 s   1 
??c                      t           j        r\	 ddlm} m} t          t          j                              | u rt          j         |                       dS dS # t          $ r Y dS w xY wdS )a/  Set default asyncio policy to be compatible with Tornado 6.

    Tornado 6 (at least) is not compatible with the default
    asyncio implementation on Windows. So here we
    pick the older SelectorEventLoopPolicy when the OS is Windows
    if the known-incompatible default policy is in use.

    This has to happen as early as possible to make it a low priority and
    overridable

    See: https://github.com/tornadoweb/tornado/issues/2608

    FIXME: if/when tornado supports the defaults in asyncio,
    remove and bump tornado requirement for py38
    r   WindowsProactorEventLoopPolicyWindowsSelectorEventLoopPolicyN)	r   
IS_WINDOWSasynciorK   rL   typeget_event_loop_policyset_event_loop_policyrG   rJ   s     r-   _fix_tornado_crashrR   u   s       P	P        G133448VVV -.L.L.N.NOOOOO WV	  	 	 	DD	P Ps   A 
A&%A&argsc                 >    ddl }| gt          |          z   |_        dS )ztsys.argv needs to exclude streamlit arguments and parameters
    and be set to what a user's script may expect.
    r   N)r6   listargv)r;   rS   r6   s      r-   _fix_sys_argvrW      s&     JJJ !DJJ.CHHHr/   c                    t          | j                   t          | j                   t          | j                   t                       t                       	 t          j                     n3# t          $ r&}t                              d|           Y d }~nd }~ww xY wd }t          j                                        |           d S )Nz Failed to load secrets.toml file)exc_infoc                  F   t          j        d          rd S t          j        d          rt          j        d          } n;t          j        d          r%t                      rd S t          j        d          } nd} t	          j        t          j        |                      d S )Nserver.headlessbrowser.serverAddressserver.address	localhost)r   rD   is_manually_setr   r   open_browserr   get_url)addrs    r-   maybe_open_browserz,_on_server_start.<locals>.maybe_open_browser   s    .// 	F!"9:: 	$%<==DD#$455 	,.. $%566DDD+-d3344444r/   )_maybe_print_old_git_warningr;   "_maybe_print_static_folder_warning
_print_urlis_running_hellor   _print_new_version_messager   load_if_toml_exists	Exceptionr1   errorrN   get_running_loop	call_soon)r%   exrc   s      r-   _on_server_startro      s     !8999&v'>???v&''' """   
G#%%%% G G G82FFFFFFFFG5 5 5$ (();<<<<<s   A. .
B8BBc                  H    t          j        d          t          j        d<   dS )z_Sets MAPBOX_API_KEY environment variable needed for PyDeck otherwise it will throw an exceptionzmapbox.tokenMAPBOX_API_KEYN)r   rD   r?   environ r/   r-   _fix_pydeck_mapbox_api_warningrt      s"     $*#4^#D#DBJ   r/   c                  F    	 ddl m}  d | _        dS # t          $ r Y dS w xY w)a1  Pydantic by default disallows to reuse of validators with the same name,
    this combined with the Streamlit execution model leads to an error on the second
    Streamlit script rerun if the Pydantic validator is registered
    in the streamlit script.

    It is important to note that the same issue exists for Pydantic validators inside
    Jupyter notebooks, https://github.com/pydantic/pydantic/issues/312 and in order
    to fix that in Pydantic they use the `in_ipython` function that checks that
    Pydantic runs not in `ipython` environment.

    Inside this function we patch `in_ipython` function to always return `True`.

    This change will relax rules for writing Pydantic validators inside
    Streamlit script a little bit, similar to how it works in jupyter,
    which should not be critical.
    r   class_validatorsc                      dS )NTrs   rs   r/   r-   <lambda>z:_fix_pydantic_duplicate_validators_error.<locals>.<lambda>   s    d r/   N)pydanticrw   
in_ipythonrG   rv   s    r-   (_fix_pydantic_duplicate_validators_errorr|      sL    "------&2l###   s    
  c                  b    t          j                    rt          j        t                     d S d S r)   )r   should_show_new_version_noticeclicksechoNEW_VERSION_TEXTrs   r/   r-   rh   rh      s4    -// &$%%%%%& &r/   c                 h   t          j        d          rt          j        |           }t          j                            |          st          j        d| dd           dS t          j	        |          }|t          k    r/t          j        dd           t          j        dd           dS dS dS )	z7Prints a warning if the static folder is misconfigured.zserver.enableStaticServingzGWARNING: Static file serving is enabled, but no static folder found at zJ. To disable static file serving, set server.enableStaticServing to false.yellowr    FzVWARNING: Static folder size is larger than 1GB. Static file serving has been disabled.N)r   rD   r	   get_app_static_dirr?   r=   isdirr   r   get_directory_sizeMAX_APP_STATIC_FOLDER_SIZE
set_option)r;   static_folder_pathstatic_folder_sizes      r-   re   re      s     566 &9:JKKw}}/00 	K<(< < < 	      "+!=>P!Q!Q!$>>>!">FFF=       ?>r/   rg   c                 V   | rd}nd}g }t          j        d          r+dt          j        t          j        d                    fg}nYt          j        d          r9t                      s+dt          j        t          j        d                    fg}nt                      rdt          j        d          fg}nt          j        d          r}t          j                    }|r)|                    dt          j        |          f           t          j	                    }|r)|                    d	t          j        |          f           nUd
t          j        d          fg}t          j                    }|r)|                    dt          j        |          f           t          j        d           t          j        d|z  dd           t          j        d           |D ]\  }}t          j        ||           t          j        d           | rt          j        d           t          j        dd           t          j        dd           t          j        d           t          j        d           t          j        d           t          j        d           d S d S )Nz9Welcome to Streamlit. Check out our demo in your browser.z4You can now view your Streamlit app in your browser.r\   URLr]   zUnix Socketr[   zNetwork URLzExternal URLz	Local URLr^    z  %sr   Tr   z5  Ready to create your own Python apps super quickly?z  Head over to F)nlzhttps://docs.streamlit.ior!   z  May you create awesome apps!)r   r_   r   ra   rD   r   r
   get_internal_ipappendget_external_ipr   r   r   	print_url)rg   title_message
named_urlsinternal_ipexternal_ipurl_nameurls          r-   rf   rf      s    OSNJ566 !QK'(9:Q(R(RSST



 	/00Q9V9X9XQ K'(9:J(K(KLLM


 
'	(	( QF-.>??@


 
	,	-	- Q.00 	Q}k.A+.N.NOPPP.00 	R~{/B;/O/OPQQQ +-k::;

 .00 	Q}k.A+.N.NOPPP	KOOO	K&6====	KOOO# * *#8S))))	KOOO KLLL%%0000/d;;;;B4555BB r/   c                 &   t          |           }|                                s|j        |j        t          k     rd                    d |j        D                       }d                    d t          D                       }t          j        d           t          j        ddd	           t          j        d           t          j        d
| d| dd           t          j        dd           t          j        dd           dS dS dS dS )zIf our script is running in a Git repo, and we're running a very old
    Git version, print a warning that Git integration will be unavailable.
    N.c              3   4   K   | ]}t          |          V  d S r)   str.0vals     r-   	<genexpr>z/_maybe_print_old_git_warning.<locals>.<genexpr>G  s(      %K%K3c#hh%K%K%K%K%K%Kr/   c              3   4   K   | ]}t          |          V  d S r)   r   r   s     r-   r   z/_maybe_print_old_git_warning.<locals>.<genexpr>H  s(      %J%J3c#hh%J%J%J%J%J%Jr/   r   z  Git integration is disabled.r   Tr   z  Streamlit requires Git z or later, but you have r    z>  Git is used by Streamlit Cloud (https://streamlit.io/cloud).z,  To enable this feature, please update Git.)r   is_validgit_versionr   joinr   r   )r;   repogit_version_stringmin_version_strings       r-   rd   rd   =  sH    #$$DMMOOQ(.. XX%K%K$:J%K%K%KKK XX%J%J/%J%J%JJJB4MMMMB2(: 2 2.2 2 2	
 	
 	
 	

 	L	
 	
 	
 	
 	BxPPPPPP'Q Q((..r/   flag_optionsc                 n    d |                                  D             }t          j        d|           dS )a;  Load config options from config.toml files, then overlay the ones set by
    flag_options.

    The "streamlit run" command supports passing Streamlit's config options
    as flags. This function reads through the config options set via flag,
    massages them, and passes them to get_config_options() so that they
    overwrite config option defaults and those loaded from config.toml files.

    Parameters
    ----------
    flag_options : Dict[str, Any]
        A dict of config options where the keys are the CLI flag version of the
        config option names.
    c                 F    i | ]\  }}||                     dd          |S )N_r   )replace)r   namer   s      r-   
<dictcomp>z'load_config_options.<locals>.<dictcomp>g  s9       D#? 	S#??r/   T)force_reparseoptions_from_flagsN)itemsr   get_config_options)r   r   s     r-   load_config_optionsr   X  sL     %++--   DEWXXXXXXr/   c                       fd}t           D ]1}t          j                            |          rt	          ||           2d S )Nc                 &    t                     d S r)   )r   )_pathr   s    r-   on_config_changedz3_install_config_watchers.<locals>.on_config_changeds  s    L)))))r/   )r   r?   r=   existsr   )r   r   filenames   `  r-   _install_config_watchersr   r  s_    * * * * * % 4 47>>(## 	4x!23334 4r/   main_script_path_strc                     dt           dd fd}t          |           }|j        dz  }t          t          |          |dd           d S )Nr   r&   c                 "    t                       d S r)   r   )r   s    r-   _on_pages_changedz1_install_pages_watcher.<locals>._on_pages_changed|  s         r/   pagesz*.pyT)glob_patternallow_nonexistent)r   r   parentr   )r   r   r;   	pages_dirs       r-   _install_pages_watcherr   {  su    ! ! ! ! ! ! 011 ''1II	     r/   is_helloc                 V   t          |            t                       t                       t          | |           t	                       t                       t          |           t          |            t          | |          dfd}t          j
         |                       dS )zuRun a script in a separate thread and start a server for the app.

    This starts a blocking asyncio eventloop.
    r&   Nc                     K                                      d {V  t                      t                       j         d {V  d S r)   )startro   r:   stopped)r%   s   r-   
run_serverzrun.<locals>.run_server  sg      llnn    	v&&& nr/   r&   N)rA   rH   rR   rW   rt   r|   r   r   r   rN   run)r;   r   rS   r   r   r%   s        @r-   r   r     s     "###"D)))"$$$,...\***+,,, $h//F      K

r/   r   )<rN   r?   r3   r6   pathlibr   typingr   r   r   r   	streamlitr   r   r	   r
   r   r   r   r   streamlit.configr   streamlit.git_utilr   r   streamlit.loggerr   streamlit.source_utilr   streamlit.watcherr   r   r   streamlit.web.serverr   r   r   __name__r1   styler   r   r:   r   rA   rH   rR   rW   ro   rt   r|   rh   re   boolrf   rd   r   r   r   r   rs   r/   r-   <module>r      s	    				  



       " " " " " " " " " " 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 . - - - - - 7 7 7 7 7 7 7 7 ' ' ' ' ' ' 8 8 8 8 8 8 Q Q Q Q Q Q Q Q Q Q S S S S S S S S S S	H		 5;2vD   ek#&)))u{<4HHH  $ 4 66 6d 6 6 6 6:C :D : : : :   :P P P P@/C /tCy /T / / / /!=V != != != != !=HE E E E  2& & & &
     2; ;$ ; ; ; ;|Q3 Q4 Q Q Q Q6Yd38n Y Y Y Y Y444S> 4d 4 4 4 4     $$$ s)$ sCx.	$
 
$ $ $ $ $ $r/   