diff --git a/IPython/html/nbconvert/handlers.py b/IPython/html/nbconvert/handlers.py index 9a65a02..dc4d2e4 100644 --- a/IPython/html/nbconvert/handlers.py +++ b/IPython/html/nbconvert/handlers.py @@ -6,7 +6,7 @@ from tornado import web from ..base.handlers import IPythonHandler, notebook_path_regex from IPython.nbformat.current import to_notebook_json -from IPython.nbconvert.exporters.export import exporter_map + from IPython.utils import tz from IPython.utils.py3compat import cast_bytes @@ -47,13 +47,30 @@ def respond_zip(handler, name, output, resources): handler.finish(buffer.getvalue()) return True +def get_exporter(format, **kwargs): + """get an exporter, raising appropriate errors""" + # if this fails, will raise 500 + try: + from IPython.nbconvert.exporters.export import exporter_map + except ImportError as e: + raise web.HTTPError(500, "Could not import nbconvert: %s" % e) + + try: + Exporter = exporter_map[format] + except KeyError: + # should this be 400? + raise web.HTTPError(404, u"No exporter for format: %s" % format) + + return Exporter(**kwargs) + class NbconvertFileHandler(IPythonHandler): SUPPORTED_METHODS = ('GET',) @web.authenticated def get(self, format, path='', name=None): - exporter = exporter_map[format](config=self.config) + + exporter = get_exporter(format, config=self.config) path = path.strip('/') os_path = self.notebook_manager.get_os_path(name, path) @@ -89,7 +106,7 @@ class NbconvertPostHandler(IPythonHandler): @web.authenticated def post(self, format): - exporter = exporter_map[format](config=self.config) + exporter = get_exporter(format, config=self.config) model = self.get_json_body() nbnode = to_notebook_json(model['content']) diff --git a/IPython/html/services/nbconvert/handlers.py b/IPython/html/services/nbconvert/handlers.py index e2ced71..fb79aa5 100644 --- a/IPython/html/services/nbconvert/handlers.py +++ b/IPython/html/services/nbconvert/handlers.py @@ -3,7 +3,10 @@ import json from tornado import web from ...base.handlers import IPythonHandler, json_errors -from IPython.nbconvert.exporters.export import exporter_map +try: + from IPython.nbconvert.exporters.export import exporter_map +except ImportError: + exporter_map = {} class NbconvertRootHandler(IPythonHandler): SUPPORTED_METHODS = ('GET',)