
    xi`>                    `   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Zd dlmZm	Z	 d dl
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mZmZ  ee      Zded	<   d
ZddZddZddZ ddZ!d dZ"ddZ#ddZ$ddZ%ddZ&ddZ'd!dZ(d"dZ)d"dZ*	 	 	 	 	 	 	 	 	 	 d#dZ+dd	 	 	 	 	 	 	 	 	 	 	 d$dZ,y)%    )annotationsN)AnyFinal)cli_utilconfigenv_util	file_utilnet_utilsecrets)
get_logger)report_watchdog_availability
watch_file)Serverserver_address_is_unix_socketserver_utilr   _LOGGERi   @c                    t         j                  d       d fd}t        j                  t        j                  |       t        j                  t        j                  |       t
        j                  dk(  r%t        j                  t        j                  |       y t        j                  t        j                  |       y )NzSetting up signal handlerc                &    j                          y N)stop)signal_numberstack_frameservers     c/var/www/html/chatbot/card-advisor-bot/venv/lib/python3.12/site-packages/streamlit/web/bootstrap.pysignal_handlerz._set_up_signal_handler.<locals>.signal_handler)   s        win32)r   intr   r   returnNone)	r   debugsignalSIGTERMSIGINTsysplatformSIGBREAKSIGQUIT)r   r   s   ` r   _set_up_signal_handlerr)   &   sk    MM-. MM&...1
MM&--0
||wfoo~6fnnn5r   c                ~    t         j                  j                  dt        j                  j	                  |              y)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)r%   pathinsertosdirnamemain_script_paths    r   _fix_sys_pathr1   5   s$     HHOOArww'789r   c                    | ryt         j                  ry	 ddl}	 |j	                          t
        j                  d       y# t        $ r Y yw xY w# t        $ r t
        j                  dd       Y yw xY w)z=Install uvloop as the default event loop policy if available.Nr   z.uvloop installed as default event loop policy.z7Failed to install uvloop. Falling back to default loop.T)exc_info)	r   
IS_WINDOWSuvloopModuleNotFoundErrorinstallr   r!   	Exceptionwarning)running_in_event_loopr5   s     r   _maybe_install_uvloopr;   >   su     
FG    
EPT 	 	

s"   A %A 	AA A32A3c                     t         j                  rD	 ddlm} m} t        t        j                               | u rt        j                   |              yyy# t        $ r Y yw xY w)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   r4   asyncior>   r?   typeget_event_loop_policyset_event_loop_policyImportErrorr=   s     r   _fix_tornado_crashrE   U   sd      	P G11348VV --.L.NO W   		s   A 	A"!A"c                2    ddl }| gt        |      |_        y)ztsys.argv needs to exclude streamlit arguments and parameters
    and be set to what a user's script may expect.
    r   N)r%   listargv)r0   argsr%   s      r   _fix_sys_argvrJ   u   s      .4:.CHr   c                    t        | j                         t        | j                         t	                dd}t        j                         j                  |       y )Nc                 F   t        j                  d      ry t        j                  d      rt        j                  d      } n8t        j                  d      r!t               ry t        j                  d      } nd} t	        j
                  t        j                  |              y )Nserver.headlessbrowser.serverAddressserver.address	localhost)r   
get_optionis_manually_setr   r   open_browserr   get_url)addrs    r   maybe_open_browserz,_on_server_start.<locals>.maybe_open_browser   s{    ./!!"9:$$%<=D##$45,.$$%56DDk11$78r   r   r    )prepare_streamlit_environmentr0   
_print_urlis_running_hellor   r@   get_running_loop	call_soon)r   rV   s     r   _on_server_startr]   ~   sC    !&"9"9:v&&' "9$ (();<r   c                 v    dt         j                  vr't        j                  d      t         j                  d<   yy)ziSets MAPBOX_API_KEY environment variable needed for PyDeck otherwise it
    will throw an exception.
    MAPBOX_API_KEYzmapbox.tokenN)r-   environr   rQ    r   r   _fix_pydeck_mapbox_api_warningrb      s/    
 rzz)'-'8'8'H

#$ *r   c                     t        j                  dd       t        j                  dd       t        j                  dd       t        j                  dd       t        j                  dd	       y
)zEnsure common MIME types are correctly registered.

    Some systems may have misconfigured /etc/mime.types, so we explicitly
    register the types we need for serving web assets correctly.
    z	text/htmlz.htmlzapplication/javascriptz.jsz.mjsztext/cssz.cssz
image/webpz.webpN)	mimetypesadd_typera   r   r   _initialize_mimetypesrf      sV     {G,/7/8z6*|W-r   c                    t                t                	 t        j                          t        |        y# t        $ r t
        j                  d       Y ,w xY w)a   Prepare the Streamlit environment for running an app.

    This function sets up the environment needed for Streamlit to run correctly.
    It should be called before starting the runtime, whether using the CLI
    (`streamlit run`) or an ASGI server (`uvicorn myapp:app`).

    This function:
    - Ensures common MIME types are correctly registered
    - Sets the MAPBOX_API_KEY environment variable for PyDeck
    - Loads secrets from secrets.toml if it exists
    - Validates static folder configuration

    Parameters
    ----------
    main_script_path
        Path to the main Streamlit script.

    Notes
    -----
    This function is automatically called by ``streamlit run``. When using
    ``st.App`` with uvicorn directly, this is called during the ASGI lifespan
    startup phase.
    z Failed to load secrets.toml fileN)rf   rb   r   load_if_toml_existsr8   r   	exception"_maybe_print_static_folder_warningr/   s    r   rX   rX      sL    0 "$>##% ''78  ><=>s   6 AAc                h   t        j                  d      rt        j                  |       }t        j
                  j                  |      st        j                  d| dd       yt        j                  |      }|t        kD  r.t        j                  dd       t        j                  dd       yyy)	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.yellow)fgFzVWARNING: Static folder size is larger than 1GB. Static file serving has been disabled.N)r   rQ   r	   get_app_static_dirr-   r+   isdirr   print_to_cliget_directory_sizeMAX_APP_STATIC_FOLDER_SIZE
set_option)r0   static_folder_pathstatic_folder_sizes      r   rj   rj      s     56&99:JKww}}/0!!() *;< 	 "+!=!=>P!Q!$>>!!">F%%= ? 7r   c                "   | rd}nd}g }t        j                  d      r-dt        j                  t        j                  d            fg}nt        j                  d      r6t               s,dt        j                  t        j                  d            fg}nt               rdt        j                  d      fg}ndt        j                  d      fg}t        j                         }|r&|j                  d	t        j                  |      f       t        j                  d
      r<t        j                         }|r&|j                  dt        j                  |      f       t        j                  d       t        j                  d| dd       t        j                  d       |D ]8  \  }}t        j                  d| ddd       t        j                  |d       : 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       y y )Nz9Welcome to Streamlit. Check out our demo in your browser.z4You can now view your Streamlit app in your browser.rN   URLrO   zUnix Socketz	Local URLrP   zNetwork URLrM   zExternal URL z  blueT)rm   boldz: F)nlrm   )rz   z5  Ready to create your own Python apps super quickly?z  Head over to )r{   zhttps://docs.streamlit.ioz  May you create awesome apps!)r   rR   r   rT   rQ   r   r
   get_internal_ipappendget_external_ipr   rp   )rZ   title_message
named_urlsinternal_ipexternal_ipurl_nameurls          r   rY   rY      s+   SNJ56K''(9(9:Q(RST


 	/09V9X K''(9(9:J(KLM

 
'	(F--.>?@

 +--k:;

 ..0}k.A.A+.NOP./"224K!!>;3F3F{3S"TU"B}o.6E"# .#8*B/EfEc-. "UV/E:9Eb!>?b!b! r   c                    | j                         D ci c]  \  }}||dk7  r|j                  dd      |! }}}t        j                  d|       yc c}}w )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.
    Nra   _.T)force_reparseoptions_from_flags)itemsreplacer   get_config_options)flag_optionsnamevalr   s       r   load_config_optionsr   &  sb    ( &++-D#?sby 	S##  DEWXs   $Ac                     d fd}t        j                  d      D ].  }t        j                  j	                  |      s#t        ||       0 y )Nc                    t               y r   )r   )_pathr   s    r   on_config_changedz3_install_config_watchers.<locals>.on_config_changedD  s    L)r   zconfig.toml)r   strr   r    )r   get_config_filesr-   r+   existsr   )r   r   filenames   `  r   _install_config_watchersr   C  s?    * ++M: 477>>(#x!234r   c                    ddl m} t        |        t        | |       t	        |       dt
        _        t                 ||      }|j                          y)a  Run a Streamlit st.App with uvicorn.

    This function is called when `streamlit run` detects an st.App instance
    in the script. It sets up the process environment and starts uvicorn.

    The App instance handles its own Streamlit-specific setup via its lifespan
    (e.g., loading secrets, preparing runtime), while this function handles
    the process-level setup that the CLI is responsible for.

    Parameters
    ----------
    main_script_path
        Path to the main Streamlit script containing the st.App.
    app_import_string
        Import string for uvicorn (e.g., "myapp:app").
    args
        Arguments to pass to the script (sys.argv).
    flag_options
        Config options from CLI flags.
    r   )UvicornRunnerzstarlette-appN)	/streamlit.web.server.starlette.starlette_serverr   r1   rJ   r   r   _server_moder   run)r0   app_import_stringrI   r   r   runners         r   run_asgi_appr   L  sN    4 N "#"D)\* *F !" ,-F
