Show More
@@ -1,188 +1,187 b'' | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "name": "" |
|
3 | "name": "" | |
4 | }, |
|
4 | }, | |
5 | "nbformat": 3, |
|
5 | "nbformat": 3, | |
6 | "nbformat_minor": 0, |
|
6 | "nbformat_minor": 0, | |
7 | "worksheets": [ |
|
7 | "worksheets": [ | |
8 | { |
|
8 | { | |
9 | "cells": [ |
|
9 | "cells": [ | |
10 | { |
|
10 | { | |
11 | "cell_type": "code", |
|
11 | "cell_type": "code", | |
12 | "collapsed": false, |
|
12 | "collapsed": false, | |
13 | "input": [ |
|
13 | "input": [ | |
14 | "# Widget related imports\n", |
|
14 | "# Widget related imports\n", | |
15 | "from IPython.html import widgets\n", |
|
15 | "from IPython.html import widgets\n", | |
16 | "from IPython.display import display, clear_output, Javascript\n", |
|
16 | "from IPython.display import display, clear_output, Javascript\n", | |
17 | "from IPython.utils.traitlets import Unicode\n", |
|
17 | "from IPython.utils.traitlets import Unicode\n", | |
18 | "\n", |
|
18 | "\n", | |
19 | "# nbconvert related imports\n", |
|
19 | "# nbconvert related imports\n", | |
20 | "from IPython.nbconvert import get_export_names, export_by_name\n", |
|
20 | "from IPython.nbconvert import get_export_names, export_by_name\n", | |
21 | "from IPython.nbconvert.writers import FilesWriter\n", |
|
21 | "from IPython.nbconvert.writers import FilesWriter\n", | |
22 | "from IPython.nbformat import current" |
|
22 | "from IPython.nbformat import current\n", | |
23 |
"from IPython.nbconvert.utils.exceptions import ConversionException |
|
23 | "from IPython.nbconvert.utils.exceptions import ConversionException" | |
24 |
|
||||
25 | ], |
|
24 | ], | |
26 | "language": "python", |
|
25 | "language": "python", | |
27 | "metadata": {}, |
|
26 | "metadata": {}, | |
28 | "outputs": [], |
|
27 | "outputs": [], | |
29 | "prompt_number": 1 |
|
28 | "prompt_number": 1 | |
30 | }, |
|
29 | }, | |
31 | { |
|
30 | { | |
32 | "cell_type": "markdown", |
|
31 | "cell_type": "markdown", | |
33 | "metadata": {}, |
|
32 | "metadata": {}, | |
34 | "source": [ |
|
33 | "source": [ | |
35 | "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." |
|
34 | "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." | |
36 | ] |
|
35 | ] | |
37 | }, |
|
36 | }, | |
38 | { |
|
37 | { | |
39 | "cell_type": "code", |
|
38 | "cell_type": "code", | |
40 | "collapsed": false, |
|
39 | "collapsed": false, | |
41 | "input": [ |
|
40 | "input": [ | |
42 | "notebook_name = widgets.TextWidget()" |
|
41 | "notebook_name = widgets.TextWidget()" | |
43 | ], |
|
42 | ], | |
44 | "language": "python", |
|
43 | "language": "python", | |
45 | "metadata": {}, |
|
44 | "metadata": {}, | |
46 | "outputs": [], |
|
45 | "outputs": [], | |
47 | "prompt_number": 2 |
|
46 | "prompt_number": 2 | |
48 | }, |
|
47 | }, | |
49 | { |
|
48 | { | |
50 | "cell_type": "markdown", |
|
49 | "cell_type": "markdown", | |
51 | "metadata": {}, |
|
50 | "metadata": {}, | |
52 | "source": [ |
|
51 | "source": [ | |
53 | "Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget." |
|
52 | "Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget." | |
54 | ] |
|
53 | ] | |
55 | }, |
|
54 | }, | |
56 | { |
|
55 | { | |
57 | "cell_type": "code", |
|
56 | "cell_type": "code", | |
58 | "collapsed": false, |
|
57 | "collapsed": false, | |
59 | "input": [ |
|
58 | "input": [ | |
60 | "js = \"\"\"var model = IPython.notebook.kernel.widget_manager.get_model('{model_id}');\n", |
|
59 | "js = \"\"\"var model = IPython.notebook.kernel.widget_manager.get_model('{model_id}');\n", | |
61 | "model.set('value', IPython.notebook.notebook_name);\n", |
|
60 | "model.set('value', IPython.notebook.notebook_name);\n", | |
62 | "model.save();\"\"\".format(model_id=notebook_name.model_id)\n", |
|
61 | "model.save();\"\"\".format(model_id=notebook_name.model_id)\n", | |
63 | "display(Javascript(data=js))" |
|
62 | "display(Javascript(data=js))" | |
64 | ], |
|
63 | ], | |
65 | "language": "python", |
|
64 | "language": "python", | |
66 | "metadata": {}, |
|
65 | "metadata": {}, | |
67 | "outputs": [ |
|
66 | "outputs": [ | |
68 | { |
|
67 | { | |
69 | "javascript": [ |
|
68 | "javascript": [ | |
70 | "var model = IPython.notebook.kernel.widget_manager.get_model('8c6583524eb3422c99491730a3e1ce6c');\n", |
|
69 | "var model = IPython.notebook.kernel.widget_manager.get_model('8c6583524eb3422c99491730a3e1ce6c');\n", | |
71 | "model.set('value', IPython.notebook.notebook_name);\n", |
|
70 | "model.set('value', IPython.notebook.notebook_name);\n", | |
72 | "model.save();" |
|
71 | "model.save();" | |
73 | ], |
|
72 | ], | |
74 | "metadata": {}, |
|
73 | "metadata": {}, | |
75 | "output_type": "display_data", |
|
74 | "output_type": "display_data", | |
76 | "text": [ |
|
75 | "text": [ | |
77 | "<IPython.core.display.Javascript at 0x164ea50>" |
|
76 | "<IPython.core.display.Javascript at 0x164ea50>" | |
78 | ] |
|
77 | ] | |
79 | } |
|
78 | } | |
80 | ], |
|
79 | ], | |
81 | "prompt_number": 3 |
|
80 | "prompt_number": 3 | |
82 | }, |
|
81 | }, | |
83 | { |
|
82 | { | |
84 | "cell_type": "code", |
|
83 | "cell_type": "code", | |
85 | "collapsed": false, |
|
84 | "collapsed": false, | |
86 | "input": [ |
|
85 | "input": [ | |
87 | "filename = notebook_name.value\n", |
|
86 | "filename = notebook_name.value\n", | |
88 | "filename" |
|
87 | "filename" | |
89 | ], |
|
88 | ], | |
90 | "language": "python", |
|
89 | "language": "python", | |
91 | "metadata": {}, |
|
90 | "metadata": {}, | |
92 | "outputs": [ |
|
91 | "outputs": [ | |
93 | { |
|
92 | { | |
94 | "metadata": {}, |
|
93 | "metadata": {}, | |
95 | "output_type": "pyout", |
|
94 | "output_type": "pyout", | |
96 | "prompt_number": 4, |
|
95 | "prompt_number": 4, | |
97 | "text": [ |
|
96 | "text": [ | |
98 | "u'Export As (nbconvert).ipynb'" |
|
97 | "u'Export As (nbconvert).ipynb'" | |
99 | ] |
|
98 | ] | |
100 | } |
|
99 | } | |
101 | ], |
|
100 | ], | |
102 | "prompt_number": 4 |
|
101 | "prompt_number": 4 | |
103 | }, |
|
102 | }, | |
104 | { |
|
103 | { | |
105 | "cell_type": "markdown", |
|
104 | "cell_type": "markdown", | |
106 | "metadata": {}, |
|
105 | "metadata": {}, | |
107 | "source": [ |
|
106 | "source": [ | |
108 | "Create the widget that will allow the user to Export the current notebook." |
|
107 | "Create the widget that will allow the user to Export the current notebook." | |
109 | ] |
|
108 | ] | |
110 | }, |
|
109 | }, | |
111 | { |
|
110 | { | |
112 | "cell_type": "code", |
|
111 | "cell_type": "code", | |
113 | "collapsed": false, |
|
112 | "collapsed": false, | |
114 | "input": [ |
|
113 | "input": [ | |
115 | "exporter_names = widgets.DropdownWidget(values=get_export_names(), value='html')\n", |
|
114 | "exporter_names = widgets.DropdownWidget(values=get_export_names(), value='html')\n", | |
116 | "export_button = widgets.ButtonWidget(description=\"Export\")\n", |
|
115 | "export_button = widgets.ButtonWidget(description=\"Export\")\n", | |
117 | "download_link = widgets.HTMLWidget(visible=False)" |
|
116 | "download_link = widgets.HTMLWidget(visible=False)" | |
118 | ], |
|
117 | ], | |
119 | "language": "python", |
|
118 | "language": "python", | |
120 | "metadata": {}, |
|
119 | "metadata": {}, | |
121 | "outputs": [], |
|
120 | "outputs": [], | |
122 | "prompt_number": 5 |
|
121 | "prompt_number": 5 | |
123 | }, |
|
122 | }, | |
124 | { |
|
123 | { | |
125 | "cell_type": "markdown", |
|
124 | "cell_type": "markdown", | |
126 | "metadata": {}, |
|
125 | "metadata": {}, | |
127 | "source": [ |
|
126 | "source": [ | |
128 | "Export the notebook when the export button is clicked." |
|
127 | "Export the notebook when the export button is clicked." | |
129 | ] |
|
128 | ] | |
130 | }, |
|
129 | }, | |
131 | { |
|
130 | { | |
132 | "cell_type": "code", |
|
131 | "cell_type": "code", | |
133 | "collapsed": false, |
|
132 | "collapsed": false, | |
134 | "input": [ |
|
133 | "input": [ | |
135 | "file_writer = FilesWriter()\n", |
|
134 | "file_writer = FilesWriter()\n", | |
136 | "\n", |
|
135 | "\n", | |
137 | "def export(name, nb):\n", |
|
136 | "def export(name, nb):\n", | |
138 | " \n", |
|
137 | " \n", | |
139 | " # Get a unique key for the notebook and set it in the resources object.\n", |
|
138 | " # Get a unique key for the notebook and set it in the resources object.\n", | |
140 | " notebook_name = name[:name.rfind('.')]\n", |
|
139 | " notebook_name = name[:name.rfind('.')]\n", | |
141 | " resources = {}\n", |
|
140 | " resources = {}\n", | |
142 | " resources['unique_key'] = notebook_name\n", |
|
141 | " resources['unique_key'] = notebook_name\n", | |
143 | " resources['output_files_dir'] = '%s_files' % notebook_name\n", |
|
142 | " resources['output_files_dir'] = '%s_files' % notebook_name\n", | |
144 | "\n", |
|
143 | "\n", | |
145 | " # Try to export\n", |
|
144 | " # Try to export\n", | |
146 | " try:\n", |
|
145 | " try:\n", | |
147 | " output, resources = export_by_name(exporter_names.value, nb)\n", |
|
146 | " output, resources = export_by_name(exporter_names.value, nb)\n", | |
148 | " except ConversionException as e:\n", |
|
147 | " except ConversionException as e:\n", | |
149 | " download_link.value = \"<br>Could not export notebook!\"\n", |
|
148 | " download_link.value = \"<br>Could not export notebook!\"\n", | |
150 | " else:\n", |
|
149 | " else:\n", | |
151 | " write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", |
|
150 | " write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", | |
152 | " \n", |
|
151 | " \n", | |
153 | " download_link.value = \"<br>Results: <a href='files/{filename}'><i>\\\"{filename}\\\"</i></a>\".format(filename=write_results)\n", |
|
152 | " download_link.value = \"<br>Results: <a href='files/{filename}'><i>\\\"{filename}\\\"</i></a>\".format(filename=write_results)\n", | |
154 | " download_link.visible = True\n", |
|
153 | " download_link.visible = True\n", | |
155 | " \n", |
|
154 | " \n", | |
156 | "def handle_export(widget):\n", |
|
155 | "def handle_export(widget):\n", | |
157 | " with open(filename, 'r') as f:\n", |
|
156 | " with open(filename, 'r') as f:\n", | |
158 | " export(filename, current.read(f, 'json'))\n", |
|
157 | " export(filename, current.read(f, 'json'))\n", | |
159 | "export_button.on_click(handle_export)" |
|
158 | "export_button.on_click(handle_export)" | |
160 | ], |
|
159 | ], | |
161 | "language": "python", |
|
160 | "language": "python", | |
162 | "metadata": {}, |
|
161 | "metadata": {}, | |
163 | "outputs": [], |
|
162 | "outputs": [], | |
164 | "prompt_number": 6 |
|
163 | "prompt_number": 6 | |
165 | }, |
|
164 | }, | |
166 | { |
|
165 | { | |
167 | "cell_type": "markdown", |
|
166 | "cell_type": "markdown", | |
168 | "metadata": {}, |
|
167 | "metadata": {}, | |
169 | "source": [ |
|
168 | "source": [ | |
170 | "Display the controls." |
|
169 | "Display the controls." | |
171 | ] |
|
170 | ] | |
172 | }, |
|
171 | }, | |
173 | { |
|
172 | { | |
174 | "cell_type": "code", |
|
173 | "cell_type": "code", | |
175 | "collapsed": false, |
|
174 | "collapsed": false, | |
176 | "input": [ |
|
175 | "input": [ | |
177 | "display(exporter_names, export_button, download_link)" |
|
176 | "display(exporter_names, export_button, download_link)" | |
178 | ], |
|
177 | ], | |
179 | "language": "python", |
|
178 | "language": "python", | |
180 | "metadata": {}, |
|
179 | "metadata": {}, | |
181 | "outputs": [], |
|
180 | "outputs": [], | |
182 | "prompt_number": 7 |
|
181 | "prompt_number": 7 | |
183 | } |
|
182 | } | |
184 | ], |
|
183 | ], | |
185 | "metadata": {} |
|
184 | "metadata": {} | |
186 | } |
|
185 | } | |
187 | ] |
|
186 | ] | |
188 | } |
|
187 | } |
General Comments 0
You need to be logged in to leave comments.
Login now