U
    õÕ/en  ã                   @  s|   d Z ddlmZ ddlZe 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„ deƒZdS )a`   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/howto/server_embed`

é    )ÚannotationsN)ÚCallableé   )ÚDocument)Ú_check_callbacké   )ÚHandlerÚhandle_exception)ÚFunctionHandlerc                      sl   e Zd ZU dZded< ded< ded< ddœddd	d
œ‡ fdd„Zeddœdd„ƒZdd	dœdd„Z‡  Z	S )r
   a`   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.

    .. autoclasstoc::

    Ú	ModifyDocÚ_funcÚboolÚ_trap_exceptionsÚ_safe_to_forkF)Útrap_exceptionsÚNone)Úfuncr   Úreturnc                  s*   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__© úG/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/application/handlers/function.pyr   ^   s
    

zFunctionHandler.__init__)r   c                 C  s   | 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   r   r   r   Úsafe_to_fork|   s    zFunctionHandler.safe_to_forkr   )r   r   c              
   C  sX   zJz|  |¡ W n6 tk
rF } z| jr4t| |ƒ n‚ W 5 d}~X Y nX W 5 d| _ X dS )z  Execute the configured ``func`` to modify the document.

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

        FN)r   r   Ú	Exceptionr   r	   )r   r   Úer   r   r   Úmodify_document‡   s    zFunctionHandler.modify_document)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úpropertyr   r   Ú__classcell__r   r   r   r   r
   C   s   

r
   )r#   Ú
__future__r   ÚloggingÚ	getLoggerr    ÚlogÚtypingr   Údocumentr   Zutil.callback_managerr   Úhandlerr   r	   Ú__all__r   r
   r   r   r   r   Ú<module>   s   
