Notebook

In [26]:

    # Widget related imports
    from IPython.html import widgets
    from IPython.display import display, clear_output, Javascript
    from traitlets import Unicode

    # nbconvert related imports
    from IPython.nbconvert import get_export_names, export_by_name
    from IPython.nbconvert.writers import FilesWriter
    from IPython.nbformat import read, NO_CONVERT
    from IPython.nbconvert.utils.exceptions import ConversionException

Create a text Widget without displaying it. The widget will be used to store the notebook's name which is otherwise only available in the front-end.

In [17]:

    notebook_name = widgets.Text()

Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget.

In [18]:

    js = """IPython.notebook.kernel.widget_manager.get_model('%s').then(function(model) {
        model.set('value', IPython.notebook.notebook_name);
        model.save();
    });
    """ % notebook_name.model_id
    display(Javascript(data=js))

    <IPython.core.display.Javascript object>

In [19]:

    filename = notebook_name.value
    filename

Out[19]:

    'Export As (nbconvert).ipynb'

Create the widget that will allow the user to Export the current notebook.

In [20]:

    exporter_names = widgets.Dropdown(options=get_export_names(), value='html')
    export_button = widgets.Button(description="Export")
    download_link = widgets.HTML(visible=False)

Export the notebook when the export button is clicked.

In [29]:

    file_writer = FilesWriter()

    def export(name, nb):

        # Get a unique key for the notebook and set it in the resources object.
        notebook_name = name[:name.rfind('.')]
        resources = {}
        resources['unique_key'] = notebook_name
        resources['output_files_dir'] = '%s_files' % notebook_name

        # Try to export
        try:
            output, resources = export_by_name(exporter_names.value, nb)
        except ConversionException as e:
            download_link.value = "<br>Could not export notebook!"
        else:
            write_results = file_writer.write(output, resources, notebook_name=notebook_name)

            download_link.value = "<br>Results: <a href='files/{filename}'><i>\"{filename}\"</i></a>".format(filename=write_results)
            download_link.visible = True

    def handle_export(widget):
        with open(filename, 'r') as f:
            export(filename, read(f, NO_CONVERT))

    export_button.on_click(handle_export)

Display the controls.

In [30]:

    display(exporter_names, export_button, download_link)

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-21-6a4d11e868fe> in handle_export(widget)
         22 def handle_export(widget):
         23     with open(filename, 'r') as f:
    ---> 24         export(filename, read(f, 'json'))
         25 export_button.on_click(handle_export)

    /home/jon/ipython/IPython/nbformat/__init__.py in read(fp, as_version, **kwargs)
        131             return read(f, as_version, **kwargs)
        132
    --> 133     return reads(fp.read(), as_version, **kwargs)
        134
        135

    /home/jon/ipython/IPython/nbformat/__init__.py in reads(s, as_version, **kwargs)
         67     nb = reader.reads(s, **kwargs)
         68     if as_version is not NO_CONVERT:
    ---> 69         nb = convert(nb, as_version)
         70     try:
         71         validate(nb)

    /home/jon/ipython/IPython/nbformat/converter.py in convert(nb, to_version)
         52     else:
         53         raise ValueError("Cannot convert notebook to v%d because that " \
    ---> 54                         "version doesn't exist" % (to_version))

    TypeError: %d format: a number is required, not str

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-23-549800f6d03d> in handle_export(widget)
         22 def handle_export(widget):
         23     with open(filename, 'r') as f:
    ---> 24         export(filename, read(f, 'json', 4))
         25
         26 export_button.on_click(handle_export)

    TypeError: read() takes 2 positional arguments but 3 were given

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-24-549800f6d03d> in handle_export(widget)
         22 def handle_export(widget):
         23     with open(filename, 'r') as f:
    ---> 24         export(filename, read(f, 'json', 4))
         25
         26 export_button.on_click(handle_export)

    TypeError: read() takes 2 positional arguments but 3 were given

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-27-e5e414ef9f49> in handle_export(widget)
         22 def handle_export(widget):
         23     with open(filename, 'r') as f:
    ---> 24         export(filename, read(f))
         25
         26 export_button.on_click(handle_export)

    TypeError: read() missing 1 required positional argument: 'as_version'

In [ ]: