{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from IPython.html import widgets\n", "from IPython.display import display, Javascript, clear_output\n", "from IPython.nbconvert import get_export_names, export_by_name\n", "from IPython.nbconvert.writers import FilesWriter\n", "from IPython.nbformat import current" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create and *display* a StringWidget without a view. The StringWidget will be used to store the notebook name which is otherwise only available in the front-end." ] }, { "cell_type": "code", "collapsed": false, "input": [ "notebook_name = widgets.StringWidget(default_view_name='')\n", "display(notebook_name)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget." ] }, { "cell_type": "code", "collapsed": false, "input": [ "js = \"\"\"var model = IPython.notebook.kernel.comm_manager.comms['{comm_id}'].model;\n", "model.set('value', IPython.notebook.notebook_name);\n", "model.save();\"\"\".format(comm_id=notebook_name._comm.comm_id)\n", "display(Javascript(data=js))" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "var model = IPython.notebook.kernel.comm_manager.comms['79dbd18695b3432b94e2265533474474'].model;\n", "model.set('value', IPython.notebook.notebook_name);\n", "model.save();" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "filename = notebook_name.value\n", "filename" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "u'Export As (nbconvert).ipynb'" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the widget that will allow the user to Export the current notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "container = widgets.ContainerWidget()\n", "container.vbox()\n", "container.align_center()\n", "\n", "options = widgets.ContainerWidget(parent=container)\n", "options.hbox()\n", "options.align_center()\n", "exporter_names = widgets.SelectionWidget(parent=options, values=get_export_names(), value='html')\n", "export_button = widgets.ButtonWidget(parent=options, description=\"Export\")\n", "\n", "download_link = widgets.StringWidget(parent=container, default_view_name=\"LabelView\", visible=False)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Export the notebook when the export button is clicked." ] }, { "cell_type": "code", "collapsed": false, "input": [ "file_writer = FilesWriter()\n", "\n", "def export(name, nb):\n", " \n", " # Get a unique key for the notebook and set it in the resources object.\n", " notebook_name = name[:name.rfind('.')]\n", " resources = {}\n", " resources['unique_key'] = notebook_name\n", " resources['output_files_dir'] = '%s_files' % notebook_name\n", "\n", " # Try to export\n", " try:\n", " output, resources = export_by_name(exporter_names.value, nb)\n", " except ConversionException as e:\n", " download_link.value = \"
Could not export notebook!\"\n", " else:\n", " write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", " \n", " download_link.value = \"
Results: \\\"{filename}\\\"\".format(filename=write_results)\n", " download_link.visible = True" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "def handle_export():\n", " with open(filename, 'r') as f:\n", " export(filename, current.read(f, 'json'))\n", "export_button.on_click(handle_export)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "download_link.visible = False\n", "display(container)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 } ], "metadata": {} } ] }