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