Export As (nbconvert).ipynb
186 lines
| 4.8 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14354 | { | ||
Min RK
|
r18669 | "cells": [ | ||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"# Widget related imports\n", | ||||
"from IPython.html import widgets\n", | ||||
"from IPython.display import display, clear_output, Javascript\n", | ||||
"from IPython.utils.traitlets import Unicode\n", | ||||
"\n", | ||||
"# nbconvert related imports\n", | ||||
"from IPython.nbconvert import get_export_names, export_by_name\n", | ||||
"from IPython.nbconvert.writers import FilesWriter\n", | ||||
Jonathan Frederic
|
r20541 | "from IPython.nbformat import read, NO_CONVERT\n", | ||
Min RK
|
r18669 | "from IPython.nbconvert.utils.exceptions import ConversionException" | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
Jonathan Frederic
|
r20541 | "This notebook shows a really roundabout way to get the name of the notebook file using widgets. The true purpose of this demo is to demonstrate how Javascript and Python widget models are related by `id`." | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
Min RK
|
r18669 | "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." | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"notebook_name = widgets.Text()" | ||||
] | ||||
}, | ||||
{ | ||||
"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", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
Jonathan Frederic
|
r20541 | "js = \"\"\"IPython.notebook.kernel.widget_manager.get_model('%s').then(function(model) {\n", | ||
" model.set('value', IPython.notebook.notebook_name);\n", | ||||
" model.save();\n", | ||||
"});\n", | ||||
"\"\"\" % notebook_name.model_id\n", | ||||
Min RK
|
r18669 | "display(Javascript(data=js))" | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"filename = notebook_name.value\n", | ||||
"filename" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"Create the widget that will allow the user to Export the current notebook." | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
Nicholas Bollweg
|
r20287 | "exporter_names = widgets.Dropdown(options=get_export_names(), value='html')\n", | ||
Min RK
|
r18669 | "export_button = widgets.Button(description=\"Export\")\n", | ||
"download_link = widgets.HTML(visible=False)" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"Export the notebook when the export button is clicked." | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"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 = \"<br>Could not export notebook!\"\n", | ||||
" else:\n", | ||||
" write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", | ||||
" \n", | ||||
" download_link.value = \"<br>Results: <a href='files/{filename}'><i>\\\"{filename}\\\"</i></a>\".format(filename=write_results)\n", | ||||
" download_link.visible = True\n", | ||||
" \n", | ||||
"def handle_export(widget):\n", | ||||
" with open(filename, 'r') as f:\n", | ||||
Jonathan Frederic
|
r20541 | " export(filename, read(f, NO_CONVERT))\n", | ||
" \n", | ||||
Min RK
|
r18669 | "export_button.on_click(handle_export)" | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"Display the controls." | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"display(exporter_names, export_button, download_link)" | ||||
] | ||||
} | ||||
], | ||||
Jonathan Frederic
|
r14354 | "metadata": { | ||
Jonathan Frederic
|
r17726 | "kernelspec": { | ||
Min RK
|
r20278 | "display_name": "Python 3", | ||
Jonathan Frederic
|
r17726 | "language": "python", | ||
Min RK
|
r20278 | "name": "python3" | ||
Jonathan Frederic
|
r17726 | }, | ||
Min RK
|
r20278 | "language_info": { | ||
"codemirror_mode": { | ||||
"name": "ipython", | ||||
"version": 3 | ||||
}, | ||||
"file_extension": ".py", | ||||
"mimetype": "text/x-python", | ||||
"name": "python", | ||||
"nbconvert_exporter": "python", | ||||
"pygments_lexer": "ipython3", | ||||
Jonathan Frederic
|
r20541 | "version": "3.4.0" | ||
Min RK
|
r20278 | } | ||
Jonathan Frederic
|
r14354 | }, | ||
Min RK
|
r18669 | "nbformat": 4, | ||
"nbformat_minor": 0 | ||||
Min RK
|
r20278 | } | ||