
    hje*                    >   d Z ddlmZ ddlZ ej        e          Zddl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 ddlmZmZmZ dd	l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# ddl$m%Z% ddl&m'Z' ddl(m)Z) erddl*m+Z+ ddl,m-Z- dZ. G d de!          Z/dS )a    Provide a Bokeh Application Handler to build up documents by running
the code from ``main.py`` or ``main.ipynb`` files in specified directories.

The directory may also optionally contain:

* A ``server_lifecyle.py`` module to provide lifecycle callbacks for the
  application and sessions.

* A ``static`` subdirectory containing app-specific static resources to
  serve.

* A ``theme.yaml`` file containing a Bokeh theme to automatically apply to
  all new documents.

* A ``templates`` subdirectory containing templates for app display

A full directory layout might look like:

.. code-block:: none

    myapp
       |
       +---main.py
       +---server_lifecycle.py
       +---static
       +---theme.yaml
       +---templates
            +---index.html

    )annotationsN)basenamedirnameexistsjoin)
ModuleType)TYPE_CHECKINGAny	Coroutine)EnvironmentFileSystemLoaderTemplate   )PathLike)Document   )ServerContextSessionContext   )
CodeRunner)Handler)NotebookHandler)ScriptHandler)ServerLifecycleHandler)ServerRequestHandler)HTTPServerRequestTheme)DirectoryHandlerc                  
    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<   g dd- fdZed.d            Zed.d            Zed/d            Z	ed/d            Z
d0dZd1d!Zd1d"Zd2d&Zd2d'Zd3d+Zd.d,Z xZS )4r   z> Load an application directory which modifies a Document.
    zCodeRunner | None_package_runnerzModuleType | None_packager   _lifecycle_handler_request_handlerzTheme | None_theme
str | None_staticzTemplate | None	_template)argvfilenamer   r)   	list[str]returnNonec                  t                                                       |}t          |d          }t          |          rt	          |d          5 }t          |                                ||          | _        ddd           n# 1 swxY w Y   | j                                        | _	        | j	        J | j	        t          j        | j	        j        <   nd| _        d| _	        t          |d          }t          |d          }t          |          r0t          |          r!t                              d| d           |}n6t          |          r|}n$t          |          r|}nt          d	|           || _        || _        |                    d
          rt&          nt(          }	 |	| j        || j	                  | _        d}
t          |d          }t          |d          }t          |          rt          |          rt          d          t          |          r|}
nt          |          r|}
|
t-          |
|| j	                  | _        nt1                      | _        t          |          r!|
J t3          |
|| j	                  | _        nt1                      | _        d| _        t          |d          }t          |          rddlm}  ||          | _        t          |d          }t          |          r|| _        d| _        t          |dd          }t          |          rFtA          tC          tE          |                              }|#                    d          | _        dS dS )z
        Keywords:
            filename (str) : a path to an application directory with either "main.py" or "main.ipynb"

            argv (list[str], optional) : a list of string arguments to make available as sys.argv to main.py
        z__init__.pyutf8)encodingNzmain.pyz
main.ipynbz)Found both 'main.py' and 'main.ipynb' in z, using 'main.py'z No 'main.py' or 'main.ipynb' in z.ipynb)r*   r)   packagezapp_hooks.pyzserver_lifecycle.pyzVDirectory style apps can provide either server_lifecycle.py or app_hooks.py, not both.z
theme.yamlr   r   )r*   static	templatesz
index.html)loader)$super__init__r   r   openr   readr!   
new_moduler"   sysmodules__name__logwarning
ValueError_path_mainendswithr   r   _main_handlerr   r#   r   r   r$   r%   bokeh.themesr   r'   r(   r   r   r   get_template)selfr*   r)   src_pathinit_py	init_py_fmain_pymain_ipymainhandlerhooks	app_hooks	lifecycle	themeyamlr   	appstaticappindexenv	__class__s                     Dlib/python3.11/site-packages/bokeh/application/handlers/directory.pyr6   zDirectoryHandler.__init__n   s    	x//'?? 	!g/// S9'1)..2B2BGT'R'R$S S S S S S S S S S S S S S S 0;;==DM=,,,26-CK.//#'D  DMx++,//'?? 	Lvh// 	LKK_H___```DDG__ 	LDDH 	LDDJJJKKK