JJLr   F)stop_immediately_for_testingc               &  	 t        |        t                t        | |       t        |       t	        j
                  d      rdt        _        ndt        _        t        | |      	d	fddfd}d}	 t        j                          d}|r9t        j                  d       t        j                   |       d	
      }|	_        yt        |       t        j                  d       t        j                    |              y# t        $ r Y w xY w)zuRun a script in a separate thread and start a server for the app.

    This starts a blocking asyncio eventloop.
    zserver.useStarlettezstarlette-managedtornadoc                    K    j                          d {    t                t                r%t        j	                  d        j                           j                   d {    y 7 V7 w)Nz'Stopping server immediately for testing)startr]   r)   r   r!   r   stopped)r   r   s   r   
run_serverzrun.<locals>.run_server  s]     lln  	v& (MMCDKKM nn 	 	s"   A2A.AA2(A0)A20A2c                 0   K             d {    y 7 wr   ra   )r   s   r   mainzrun.<locals>.main  s     ls   FTz&Running server in existing event loop.zbootstrap.run_server)r   z"Starting new event loop for serverNrW   )r1   rE   rJ   r   r   rQ   r   r   r@   r[   RuntimeErrorr   r!   create_task_bootstrap_taskr;   r   )
r0   is_hellorI   r   r   r   r:   taskr   r   s
       `   @@r   r   r   {  s     "#"D)\* ./1' $h/F& "  " $
 >?""460FG "&34 	:;DF!  s   8D 	DD)r   r   r   r    )r0   r   r   r    )r:   boolr   r    rW   )r0   r   rI   	list[str]r   r    )rZ   r   r   r    )r   dict[str, Any]r   r    )
r0   r   r   r   rI   r   r   r   r   r    )r0   r   r   r   rI   r   r   r   r   r   r   r    )-
__future__r   r@   rd   r-   r"   r%   typingr   r   	streamlitr   r   r   r	   r
   r   streamlit.loggerr   streamlit.watcherr   r   streamlit.web.serverr   r   r   __name__r   __annotations__rr   r)   r1   r;   rE   rJ   r]   rb   rf   rX   rj   rY   r   r   r   r   ra   r   r   <module>r      s	   #   	  
  N N ' F S SH% % 4 6:
.P@/=4I
.!9H28"vY:4,,, , !	,
 
,j */HHH H !	H #'H 
Hr   