U
    /ek                     @  s~   d Z ddlmZ ddlZeeZddl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ZG dd deZdS )a   Provide a Bokeh Application Handler to build up documents by running
the code from Jupyter notebook (``.ipynb``) files.

This handler is configured with the filename of a Jupyter notebook. When a
Bokeh application calls ``modify_doc``, the code from all the notebook cells
is collected and executed to process a new Document for a session. When the
notebook code is executed, the Document being modified will be available as
``curdoc``, and any optionally provided ``args`` will be available as
``sys.argv``.

    )annotationsN)
ModuleType)List   )PathLike)import_required   )CodeHandler)NotebookHandlerc                      s<   e Zd ZdZdZdZg dddddd	d
 fddZ  ZS )r
   zo A Handler that uses code in a Jupyter notebook for modifying Bokeh
    Documents.

    .. autoclasstoc::

    zI%s: call to %s() ignored when running notebooks with the 'bokeh' command.ZNotebookN)argvpackager   z	List[str]zModuleType | NoneNone)filenamer   r   returnc             	     s   t dd}t dd}G dd d|jj}| g}t|ddV}|||j}	| }
|D ]}|
| qX|
|	\}}|	dd	}|	d
d	}W 5 Q R X t
 j||||d dS )zg

        Keywords:
            filename (str) : a path to a Jupyter notebook (".ipynb") file

        nbformatzQThe Bokeh notebook application handler requires Jupyter Notebook to be installed.	nbconvertc                   @  s:   e Zd ZdZedZdddddZdd Zd	d
 Z	dS )z6NotebookHandler.__init__.<locals>.StripMagicsProcessorz
            Preprocessor to convert notebooks to Python source while stripping
            out all magics (i.e IPython specific syntax).
            z^\s*(?P<magic>%%\w\w+)($|(\s+))str)sourcer   c                 S  s`   g }|  D ]H}| j|}|dkr0|| qd}|j| j|dd}t| qd	|S )zb
                Given the source of a cell, filter out all cell and line magics.
                Nz7Stripping out IPython magic {magic} in code cell {cell}magic)cellr   
)

splitlines_magic_patternmatchappendformat_cell_countergrouplogwarningjoin)selfr   filteredliner   msgmessage r&   G/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/application/handlers/notebook.pystrip_magicsW   s    zCNotebookHandler.__init__.<locals>.StripMagicsProcessor.strip_magicsc                 S  s4   |d dkr,|  j d7  _ | |d |d< ||fS )NZ	cell_typecoder   r   )r   r(   )r!   r   	resourcesindexr&   r&   r'   preprocess_cellf   s    zFNotebookHandler.__init__.<locals>.StripMagicsProcessor.preprocess_cellc                 S  s   d| _ | ||S )Nr   )r   
preprocess)r!   nbr*   r&   r&   r'   __call__l   s    z?NotebookHandler.__init__.<locals>.StripMagicsProcessor.__call__N)
__name__
__module____qualname____doc__recompiler   r(   r,   r/   r&   r&   r&   r'   StripMagicsProcessorO   s
   
r6   zutf-8)encodingzget_ipython().run_line_magic zget_ipython().magic)r   r   r   r   N)r   preprocessorsZPreprocessoropenreadZ
NO_CONVERTZPythonExporterZregister_preprocessorZfrom_notebook_nodereplacesuper__init__)r!   r   r   r   r   r   r6   r9   fr.   Zexporterpreprocessorr   _	__class__r&   r'   r>   E   s    

!zNotebookHandler.__init__)r0   r1   r2   r3   Z_logger_textZ_originr>   __classcell__r&   r&   rB   r'   r
   9   s   r
   )r3   
__future__r   logging	getLoggerr0   r   r4   typesr   typingr   Z
core.typesr   Zutil.dependenciesr   r)   r	   __all__r
   r&   r&   r&   r'   <module>   s   
