
    -eO                        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
 ddlmZ dd	lmZmZ d
Zee
gdf         Z G d de          ZdS )aX   Provide a Bokeh Application Handler to build up documents by running
a specified Python function.

This Handler is not used by the Bokeh server command line tool, but is often
useful if users wish to embed the Bokeh server programmatically:

.. 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()

For complete examples of this technique, see :bokeh-tree:`examples/server/api`

    )annotationsN)Callable   )Document)_check_callback   )Handlerhandle_exception)FunctionHandlerc                  j     e Zd ZU dZded<   ded<   ded<   ddd fdZedd            ZddZ xZ	S )r   aI   A Handler that accepts a plain python function to use for modifying
    Bokeh Documents.

    For example, the following code configures a handler with a function that
    adds an empty plot to a Document:

    .. code-block:: python

        def add_empty_plot(doc: Document):
            p = figure(x_range=(0, 10), y_range=(0, 10))
            doc.add_root(p)
            return doc

        handler = FunctionHandler(add_empty_plot)

    This handler could be configured on an Application, and the Application
    would run this function every time a new session is created.

    	ModifyDoc_funcbool_trap_exceptions_safe_to_forkF)trap_exceptionsfuncr   returnNonec                   t                                                       t          |d           || _        || _        d| _        dS )a0  

        Args:
            func (callable) : a function to modify and return a Bokeh Document.
                The function should have the form:

                .. code-block:: python

                    def func(doc: Document):
                        # modify doc
                        return doc

                and it  should return the passed-in document after making any
                modifications in-place.

            trap_exceptions (bool) : should exceptions in `func` be caught and
                logged or allowed to propagate

        )docTN)super__init__r   r   r   r   )selfr   r   	__class__s      Clib/python3.11/site-packages/bokeh/application/handlers/function.pyr   zFunctionHandler.__init__[   sH    ( 	h'''
 /!    c                    | j         S )z Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if ``modify_doc`` has already been called.

        )r   )r   s    r   safe_to_forkzFunctionHandler.safe_to_forky   s     !!r   r   r   c                    	 |                      |           n0# t          $ r#}| j        rt          | |           n Y d}~nd}~ww xY wd| _        dS # d| _        w xY w)z Execute the configured ``func`` to modify the document.

        After this method is first executed, ``safe_to_fork`` will return
        ``False``.

        NF)r   	Exceptionr   r
   r   )r   r   es      r   modify_documentzFunctionHandler.modify_document   s    	'JJsOOOO 	 	 	$  q)))) *))))	 "'DD&&&&s*    A 
AA A  AA 	A)r   r   r   r   r   r   )r   r   )r   r   r   r   )
__name__
__module____qualname____doc____annotations__r   propertyr   r#   __classcell__)r   s   @r   r   r   B   s          ( CH " " " " " " " "< " " " X"' ' ' ' ' ' ' 'r   r   )r'   
__future__r   logging	getLoggerr$   logtypingr   documentr   util.callback_managerr   handlerr	   r
   __all__r   r    r   r   <module>r5      s    0 # " " " " " g!!       !           4 4 4 4 4 4 . . . . . . . . hZ%&	Q' Q' Q' Q' Q'g Q' Q' Q' Q' Q'r   