##// END OF EJS Templates
Lots of doc work.
Lots of doc work.

File last commit:

r16120:24b93a1d
r16120:24b93a1d
Show More
Export As (nbconvert).ipynb
185 lines | 5.0 KiB | text/plain | TextLexer
Jonathan Frederic
Added Export As button...
r14354 {
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "# Widget related imports\n",
Jonathan Frederic
Added Export As button...
r14354 "from IPython.html import widgets\n",
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "from IPython.display import display, clear_output, Javascript\n",
"from IPython.utils.traitlets import Unicode\n",
"\n",
"# nbconvert related imports\n",
Jonathan Frederic
Added Export As button...
r14354 "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": [
Jonathan Frederic
Renamed widgets......
r14834 "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."
Jonathan Frederic
Added Export As button...
r14354 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Renamed widgets......
r14834 "notebook_name = widgets.TextWidget()"
Jonathan Frederic
Added Export As button...
r14354 ],
"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": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "js = \"\"\"var model = IPython.notebook.kernel.widget_manager.get_model('{model_id}');\n",
Jonathan Frederic
Added Export As button...
r14354 "model.set('value', IPython.notebook.notebook_name);\n",
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "model.save();\"\"\".format(model_id=notebook_name.model_id)\n",
Jonathan Frederic
Added Export As button...
r14354 "display(Javascript(data=js))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"javascript": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "var model = IPython.notebook.kernel.widget_manager.get_model('8c6583524eb3422c99491730a3e1ce6c');\n",
Jonathan Frederic
Added Export As button...
r14354 "model.set('value', IPython.notebook.notebook_name);\n",
"model.save();"
],
"metadata": {},
"output_type": "display_data",
"text": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "<IPython.core.display.Javascript at 0x164ea50>"
Jonathan Frederic
Added Export As button...
r14354 ]
}
],
"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": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "exporter_names = widgets.DropdownWidget(values=get_export_names(), value='html')\n",
"export_button = widgets.ButtonWidget(description=\"Export\")\n",
"download_link = widgets.HTMLWidget(visible=False)"
Jonathan Frederic
Added Export As button...
r14354 ],
"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 = \"<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",
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 " download_link.visible = True\n",
" \n",
Jonathan Frederic
Added Export As button...
r14354 "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": [],
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display the controls."
]
Jonathan Frederic
Added Export As button...
r14354 },
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "display(exporter_names, export_button, download_link)"
Jonathan Frederic
Added Export As button...
r14354 ],
"language": "python",
"metadata": {},
"outputs": [],
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 "prompt_number": 7
Jonathan Frederic
Added Export As button...
r14354 }
],
"metadata": {}
}
]
}