%)]]8%<%<O//-$WdjtT][[[>22	#899	) 		!2!2 	uvvvI 	EEI 	E&<eRV`d`m&n&n&nD##&-iiD#) 	.$$$$8%d\`\i$j$j$jD!!$+IID!<00	) 	4******%333DK8,,	) 	%$DL+|<<( 	<%5gh6G6G%H%HIIIC --l;;DNNN	< 	<s   )B		BBc                2    | j         j        p| j        j        S )zE If the handler fails, may contain a related error message.

        )rC   errorr#   rF   s    rV   rX   zDirectoryHandler.error   s    
 !'H4+B+HH    c                2    | j         j        p| j        j        S )zJ If the handler fails, may contain a traceback or other details.

        )rC   error_detailr#   rY   s    rV   r\   zDirectoryHandler.error_detail   s    
 !.V$2I2VVrZ   boolc                2    | j         j        p| j        j        S )z; ``True`` if the handler failed to modify the doc

        )rC   failedr#   rY   s    rV   r_   zDirectoryHandler.failed   s    
 !(JD,C,JJrZ   c                    | j         j        S )z Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if the configured code (script, notebook, etc.) has already
        been run.

        )rC   safe_to_forkrY   s    rV   ra   zDirectoryHandler.safe_to_fork   s     !..rZ   docr   c                    | j         j        rdS | j        | j        |_        | j        | j        |_        | j                            |           dS )a   Execute the configured ``main.py`` or ``main.ipynb`` to modify the
        document.

        This method will also search the app directory for any theme or
        template files, and automatically configure the document with them
        if they are found.

        N)r#   r_   r%   themer(   templaterC   modify_document)rF   rb   s     rV   rf   z DirectoryHandler.modify_document   sZ     ") 	F ;"CI>%>CL 	**3/////rZ   server_contextr   c                    | j         r&| j        r| j                             | j                   | j                            |          S )z Execute `on_server_unloaded`` from ``server_lifecycle.py`` (if
        it is defined) when the server is first started.

        Args:
            server_context (ServerContext) :

        )r!   r"   runr#   on_server_loadedrF   rg   s     rV   rj   z!DirectoryHandler.on_server_loaded   sH      	4DM 	4 $$T]333&77GGGrZ   c                6    | j                             |          S )am   Execute ``on_server_unloaded`` from ``server_lifecycle.py`` (if
        it is defined) when the server cleanly exits. (Before stopping the
        server's ``IOLoop``.)

        Args:
            server_context (ServerContext) :

        .. warning::
            In practice this code may not run, since servers are often killed
            by a signal.


        )r#   on_server_unloadedrk   s     rV   rm   z#DirectoryHandler.on_server_unloaded   s     &99.IIIrZ   session_contextr   Coroutine[Any, Any, None]c                6    | j                             |          S )z Execute ``on_session_created`` from ``server_lifecycle.py`` (if
        it is defined) when a new session is created.

        Args:
            session_context (SessionContext) :

        )r#   on_session_createdrF   rn   s     rV   rq   z#DirectoryHandler.on_session_created  s     &99/JJJrZ   c                6    | j                             |          S )z Execute ``on_session_destroyed`` from ``server_lifecycle.py`` (if
        it is defined) when a session is destroyed.

        Args:
            session_context (SessionContext) :

        )r#   on_session_destroyedrr   s     rV   rt   z%DirectoryHandler.on_session_destroyed  s     &;;OLLLrZ   requestr   dict[str, Any]c                6    | j                             |          S )a#   Processes incoming HTTP request returning a dictionary of
        additional data to add to the session_context.

        Args:
            request: HTTP request

        Returns:
            A dictionary of JSON serializable data to be included on
            the session context.
        )r$   process_request)rF   ru   s     rV   rx   z DirectoryHandler.process_request$  s     $44W===rZ   c                B    | j         rdS dt          | j                  z   S )zd The last path component for the basename of the path to the
        configured directory.

        N/)r_   r   r@   rY   s    rV   url_pathzDirectoryHandler.url_path1  s*    
 ; 	.4 $*----rZ   )r*   r   r)   r+   r,   r-   )r,   r&   )r,   r]   )rb   r   r,   r-   )rg   r   r,   r-   )rn   r   r,   ro   )ru   r   r,   rv   )r<   
__module____qualname____doc____annotations__r6   propertyrX   r\   r_   ra   rf   rj   rm   rq   rt   rx   r{   __classcell__)rU   s   @rV   r   r   `   s          '&&&@B J< J< J< J< J< J< J< J<\ I I I XI W W W XW K K K XK / / / X/0 0 0 0,H H H HJ J J J K K K KM M M M> > > >	. 	. 	. 	. 	. 	. 	. 	.rZ   r   )0r~   
__future__r   logging	getLoggerr<   r=   r:   os.pathr   r   r   r   typesr   typingr	   r
   r   jinja2r   r   r   
core.typesr   documentr   applicationr   r   code_runnerr   rM   r   notebookr   scriptr   server_lifecycler   server_request_handlerr   tornado.httputilr   themesr   __all__r    rZ   rV   <module>r      s   D # " " " " " g!! 


                  0 0 0 0 0 0 0 0 0 0 ; : : : : : : : : : # " " " " "             7 7 7 7 7 7 7 7 # # # # # #       % % % % % % ! ! ! ! ! ! 4 4 4 4 4 4 8 8 8 8 8 8  222222Z. Z. Z. Z. Z.w Z. Z. Z. Z. Z.rZ   