Beispiel #1
0
    def get(self):
        self.check_xsrf_cookie()
        try:
            from nbconvert.exporters import base
        except ImportError as e:
            raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
        res = {}
        exporters = base.get_export_names()
        for exporter_name in exporters:
            try:
                exporter_class = base.get_exporter(exporter_name)
            except ValueError:
                # I think the only way this will happen is if the entrypoint
                # is uninstalled while this method is running
                continue
            # XXX: According to the docs, it looks like this should be set to None
            # if the exporter shouldn't be exposed to the front-end and a friendly
            # name if it should. However, none of the built-in exports have it defined.
            # if not exporter_class.export_from_notebook:
            #    continue
            res[exporter_name] = {
                "output_mimetype": exporter_class.output_mimetype,
            }

        self.finish(json.dumps(res))
Beispiel #2
0
def nbconv(in_file: str, exporter: str = None) -> Tuple[str, str]:
    """Convert a notebook into various formats using ``nbformat`` exporters.

    :param in_file: The name of the input notebook file.
    :param exporter: The exporter that determines the output file type.
    :return: A 2-tuple of the output file's 1) name and 2) contents.
    :note: The exporter type must be 'asciidoc', 'pdf', 'html', 'latex',
           'markdown', 'python', 'rst', 'script', or 'slides'.
           All formats except 'HTML' require pandoc.
           Exporting to pdf requires latex.
    """
    in_file_path = Path(in_file)
    if not exporter:
        ext_exp_dict = {
            ".asciidoc": "asciidoc",
            ".adoc": "asciidoc",
            ".asc": "asciidoc",
            ".pdf": "pdf",
            ".html": "html",
            ".tex": "latex",
            ".md": "markdown",
            ".py": "python",
            ".R": "script",
            ".rst": "rst",
        }
        if in_file_path.suffix in ext_exp_dict:
            exporter = ext_exp_dict[in_file_path.suffix]
        else:
            print("Unable to infer exporter type!")
    contents, resources = get_exporter(exporter)().from_filename(in_file)
    out_name = in_file_path.stem + resources.get("output_extension", ".txt")
    return out_name, contents
Beispiel #3
0
    def get(self, format, path):

        exporter = get_exporter(format, config=self.config, log=self.log)

        path = path.strip('/')
        # If the notebook relates to a real file (default contents manager),
        # give its path to nbconvert.
        if hasattr(self.contents_manager, '_get_os_path'):
            os_path = self.contents_manager._get_os_path(path)
            ext_resources_dir, basename = os.path.split(os_path)
        else:
            ext_resources_dir = None

        model = self.contents_manager.get(path=path)
        name = model['name']
        if model['type'] != 'notebook':
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        nb = model['content']

        self.set_header('Last-Modified', model['last_modified'])

        # create resources dictionary
        mod_date = model['last_modified'].strftime(text.date_format)
        nb_title = os.path.splitext(name)[0]

        resource_dict = {
            "metadata": {
                "name": nb_title,
                "modified_date": mod_date
            },
            "config_dir": self.application.settings['config_dir']
        }

        if ext_resources_dir:
            resource_dict['metadata']['path'] = ext_resources_dir

        try:
            output, resources = exporter.from_notebook_node(
                nb, resources=resource_dict)
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # Force download if requested
        if self.get_argument('download', 'false').lower() == 'true':
            filename = os.path.splitext(
                name)[0] + resources['output_extension']
            self.set_attachment_header(filename)

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.finish(output)
    def from_notebook_node(self, nb, resources=None, **kw):
        # track conversion time.
        start_time = time()

        # Preprocess the notebook
        nb, resources = self._preprocess(nb, resources)

        # nbconvert options embedded in the metadata as a dict or a list.
        convert = nb.metadata.pop('nbconvert', {})
        if isinstance(convert, dict):
            convert = convert.items()

        for exporter, config in convert:
            app = NbConvertApp(config=Config(config))
            app.initialize([])
            app.exporter = get_exporter(exporter)(config=app.config)
            app.convert_single_notebook(self.filename)

        # How long did the conversions take?
        if convert:
            app.log.info(
                "Conversions completed in {} seconds.".format(time() -
                                                              start_time))

        return
Beispiel #5
0
def make_html_bundle(model, nb_title, config_dir, ext_resources_dir, config,
                     jupyter_log):
    # create resources dictionary
    resource_dict = {
        "metadata": {
            "name": nb_title,
            "modified_date": model['last_modified'].strftime(text.date_format)
        },
        "config_dir": config_dir
    }

    if ext_resources_dir:
        resource_dict['metadata']['path'] = ext_resources_dir

    exporter = get_exporter(config=config, log=jupyter_log)
    notebook = model['content']
    output, resources = exporter.from_notebook_node(notebook,
                                                    resources=resource_dict)

    filename = splitext(model['name'])[0] + resources['output_extension']
    log.info('filename = %s' % filename)

    bundle_file = tempfile.TemporaryFile(prefix='rsc_bundle')

    with tarfile.open(mode='w:gz', fileobj=bundle_file) as bundle:
        bundle_add_buffer(bundle, filename, output)

        # manifest
        manifest = make_html_manifest(filename)
        log.debug('manifest: %r', manifest)
        bundle_add_buffer(bundle, 'manifest.json', json.dumps(manifest))

    # rewind file pointer
    bundle_file.seek(0)
    return bundle_file
Beispiel #6
0
    def post(self, format):
        exporter = get_exporter(format, config=self.config)

        model = self.get_json_body()
        name = model.get('name', 'notebook.ipynb')
        nbnode = from_dict(model['content'])

        try:
            output, resources = exporter.from_notebook_node(
                nbnode,
                resources={
                    "metadata": {
                        "name": name[:name.rfind('.')],
                    },
                    "config_dir": self.application.settings['config_dir'],
                })
        except Exception as e:
            raise web.HTTPError(500, "nbconvert failed: %s" % e) from e

        if respond_zip(self, name, output, resources):
            return

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.finish(output)
Beispiel #7
0
    async def post(self, format):
        exporter = get_exporter(format, config=self.config)

        model = self.get_json_body()
        name = model.get("name", "notebook.ipynb")
        nbnode = from_dict(model["content"])

        try:
            output, resources = await run_sync(
                lambda: exporter.from_notebook_node(
                    nbnode,
                    resources={
                        "metadata": {"name": name[: name.rfind(".")]},
                        "config_dir": self.application.settings["config_dir"],
                    },
                )
            )
        except Exception as e:
            raise web.HTTPError(500, "nbconvert failed: %s" % e) from e

        if respond_zip(self, name, output, resources):
            return

        # MIME type
        if exporter.output_mimetype:
            self.set_header("Content-Type", "%s; charset=utf-8" % exporter.output_mimetype)

        self.finish(output)
Beispiel #8
0
    async def get(self, format, path):
        self.check_xsrf_cookie()
        exporter = get_exporter(format, config=self.config, log=self.log)

        path = path.strip("/")
        # If the notebook relates to a real file (default contents manager),
        # give its path to nbconvert.
        if hasattr(self.contents_manager, "_get_os_path"):
            os_path = self.contents_manager._get_os_path(path)
            ext_resources_dir, basename = os.path.split(os_path)
        else:
            ext_resources_dir = None

        model = await ensure_async(self.contents_manager.get(path=path))
        name = model["name"]
        if model["type"] != "notebook":
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        nb = model["content"]

        self.set_header("Last-Modified", model["last_modified"])

        # create resources dictionary
        mod_date = model["last_modified"].strftime(date_format)
        nb_title = os.path.splitext(name)[0]

        resource_dict = {
            "metadata": {"name": nb_title, "modified_date": mod_date},
            "config_dir": self.application.settings["config_dir"],
        }

        if ext_resources_dir:
            resource_dict["metadata"]["path"] = ext_resources_dir

        # Exporting can take a while, delegate to a thread so we don't block the event loop
        try:
            output, resources = await run_sync(
                lambda: exporter.from_notebook_node(nb, resources=resource_dict)
            )
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e) from e

        if respond_zip(self, name, output, resources):
            return

        # Force download if requested
        if self.get_argument("download", "false").lower() == "true":
            filename = os.path.splitext(name)[0] + resources["output_extension"]
            self.set_attachment_header(filename)

        # MIME type
        if exporter.output_mimetype:
            self.set_header("Content-Type", "%s; charset=utf-8" % exporter.output_mimetype)

        self.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
        self.finish(output)
Beispiel #9
0
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)
Beispiel #10
0
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)
Beispiel #11
0
    def get(self, format, path):
        
        exporter = get_exporter(format, config=self.config, log=self.log)
        
        path = path.strip('/')
        # If the notebook relates to a real file (default contents manager),
        # give its path to nbconvert.
        if hasattr(self.contents_manager, '_get_os_path'):
            os_path = self.contents_manager._get_os_path(path)
        else:
            os_path = ''

        model = self.contents_manager.get(path=path)
        name = model['name']
        if model['type'] != 'notebook':
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        self.set_header('Last-Modified', model['last_modified'])

        try:
            output, resources = exporter.from_notebook_node(
                model['content'],
                resources={
                    "metadata": {
                        "name": name[:name.rfind('.')],
                        "modified_date": (model['last_modified']
                            .strftime(text.date_format)),
                        "path" : os_path
                    },
                    "config_dir": self.application.settings['config_dir'],
                }
            )
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # Force download if requested
        if self.get_argument('download', 'false').lower() == 'true':
            filename = os.path.splitext(name)[0] + resources['output_extension']
            self.set_attachment_header(filename)

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.finish(output)
Beispiel #12
0
def get_frontend_exporters():
    from nbconvert.exporters.base import get_export_names, get_exporter

    # name=exporter_name, display=export_from_notebook+extension
    ExporterInfo = namedtuple('ExporterInfo', ['name', 'display'])

    default_exporters = [
        ExporterInfo(name='html', display='HTML (.html)'),
        ExporterInfo(name='latex', display='LaTeX (.tex)'),
        ExporterInfo(name='markdown', display='Markdown (.md)'),
        ExporterInfo(name='notebook', display='Notebook (.ipynb)'),
        ExporterInfo(name='pdf', display='PDF via LaTeX (.pdf)'),
        ExporterInfo(name='rst', display='reST (.rst)'),
        ExporterInfo(name='script', display='Script (.txt)'),
        ExporterInfo(name='slides', display='Reveal.js slides (.slides.html)')
    ]

    frontend_exporters = []
    for name in get_export_names():
        exporter_class = get_exporter(name)
        exporter_instance = exporter_class()
        ux_name = getattr(exporter_instance, 'export_from_notebook', None)
        super_uxname = getattr(super(exporter_class, exporter_instance),
                               'export_from_notebook', None)

        # Ensure export_from_notebook is explicitly defined & not inherited
        if ux_name is not None and ux_name != super_uxname:
            display = _('{} ({})'.format(ux_name,
                                         exporter_instance.file_extension))
            frontend_exporters.append(ExporterInfo(name, display))

    # Ensure default_exporters are in frontend_exporters if not already
    # This protects against nbconvert versions lower than 5.5
    names = set(exporter.name.lower() for exporter in frontend_exporters)
    for exporter in default_exporters:
        if exporter.name not in names:
            frontend_exporters.append(exporter)

    # Protect against nbconvert 5.5.0
    python_exporter = ExporterInfo(name='python', display='python (.py)')
    if python_exporter in frontend_exporters:
        frontend_exporters.remove(python_exporter)

    # Protect against nbconvert 5.4.x
    template_exporter = ExporterInfo(name='custom', display='custom (.txt)')
    if template_exporter in frontend_exporters:
        frontend_exporters.remove(template_exporter)
    return sorted(frontend_exporters)
Beispiel #13
0
def get_exporter(**kwargs):
    """get an exporter, raising appropriate errors"""
    # if this fails, will raise 500
    try:
        from nbconvert.exporters.base import get_exporter
    except ImportError as e:
        raise Exception("Could not import nbconvert: %s" % e)

    try:
        Exporter = get_exporter('html')
    except KeyError:
        raise Exception("No exporter for format: html")

    try:
        return Exporter(**kwargs)
    except Exception as e:
        raise Exception("Could not construct Exporter: %s" % e)
    def get(self, format, path):

        exporter = get_exporter(format, config=self.config, log=self.log)

        path = path.strip('/')
        model = self.contents_manager.get(path=path)
        name = model['name']
        if model['type'] != 'notebook':
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        self.set_header('Last-Modified', model['last_modified'])

        try:
            output, resources = exporter.from_notebook_node(
                model['content'],
                resources={
                    "metadata": {
                        "name":
                        name[:name.rfind('.')],
                        "modified_date":
                        (model['last_modified'].strftime(text.date_format))
                    },
                    "config_dir": self.application.settings['config_dir'],
                })
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # Force download if requested
        if self.get_argument('download', 'false').lower() == 'true':
            filename = os.path.splitext(
                name)[0] + resources['output_extension']
            self.set_header(
                'Content-Disposition',
                'attachment; filename="%s"' % escape.url_escape(filename))

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.finish(output)
Beispiel #15
0
def get_exporter(format, **kwargs):
    """get an exporter, raising appropriate errors"""
    # if this fails, will raise 500
    try:
        from nbconvert.exporters.base import get_exporter
    except ImportError as e:
        raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e

    try:
        Exporter = get_exporter(format)
    except KeyError as e:
        # should this be 400?
        raise web.HTTPError(404, u"No exporter for format: %s" % format) from e

    try:
        return Exporter(**kwargs)
    except Exception as e:
        app_log.exception("Could not construct Exporter: %s", Exporter)
        raise web.HTTPError(500, "Could not construct Exporter: %s" % e) from e
Beispiel #16
0
def get_exporter(format, **kwargs):
    """get an exporter, raising appropriate errors"""
    # if this fails, will raise 500
    try:
        from nbconvert.exporters.base import get_exporter
    except ImportError as e:
        raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
    
    try:
        Exporter = get_exporter(format)
    except KeyError:
        # should this be 400?
        raise web.HTTPError(404, u"No exporter for format: %s" % format)
    
    try:
        return Exporter(**kwargs)
    except Exception as e:
        app_log.exception("Could not construct Exporter: %s", Exporter)
        raise web.HTTPError(500, "Could not construct Exporter: %s" % e)
Beispiel #17
0
def get_custom_frontend_exporters():
    from nbconvert.exporters.base import get_export_names, get_exporter

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

    # iOS: which extensions are activated?
    show_latex_envs = check_extension_activated('latex_envs/latex_envs')
    show_toc = check_extension_activated('toc2/main')
    show_ch = check_extension_activated('collapsible_headings/main')

    for name in sorted(get_export_names()):
        # iOS: get rid of file types already present in the menu:
        # or formats that won't work (custom, selectLanguage, pdf):
        # script should be re-enabled once there are multiple languages
        if ((name == 'pdf') or (name == 'python') or (name == 'latex')
                or (name == 'markdown') or (name == 'rst') or (name == 'html')
                or (name == 'slides') or (name == 'notebook')
                or (name == 'custom') or (name == 'selectLanguage')
                or (name == 'script')):
            continue
        # Activate menu items depending on extensions being activated:
        if (not show_latex_envs):
            if (name == 'html_with_lenvs' or name == 'html_with_toclenvs'
                    or name == 'latex_with_lenvs'
                    or name == 'slides_with_lenvs'):
                continue
        if (not show_toc):
            if (name == 'html_with_toclenvs' or name == 'toc2'
                    or name == 'html_toc'):
                continue
        if (not show_ch):
            if (name == 'html_ch'): continue
        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)
Beispiel #18
0
    def post(self, format):
        exporter = get_exporter(format, config=self.config)
        
        model = self.get_json_body()
        name = model.get('name', 'notebook.ipynb')
        nbnode = from_dict(model['content'])
        
        try:
            output, resources = exporter.from_notebook_node(nbnode, resources={
                "metadata": {"name": name[:name.rfind('.')],},
                "config_dir": self.application.settings['config_dir'],
            })
        except Exception as e:
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.finish(output)
Beispiel #19
0
    def get(self):
        try:
            from nbconvert.exporters import base
        except ImportError as e:
            raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
        res = {}
        exporters = base.get_export_names()
        for exporter_name in exporters:
            try:
                exporter_class = base.get_exporter(exporter_name)
            except ValueError:
                # I think the only way this will happen is if the entrypoint
                # is uninstalled while this method is running
                continue
            # XXX: According to the docs, it looks like this should be set to None
            # if the exporter shouldn't be exposed to the front-end and a friendly
            # name if it should. However, none of the built-in exports have it defined.
            # if not exporter_class.export_from_notebook:
            #    continue
            res[exporter_name] = {
                "output_mimetype": exporter_class.output_mimetype,
            }

        self.finish(json.dumps(res))
Beispiel #20
0
    def get(self, format, path):

        # iOS, remove '%20' and others in file name:
        if (sys.platform == 'darwin' and os.uname().machine.startswith('iP')):
            unquotedPath = urllib.parse.unquote(path)
            path = unquotedPath

        exporter = get_exporter(format, config=self.config, log=self.log)

        # iOS: remove this line
        if not (sys.platform == 'darwin'
                and os.uname().machine.startswith('iP')):
            path = path.strip('/')
        # If the notebook relates to a real file (default contents manager),
        # give its path to nbconvert.
        if hasattr(self.contents_manager, '_get_os_path'):
            os_path = self.contents_manager._get_os_path(path)
            ext_resources_dir, basename = os.path.split(os_path)
        else:
            ext_resources_dir = None

        model = yield maybe_future(self.contents_manager.get(path=path))
        name = model['name']
        if model['type'] != 'notebook':
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        nb = model['content']

        self.set_header('Last-Modified', model['last_modified'])

        # create resources dictionary
        mod_date = model['last_modified'].strftime(text.date_format)
        nb_title = os.path.splitext(name)[0]

        resource_dict = {
            "metadata": {
                "name": nb_title,
                "modified_date": mod_date
            },
            "config_dir": self.application.settings['config_dir']
        }

        if ext_resources_dir:
            resource_dict['metadata']['path'] = ext_resources_dir

        try:
            output, resources = exporter.from_notebook_node(
                nb, resources=resource_dict)
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e) from e

        if respond_zip(self, name, output, resources):
            return

        # iOS: download the file that was created locally and return:
        if (sys.platform == 'darwin' and os.uname().machine.startswith('iP')):
            filename = os.path.splitext(
                name)[0] + resources['output_extension']
            filename = ext_resources_dir + '/' + os.path.splitext(
                name)[0] + resources['output_extension']
            if (format != 'ipynb'):
                file = open(filename, "w")
                file.write(output)
            self.finish(
                filename
            )  # send back to the application the name of the file we have created
            return

        # Force download if requested
        if self.get_argument('download', 'false').lower() == 'true':
            filename = os.path.splitext(
                name)[0] + resources['output_extension']
            self.set_attachment_header(filename)

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.set_header('Cache-Control',
                        'no-store, no-cache, must-revalidate, max-age=0')
        self.finish(output)
Beispiel #21
0
    def get(self, format, path):

        exporter = get_exporter(format, config=self.config, log=self.log)

        path = path.strip('/')
        # If the notebook relates to a real file (default contents manager),
        # give its path to nbconvert.
        if hasattr(self.contents_manager, '_get_os_path'):
            os_path = self.contents_manager._get_os_path(path)
            ext_resources_dir, basename = os.path.split(os_path)
        else:
            ext_resources_dir = None

        model = self.contents_manager.get(path=path)
        name = model['name']
        if model['type'] != 'notebook':
            # not a notebook, redirect to files
            return FilesRedirectHandler.redirect_to_files(self, path)

        nb = model['content']

        self.set_header('Last-Modified', model['last_modified'])

        # create resources dictionary
        mod_date = model['last_modified'].strftime(text.date_format)
        nb_title = os.path.splitext(name)[0]

        resource_dict = {
            "metadata": {
                "name": nb_title,
                "modified_date": mod_date
            },
            "config_dir": self.application.settings['config_dir']
        }

        if ext_resources_dir:
            resource_dict['metadata']['path'] = ext_resources_dir

        try:
            output, resources = exporter.from_notebook_node(
                nb,
                resources=resource_dict
            )
        except Exception as e:
            self.log.exception("nbconvert failed: %s", e)
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # Force download if requested
        if self.get_argument('download', 'false').lower() == 'true':
            filename = os.path.splitext(name)[0] + resources['output_extension']
            self.set_attachment_header(filename)

        # MIME type
        if exporter.output_mimetype:
            self.set_header('Content-Type',
                            '%s; charset=utf-8' % exporter.output_mimetype)

        self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
        self.finish(output)