
    -e'                        d Z ddlmZ ddlZ ej        e          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 ddlmZmZ erdd	lmZ d
dlmZ dZ G d d          ZddZdS )a   Provide a base class for Bokeh Application handler classes.

When a Bokeh server session is initiated, the Bokeh server asks the Application
for a new Document to service the session. To do this, the Application first
creates a new empty Document, then it passes this new Document to the
``modify_document`` method of each of its handlers. When all handlers have
updated the Document, it is used to service the user session.

Below is an example outline of a custom handler that might modify documents
based off information in some database:

.. code-block:: python

    class DatabaseHandler(Handler):
        """ A Bokeh Application handler to initialize Documents from a database

        """

        def modify_document(self, doc: Document) -> None:
            # do some data base lookup here to generate 'plot'

            # add the plot to the document (i.e modify the document)
            doc.add_root(plot)

    )annotationsN)TYPE_CHECKINGAny   )Document   )ServerContextSessionContext)HTTPServerRequest   )
CodeRunner)Handlerc                      e Zd ZU dZded<   ded<   ded<   ded<   d!d
Zed"d            Zed"d            Zed#d            Z	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 S )(r   zV Provide a mechanism for Bokeh applications to build up new Bokeh
    Documents.

    bool_failed
str | None_error_error_detail_staticreturnNonec                >    d| _         d | _        d | _        d | _        d S )NF)r   r   r   r   selfs    Blib/python3.11/site-packages/bokeh/application/handlers/handler.py__init__zHandler.__init__W   s#    !    c                    | j         S )zE If the handler fails, may contain a related error message.

        )r   r   s    r   errorzHandler.error_   s    
 {r   c                    | j         S )zJ If the handler fails, may contain a traceback or other details.

        )r   r   s    r   error_detailzHandler.error_detailf   s    
 !!r   c                    | j         S )z; ``True`` if the handler failed to modify the doc

        )r   r   s    r   failedzHandler.failedm   s    
 |r   c                    dS )NT r   s    r   safe_to_forkzHandler.safe_to_forkt   s    tr   docr   c                     t          d          )a   Modify an application document in a specified manner.

        When a Bokeh server session is initiated, the Bokeh server asks the
        Application for a new Document to service the session. To do this,
        the Application first creates a new empty Document, then it passes
        this Document to the ``modify_document`` method of each of its
        handlers. When all handlers have updated the Document, it is used to
        service the user session.

        *Subclasses must implement this method*

        Args:
            doc (Document) : A Bokeh Document to update in-place

        Returns:
            Document

        zimplement modify_document())NotImplementedError)r   r'   s     r   modify_documentzHandler.modify_documentz   s    & ""?@@@r   server_contextr	   c                    dS )a.   Execute code when the server is first started.

        Subclasses may implement this method to provide for any one-time
        initialization that is necessary after the server starts, but
        before any sessions are created.

        Args:
            server_context (ServerContext) :

        Nr%   r   r+   s     r   on_server_loadedzHandler.on_server_loaded   s	     	r   c                    dS )a   Execute code when the server cleanly exits. (Before stopping the
        server's ``IOLoop``.)

        Subclasses may implement this method to provide for any one-time
        tear down that is necessary before the server exits.

        Args:
            server_context (ServerContext) :

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

        Nr%   r-   s     r   on_server_unloadedzHandler.on_server_unloaded   s	     	r   session_contextr
   c                
   K   dS )a#   Execute code when a new session is created.

        Subclasses may implement this method to provide for any per-session
        initialization that is necessary before ``modify_doc`` is called for
        the session.

        Args:
            session_context (SessionContext) :

        Nr%   r   r1   s     r   on_session_createdzHandler.on_session_created   s       	r   c                
   K   dS )a    Execute code when a session is destroyed.

        Subclasses may implement this method to provide for any per-session
        tear-down that is necessary when sessions are destroyed.

        Args:
            session_context (SessionContext) :

        Nr%   r3   s     r   on_session_destroyedzHandler.on_session_destroyed   s       	r   requestr   dict[str, Any]c                    i 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%   )r   r7   s     r   process_requestzHandler.process_request   s	     	r   c                "    | j         rdS | j        S )zI Return a path to app-specific static resources, if applicable.

        N)r#   r   r   s    r   static_pathzHandler.static_path   s     ; 	 4<r   c                    dS )aR   Returns a default URL path, if applicable.

        Handlers subclasses may optionally implement this method, to inform
        the Bokeh application what URL it should be installed at.

        If multiple handlers specify ``url_path`` the Application will use the
        value from the first handler in its list of handlers.

        Nr%   r   s    r   url_pathzHandler.url_path   s	     tr   N)r   r   )r   r   )r   r   )r'   r   r   r   )r+   r	   r   r   )r1   r
   r   r   )r7   r   r   r8   )__name__
__module____qualname____doc____annotations__r   propertyr   r!   r#   r&   r*   r.   r0   r4   r6   r:   r<   r>   r%   r   r   r   r   L   s         
 MMM       X " " " X"    X    X
A A A A*      "   
 
 
 
          
 
 
 
 
 
r   r   handlerHandler | CodeRunnere	Exceptionr   r   c           	        d| _         t          j                    | _        t	          j                    \  }}}t          j        |          d         \  }}}}t          j        	                    |          }| d|d| d| d| 	| _
        dS )z4 Record an exception and details on a Handler.

    Tz
File z, line z, in z:
N)r   	traceback
format_excr   sysexc_info
extract_tbospathbasenamer   )	rE   rG   _exc_tracebackfilenameline_numberfunctxtrR   s	            r   handle_exceptionrY      s     GO%022G,..Aq-'0';M'J'J2'N$Hk4w))HUU(UU[UUtUUPSUUGNNNr   )rE   rF   rG   rH   r   r   )rB   
__future__r   logging	getLoggerr?   logrP   rM   rK   typingr   r   documentr   applicationr	   r
   tornado.httputilr   code_runnerr   __all__r   rY   r%   r   r   <module>rd      s*   : # " " " " " g!! 
			 



     % % % % % % % % !           7 7 7 7 7 7 7 7 (222222''''''Z Z Z Z Z Z Z ZzV V V V V Vr   