Skip to content

Commit 64f02d8

Browse files
authored
Merge pull request #3323 from mdboom/custom-exporters
Add any extra installed nbconvert exporters to the "Download as" menu
2 parents 4ad6517 + b2ffad9 commit 64f02d8

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

notebook/notebook/handlers.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6+
from collections import namedtuple
67
import os
78
from tornado import web
89
HTTPError = web.HTTPError
@@ -11,6 +12,20 @@
1112
IPythonHandler, FilesRedirectHandler, path_regex,
1213
)
1314
from ..utils import url_escape
15+
from ..transutils import _
16+
17+
18+
def get_custom_frontend_exporters():
19+
from nbconvert.exporters.base import get_export_names, get_exporter
20+
21+
ExporterInfo = namedtuple('ExporterInfo', ['name', 'display'])
22+
23+
for name in sorted(get_export_names()):
24+
exporter = get_exporter(name)()
25+
ux_name = getattr(exporter, 'export_from_notebook', None)
26+
if ux_name is not None:
27+
display = _('{} ({})'.format(ux_name, exporter.file_extension))
28+
yield ExporterInfo(name, display)
1429

1530

1631
class NotebookHandler(IPythonHandler):
@@ -40,7 +55,8 @@ def get(self, path):
4055
notebook_name=name,
4156
kill_kernel=False,
4257
mathjax_url=self.mathjax_url,
43-
mathjax_config=self.mathjax_config
58+
mathjax_config=self.mathjax_config,
59+
get_custom_frontend_exporters=get_custom_frontend_exporters
4460
)
4561
)
4662

notebook/static/notebook/js/menubar.js

+2-26
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,8 @@ define([
189189
that._nbconvert('html', false);
190190
});
191191

192-
this.element.find('#download_html').click(function () {
193-
that._nbconvert('html', true);
194-
});
195-
196-
this.element.find('#download_slides').click(function () {
197-
that._nbconvert('slides', true);
198-
});
199-
200-
this.element.find('#download_markdown').click(function () {
201-
that._nbconvert('markdown', true);
202-
});
203-
204-
this.element.find('#download_rst').click(function () {
205-
that._nbconvert('rst', true);
206-
});
207-
208-
this.element.find('#download_pdf').click(function () {
209-
that._nbconvert('pdf', true);
210-
});
211-
212-
this.element.find('#download_latex').click(function () {
213-
that._nbconvert('latex', true);
214-
});
215-
216-
this.element.find('#download_script').click(function () {
217-
that._nbconvert('script', true);
192+
this.element.find('#download_menu li').click(function (ev) {
193+
that._nbconvert(ev.target.parentElement.getAttribute('id').substring(9), true);
218194
});
219195

220196
this.events.on('trust_changed.Notebook', function (event, trusted) {

notebook/templates/notebook.html

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
<li id="download_rst"><a href="#">{% trans %}reST (.rst){% endtrans %}</a></li>
115115
<li id="download_latex"><a href="#">{% trans %}LaTeX (.tex){% endtrans %}</a></li>
116116
<li id="download_pdf"><a href="#">{% trans %}PDF via LaTeX (.pdf){% endtrans %}</a></li>
117+
{% for exporter in get_custom_frontend_exporters() %}
118+
<li id="download_{{ exporter.name }}">
119+
<a href="#">{{ exporter.display }}</a>
120+
</li>
121+
{% endfor %}
117122
</ul>
118123
</li>
119124
<li class="dropdown-submenu hidden"><a href="#">{% trans %}Deploy as{% endtrans %}</a>

0 commit comments

Comments
 (0)