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