
    -e                        d Z ddlmZ ddlZ ej        e          ZddlmZ ddl	m
Z
mZ ddl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Z G d de          Zedd            ZdS )a   Provide a Bokeh Application Handler to build up documents by compiling
and executing Python source code.

This Handler is used by the Bokeh server command line tool to build
applications that run off scripts and notebooks.

.. code-block:: python

    def make_doc(doc: Document):
        # do work to modify the document, add plots, widgets, etc.
        return doc

    app = Application(FunctionHandler(make_doc))

    server = Server({'/bkapp': app}, io_loop=IOLoop.current())
    server.start()

    )annotationsN)contextmanager)basenamesplitext)
ModuleType)AnyCallableClassVar   )PathLike)Document)curdocpatch_curdoc   )
CodeRunner)Handler)CodeHandlerc                       e Zd ZU dZg dZded<   ded<   ded<   g dd	d$ f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 xZS )*r   z0 Run source code which modifies a Document

    )output_notebookoutput_fileshowsavereset_outputdict[str, Callable[..., None]]_loggerszClassVar[str]_logger_text_originN)argvpackagesourcestrfilenamer   r   	list[str]r   ModuleType | NonereturnNonec                   t                                                       t          ||||          | _        i | _        t
          j        D ]}|                     |          | j        |<    dS )a&  

        Args:
            source (str) : python source code

            filename (str) : a filename to use in any debugging or error output

            argv (list[str], optional) : a list of string arguments to make
                available as ``sys.argv`` when the code executes

        )r   N)super__init__r   _runnerr   r   _io_functions_make_io_logger)selfr    r"   r   r   f	__class__s         ?lib/python3.11/site-packages/bokeh/application/handlers/code.pyr)   zCodeHandler.__init__R   ss     	!&(D'JJJ* 	7 	7A#33A66DM!	7 	7    
str | Nonec                    | j         j        S )zE If the handler fails, may contain a related error message.

        )r*   errorr-   s    r0   r4   zCodeHandler.errorh   s    
 |!!r1   c                    | j         j        S )zJ If the handler fails, may contain a traceback or other details.

        )r*   error_detailr5   s    r0   r7   zCodeHandler.error_detailo   s    
 |((r1   boolc                    | j         j        S )z; ``True`` if the handler failed to modify the doc

        )r*   failedr5   s    r0   r:   zCodeHandler.failedv   s    
 |""r1   c                    | j         j         S )z Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if the code has already been executed.

        )r*   ranr5   s    r0   safe_to_forkzCodeHandler.safe_to_fork}   s     <###r1   docr   c                x   | j                                         }|dS |j                            |           t	          | j                  5  t          |          5  | j                             ||                     |                     ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z Run Bokeh application code to update a ``Document``

        Args:
            doc (Document) : a ``Document`` to update

        N)	r*   
new_modulemodulesadd_monkeypatch_ior   r   run_make_post_doc_check)r-   r>   modules      r0   modify_documentzCodeHandler.modify_document   s[    ((** >F 	T]++ 	I 	Ic"" I I  )B)B3)G)GHHHI I I I I I I I I I I I I I I	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is6   B//BB/B	B/B	B//B36B3c                r    | j         rdS dt          t          | j        j                            d         z   S )zO The last path component for the basename of the configured filename.

        N/r   )r:   r   r   r*   pathr5   s    r0   url_pathzCodeHandler.url_path   s:     ; 	4 Xht|'899::1===r1   nameCallable[..., None]c                     d fd}|S )Nargsr   kwargsr%   r&   c                 ^    t                               j        j        j                   d S )N)loginfor   r*   rJ   )rO   rP   rL   r-   s     r0   loggerz+CodeHandler._make_io_logger.<locals>.logger   s'    HHT&(94@@@@@r1   )rO   r   rP   r   r%   r&    )r-   rL   rT   s   `` r0   r,   zCodeHandler._make_io_logger   s3    	A 	A 	A 	A 	A 	A 	Ar1   Callable[[], None]c                     d fd}|S )Nr%   r&   c                 p    t                       ur$t          j         dj        j        d          d S )Nz at z replaced the output document)r   RuntimeErrorr   r*   rJ   )r>   r-   s   r0   funcz.CodeHandler._make_post_doc_check.<locals>.func   s@    xxs"""dl#j#j8I#j#j#jkkk #"r1   )r%   r&   rU   )r-   r>   rZ   s   `` r0   rE   z CodeHandler._make_post_doc_check   s5    	l 	l 	l 	l 	l 	l 	l r1   )
r    r!   r"   r   r   r#   r   r$   r%   r&   )r%   r2   )r%   r8   )r>   r   r%   r&   )rL   r!   r%   rM   )r>   r   r%   rV   )__name__
__module____qualname____doc__r+   __annotations__r)   propertyr4   r7   r:   r=   rG   rK   r,   rE   __classcell__)r/   s   @r0   r   r   C   s_          WVVM,,,,MOnr 7 7 7 7 7 7 7 7, " " " X" ) ) ) X) # # # X# $ $ $ X$I I I I2> > > >          r1   r   loggersr   r%   dict[str, Any]c              #     K   dd l m} i }t          j        D ],}t	          ||          ||<   t          ||| |                    -d V  |D ]}t          ||||                    d S )Nr   )bokeh.ioior   r+   getattrsetattr)rb   rf   oldr.   s       r0   rC   rC      s      C& # #QAAwqz""""	EEE  As1v r1   )rb   r   r%   rc   ) r^   
__future__r   logging	getLoggerr[   rR   
contextlibr   os.pathr   r   typesr   typingr   r	   r
   
core.typesr   documentr   io.docr   r   code_runnerr   handlerr   __all__r   rC   rU   r1   r0   <module>rw      s`   , # " " " " " g!! & % % % % % & & & & & & & &       * * * * * * * * * * # " " " " "             * * * * * * * * # # # # # #      u u u u u' u u u|      r1   