from __future__ import absolute_import, print_function

from bokeh.util.dependencies import import_required

from .code import CodeHandler

class NotebookHandler(CodeHandler):
    """ Run a notebook which modifies a Document

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

    """
    
    _logger_text = "%s: call to %s() ignored when running notebooks with the 'bokeh' command."

    _origin = "Notebook"

    def __init__(self, *args, **kwargs):
        
        nbformat = import_required('nbformat', 'The Bokeh notebook application handler requires Jupyter Notebook to be installed.')
        nbconvert = import_required('nbconvert', 'The Bokeh notebook application handler requires Jupyter Notebook to be installed.')
        
        if 'filename' not in kwargs:
            raise ValueError('Must pass a filename to NotebookHandler')

        with open(kwargs['filename']) as f:
            nb = nbformat.read(f, nbformat.NO_CONVERT)
            exporter = nbconvert.PythonExporter()
            source, meta = exporter.from_notebook_node(nb)
            kwargs['source'] = source

        super(NotebookHandler, self).__init__(*args, **kwargs)
