##// END OF EJS Templates
Update examples, fixing bugs found in review.
Update examples, fixing bugs found in review.

File last commit:

r17726:0ad4ffe0
r17946:fd20d2a3
Show More
Export As (nbconvert).ipynb
167 lines | 4.6 KiB | text/plain | TextLexer
Jonathan Frederic
Added Export As button...
r14354 {
"metadata": {
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "kernelspec": {
"codemirror_mode": {
"name": "python",
"version": 2
},
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"name": "",
"signature": "sha256:dc519f61f8a484b3a8f0b05ad1891fad701324c837325b12d31cce4f31647c2f"
Jonathan Frederic
Added Export As button...
r14354 },
"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",
jdavidheiser
Update Export As (nbconvert).ipynb
r16156 "from IPython.nbformat import current\n",
"from IPython.nbconvert.utils.exceptions import ConversionException"
Jonathan Frederic
Added Export As button...
r14354 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 },
{
"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
Updated widget examples for recent changes and,...
r17726 "notebook_name = widgets.Text()"
Jonathan Frederic
Added Export As button...
r14354 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 },
{
"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": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"filename = notebook_name.value\n",
"filename"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 },
{
"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
Updated widget examples for recent changes and,...
r17726 "exporter_names = widgets.Dropdown(values=get_export_names(), value='html')\n",
"export_button = widgets.Button(description=\"Export\")\n",
"download_link = widgets.HTML(visible=False)"
Jonathan Frederic
Added Export As button...
r14354 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 },
{
"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",
jdavidheiser
fix minor bugs in export as (nbconvert) example
r16155 "def handle_export(widget):\n",
Jonathan Frederic
Added Export As button...
r14354 " with open(filename, 'r') as f:\n",
" export(filename, current.read(f, 'json'))\n",
"export_button.on_click(handle_export)"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Fixed some more example files (see list below)......
r14742 },
{
"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": {},
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "outputs": []
Jonathan Frederic
Added Export As button...
r14354 }
],
"metadata": {}
}
]
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 }