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