From 198d74aedacd98f0240f5920c7949e3f9a325bd7 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Fri, 9 Feb 2018 13:17:12 -0500 Subject: [PATCH 1/2] Add any extra installed nbconvert exporters to the "Download as" menu --- notebook/notebook/handlers.py | 18 ++++++++++++++++- notebook/static/notebook/js/menubar.js | 28 ++------------------------ notebook/templates/notebook.html | 6 ++++++ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/notebook/notebook/handlers.py b/notebook/notebook/handlers.py index c885a55c94..82110a81ae 100644 --- a/notebook/notebook/handlers.py +++ b/notebook/notebook/handlers.py @@ -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 @@ -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): @@ -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 ) ) diff --git a/notebook/static/notebook/js/menubar.js b/notebook/static/notebook/js/menubar.js index 17a6945a29..450ce310cd 100644 --- a/notebook/static/notebook/js/menubar.js +++ b/notebook/static/notebook/js/menubar.js @@ -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) { diff --git a/notebook/templates/notebook.html b/notebook/templates/notebook.html index 589efdd470..ff710680af 100644 --- a/notebook/templates/notebook.html +++ b/notebook/templates/notebook.html @@ -114,6 +114,12 @@
  • {% trans %}reST (.rst){% endtrans %}
  • {% trans %}LaTeX (.tex){% endtrans %}
  • {% trans %}PDF via LaTeX (.pdf){% endtrans %}
  • + {% for exporter in get_custom_frontend_exporters() %} +
  • + {{ exporter.display }} +
  • + + {% endfor %}
  • {{ exporter.display }}
  • - {% endfor %}