ó
mÜJ]c           @` s§   d  Z  d d l m Z m Z m Z m Z d d l Z e j e ƒ Z	 d d l
 Z
 d d l Z d d l Z d d l m Z d d l m Z d Z d	 e f d
 „  ƒ  YZ d S(   uÿ   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``.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   import_requiredi   (   t   CodeHandleru   NotebookHandlert   NotebookHandlerc           B` s#   e  Z d  Z d Z d Z d „  Z RS(   uX    A Handler that uses code in a Jupyter notebook for modifying Bokeh
    Documents.

    uI   %s: call to %s() ignored when running notebooks with the 'bokeh' command.u   Notebookc         O` s[  t  d d ƒ } t  d d ƒ } d | k r9 t d ƒ ‚ n  d | j j f d „  ƒ  Y} | ƒ  g } | d } t j | d d	 ƒ¼ } | j | | j ƒ }	 | j ƒ  }
 x | D] } |
 j	 | ƒ q« W|
 j
 |	 ƒ \ } } | j d
 d ƒ } | j d d ƒ } t j j d k r.t | t ƒ r.| j d	 ƒ } n  | | d <Wd QXt t |  ƒ j | | Ž  d S(   ug   

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

        u   nbformatuQ   The Bokeh notebook application handler requires Jupyter Notebook to be installed.u	   nbconvertu   filenameu'   Must pass a filename to NotebookHandlert   StripMagicsProcessorc           B` s8   e  Z d  Z e j d ƒ Z d „  Z d „  Z d „  Z RS(   u–   
            Preprocessor to convert notebooks to Python source while stripping
            out all magics (i.e IPython specific syntax).
            u   ^\s*(?P<magic>%%\w\w+)($|(\s+))c         S` s   g  } xy | j  ƒ  D]k } |  j j | ƒ } | d k rG | j | ƒ q d } | j d |  j d | j d ƒ ƒ } t j	 | ƒ q Wd j
 | ƒ S(   ub   
                Given the source of a cell, filter out all cell and line magics.
                u7   Stripping out IPython magic {magic} in code cell {cell}t   cellt   magicu   magicu   
N(   t
   splitlinest   _magic_patternt   matcht   Nonet   appendt   formatt   _cell_countert   groupt   logt   warningt   join(   t   selft   sourcet   filteredt   lineR   t   msgt   message(    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyt   strip_magicsZ   s    $c         S` sC   | d d k r9 |  j  d 7_  |  j | d ƒ | d <n  | | f S(   Nu	   cell_typeu   codei   u   source(   R   R   (   R   R   t	   resourcest   index(    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyt   preprocess_celli   s    c         S` s   d |  _  |  j | | ƒ S(   Ni    (   R   t
   preprocess(   R   t   nbR   (    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyt   __call__o   s    	(	   t   __name__t
   __module__t   __doc__t   ret   compileR   R   R   R!   (    (    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyR   R   s
   		t   encodingu   utf-8u   get_ipython().run_line_magicu    u   get_ipython().magici   u   sourceN(   R   t
   ValueErrort   preprocessorst   Preprocessort   iot   opent   readt
   NO_CONVERTt   PythonExportert   register_preprocessort   from_notebook_nodet   replacet   syst   version_infot   majort
   isinstancet   unicodet   encodet   superR   t   __init__(   R   t   argst   kwargst   nbformatt	   nbconvertR   R)   t   filenamet   fR    t   exportert   preprocessorR   t   _(    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyR:   D   s&    !
!(   R"   R#   R$   t   _logger_textt   _originR:   (    (    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyR   :   s   (   u   NotebookHandler(   R$   t
   __future__R    R   R   R   t   loggingt	   getLoggerR"   R   R+   R%   R3   t   util.dependenciesR   t   codeR   t   __all__R   (    (    (    sB   lib/python2.7/site-packages/bokeh/application/handlers/notebook.pyt   <module>   s   "