Skip to content

Commit

Permalink
Merge pull request #3323 from mdboom/custom-exporters
Browse files Browse the repository at this point in the history
Add any extra installed nbconvert exporters to the "Download as" menu
  • Loading branch information
takluyver authored Feb 12, 2018
2 parents 4ad6517 + b2ffad9 commit 64f02d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
18 changes: 17 additions & 1 deletion notebook/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from collections import namedtuple
import os
from tornado import web
HTTPError = web.HTTPError
Expand All @@ -11,6 +12,20 @@
IPythonHandler, FilesRedirectHandler, path_regex,
)
from ..utils import url_escape
from ..transutils import _


def get_custom_frontend_exporters():
from nbconvert.exporters.base import get_export_names, get_exporter

ExporterInfo = namedtuple('ExporterInfo', ['name', 'display'])

for name in sorted(get_export_names()):
exporter = get_exporter(name)()
ux_name = getattr(exporter, 'export_from_notebook', None)
if ux_name is not None:
display = _('{} ({})'.format(ux_name, exporter.file_extension))
yield ExporterInfo(name, display)


class NotebookHandler(IPythonHandler):
Expand Down Expand Up @@ -40,7 +55,8 @@ def get(self, path):
notebook_name=name,
kill_kernel=False,
mathjax_url=self.mathjax_url,
mathjax_config=self.mathjax_config
mathjax_config=self.mathjax_config,
get_custom_frontend_exporters=get_custom_frontend_exporters
)
)

Expand Down
28 changes: 2 additions & 26 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,8 @@ define([
that._nbconvert('html', false);
});

this.element.find('#download_html').click(function () {
that._nbconvert('html', true);
});

this.element.find('#download_slides').click(function () {
that._nbconvert('slides', true);
});

this.element.find('#download_markdown').click(function () {
that._nbconvert('markdown', true);
});

this.element.find('#download_rst').click(function () {
that._nbconvert('rst', true);
});

this.element.find('#download_pdf').click(function () {
that._nbconvert('pdf', true);
});

this.element.find('#download_latex').click(function () {
that._nbconvert('latex', true);
});

this.element.find('#download_script').click(function () {
that._nbconvert('script', true);
this.element.find('#download_menu li').click(function (ev) {
that._nbconvert(ev.target.parentElement.getAttribute('id').substring(9), true);
});

this.events.on('trust_changed.Notebook', function (event, trusted) {
Expand Down
5 changes: 5 additions & 0 deletions notebook/templates/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<li id="download_rst"><a href="#">{% trans %}reST (.rst){% endtrans %}</a></li>
<li id="download_latex"><a href="#">{% trans %}LaTeX (.tex){% endtrans %}</a></li>
<li id="download_pdf"><a href="#">{% trans %}PDF via LaTeX (.pdf){% endtrans %}</a></li>
{% for exporter in get_custom_frontend_exporters() %}
<li id="download_{{ exporter.name }}">
<a href="#">{{ exporter.display }}</a>
</li>
{% endfor %}
</ul>
</li>
<li class="dropdown-submenu hidden"><a href="#">{% trans %}Deploy as{% endtrans %}</a>
Expand Down

0 comments on commit 64f02d8

Please sign in to comment.