How to use the nbconvert.exporters.base.get_exporter function in nbconvert

To help you get started, we’ve selected a few nbconvert examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jupyter / notebook / notebook / services / nbconvert / handlers.py View on Github external
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))
github deathbeds / jupyter-graphql / src / jupyter_graphql / schema / nbconvert / __init__.py View on Github external
def resolve_format(self, info, path):
        exporter = get_exporter(name)()
        model = CM(info).get(path=path)
        try:
            output, resources = exporter.from_notebook_node(
                nb=model["content"], resources={}
            )
        except Exception as err:
            print(err)
        return {"output": output, "resources": resources}
github jupyter / notebook / notebook / nbconvert / handlers.py View on Github external
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)
github py4ds / nbless / src / nbless / nbconv.py View on Github external
".asc": "asciidoc",
            ".pdf": "pdf",
            ".html": "html",
            ".tex": "latex",
            ".md": "markdown",
            ".py": "python",
            ".R": "script",
            ".rst": "rst",
        }
        if out_path.suffix in ext_exp_dict:
            exporter = ext_exp_dict[out_path.suffix]
        else:
            print("Unable to infer exporter type from output filename!")
            print("Setting exporter to HTML!")
            exporter = "html"
    contents, resources = get_exporter(exporter)().from_filename(in_file)
    if not out_file:
        name = in_path.stem + resources.get("output_extension", ".txt")
        return name, contents
    else:
        return out_file, contents
github jupyter / notebook / notebook / notebook / handlers.py View on Github external
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: