B
    @\                 @   sn   d Z ddlmZmZmZmZ ddlZddlZddlZe	e
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``.

    )absolute_importdivisionprint_functionunicode_literalsN   )import_required   )CodeHandler)NotebookHandlerc                   s(   e Zd ZdZdZdZ fddZ  ZS )r
   zX A Handler that uses code in a Jupyter notebook for modifying Bokeh
    Documents.

    zI%s: call to %s() ignored when running notebooks with the 'bokeh' command.ZNotebookc          	      s   t dd}t dd}d|kr$tdG dd d|jj}| g}|d }t|}|||j}	| }
x|D ]}|
| qnW |
	|	\}}|
dd	}|
d
d	}tjjdkrt|tr|d}||d< W dQ R X tt| j|| 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.	nbconvertfilenamez'Must pass a filename to NotebookHandlerc               @   s2   e Zd ZdZedZ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+))c             S   sd   g }xT|  D ]H}| j|}|dkr2|| qd}|j| j|dd}t| qW 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warnjoin)selfsourceZfilteredliner   msgmessage r    Blib/python3.7/site-packages/bokeh/application/handlers/notebook.pystrip_magicsY   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_cellh   s    zFNotebookHandler.__init__.<locals>.StripMagicsProcessor.preprocess_cellc             S   s   d| _ | ||S )Nr   )r   Z
preprocess)r   nbr$   r    r    r!   __call__n   s    z?NotebookHandler.__init__.<locals>.StripMagicsProcessor.__call__N)
__name__
__module____qualname____doc__recompiler   r"   r&   r(   r    r    r    r!   StripMagicsProcessorQ   s
   
r/   zget_ipython().run_line_magic zget_ipython().magic   zutf-8r   N)r   
ValueErrorpreprocessorsZPreprocessoropenreadZ
NO_CONVERTZPythonExporterZregister_preprocessorZfrom_notebook_nodereplacesysversion_infomajor
isinstanceZunicodeencodesuperr
   __init__)r   argskwargsr   r   r/   r3   r   fr'   ZexporterZpreprocessorr   _)	__class__r    r!   r=   C   s&    

!


zNotebookHandler.__init__)r)   r*   r+   r,   Z_logger_textZ_originr=   __classcell__r    r    )rB   r!   r
   9   s   r
   )r,   Z
__future__r   r   r   r   r-   r7   ZloggingZ	getLoggerr)   r   Zutil.dependenciesr   r#   r	   __all__r
   r    r    r    r!   <module>   s   
