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