Esempio n. 1
0
 def run(self, ipub_config=None):
     if ipub_config is None:
         ipub_config = {}
     ipub_config["outpath"] = str(self.converted_path)
     app = IpyPubMain(config={"IpyPubMain": ipub_config})
     self._output_data = app(self.input_file if self.
                             input_file is not None else self.source_path)
def convert_all(inpath, outpath):
    """ convert notebook using all available plugins """
    for plugin_name, plugin_path in iter_all_export_paths():

        out_folder = tempfile.mkdtemp()
        publish = IpyPubMain(config={
            "IpyPubMain": {
                "conversion": plugin_name,
                "outpath": out_folder
            }
        })
        try:
            outdata = publish(str(inpath))

            exporter = outdata["exporter"]
            outpath = outdata["outpath"]

            extension = exporter.file_extension
            out_name = os.path.splitext(os.path.basename(
                str(inpath)))[0] + extension
            outfile = os.path.join(out_folder, out_name)

            if not os.path.exists(outfile):
                raise IOError("could not find: {} for {}".format(
                    outfile, plugin_name))

            shutil.copyfile(
                outfile, os.path.join(str(outpath), plugin_name + extension))

        finally:
            shutil.rmtree(out_folder)
Esempio n. 3
0
def test_nbexport_latex_empty(ipynb_app):
    template = str_to_jinja('', "template_name")
    config = dict_to_config({'LatexExporter.template_file': "template_name"})
    exporter_cls = create_exporter_cls('nbconvert.exporters.LatexExporter')
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == 'text/latex'
    assert body == ''
Esempio n. 4
0
def test_nbexport_html_empty(ipynb_app):
    template = str_to_jinja("", "template_name")
    config = dict_to_config({"HTMLExporter.template_file": "template_name"})
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter_cls = create_exporter_cls("nbconvert.exporters.HTMLExporter")
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == "text/html"

    assert body == ""
Esempio n. 5
0
def test_nbexport_latex_mkdown1(ipynb_app):
    template = str_to_jinja(
        """
((* block markdowncell scoped *))
test123
((* endblock markdowncell *))
    """, "template_name")
    config = dict_to_config({'LatexExporter.template_file': "template_name"})
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter_cls = create_exporter_cls('nbconvert.exporters.LatexExporter')
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == 'text/latex'
    assert body.strip() == 'test123'
Esempio n. 6
0
def test_nbexport_html_mkdown2(ipynb_app):
    template = str_to_jinja(
        """
{%- extends 'display_priority.tpl' -%}
{% block markdowncell scoped %}
{{cell.source}}
{% endblock markdowncell %}
    """, "template_name")
    config = dict_to_config({'HTMLExporter.template_file': "template_name"})
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter_cls = create_exporter_cls('nbconvert.exporters.HTMLExporter')
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == 'text/html'

    assert body.strip() == '# a title\n\nsome text'
Esempio n. 7
0
def test_nbexport_html_mkdown1(ipynb_app):
    template = str_to_jinja(
        """
{% block markdowncell scoped %}
test123
{% endblock markdowncell %}
    """,
        "template_name",
    )
    config = dict_to_config({"HTMLExporter.template_file": "template_name"})
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter_cls = create_exporter_cls("nbconvert.exporters.HTMLExporter")
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == "text/html"

    assert body.strip() == "test123"
Esempio n. 8
0
def test_nbexport_latex_mkdown2(ipynb_app):
    template = str_to_jinja(
        """
((*- extends 'display_priority.tplx' -*))
((* block markdowncell scoped *))
(((cell.source)))
((* endblock markdowncell *))
    """,
        "template_name",
    )
    config = dict_to_config({"LatexExporter.template_file": "template_name"})
    nb, path = nbmerge.merge_notebooks(ipynb_app.input_file)
    exporter_cls = create_exporter_cls("nbconvert.exporters.LatexExporter")
    exporter, body, resources = IpyPubMain().export_notebook(
        nb, exporter_cls, config, template)
    assert exporter.output_mimetype == "text/latex"

    assert body.strip() == "# a title\n\nsome text"
Esempio n. 9
0
def nbpublish(ipynb_path,
              outformat='latex_ipypublish_main',
              outpath=None,
              dump_files=True,
              ignore_prefix='_',
              clear_files=False,
              create_pdf=False,
              pdf_in_temp=False,
              pdf_debug=False,
              launch_browser=False,
              log_level='INFO',
              dry_run=False,
              print_traceback=False,
              export_paths=()):
    """ convert one or more Jupyter notebooks to a published format

    paths can be string of an existing file or folder,
    or a pathlib.Path like object

    Parameters
    ----------
    ipynb_path
        notebook file or directory
    outformat: str
        output format to use
    outpath : str or pathlib.Path
        path to output converted files
    dump_files: bool
        write files from nbconvert (containing images, etc) to outpath
    ignore_prefix: str
        ignore ipynb files with this prefix
    clear_files : str
        whether to clear existing external files in outpath folder
    create_pdf: bool
        convert to pdf (if converting to latex)
    pdf_in_temp: bool
        run pdf conversion in a temporary folder
        and only copy back the pdf file
    pdf_debug: bool
        run latexmk in interactive mode
    log_level: str
        the logging level (debug, info, critical, ...)

    """
    # run
    config = {
        "IpyPubMain": {
            "conversion":
            outformat,
            "plugin_folder_paths":
            export_paths,
            "outpath":
            outpath,
            "ignore_prefix":
            ignore_prefix,
            "log_to_stdout":
            True,
            "log_level_stdout":
            log_level,
            "log_to_file":
            True,
            "log_level_file":
            log_level,
            "default_pporder_kwargs":
            dict(
                dry_run=dry_run,
                clear_existing=clear_files,
                dump_files=dump_files,
                create_pdf=create_pdf,
            ),
            "default_ppconfig_kwargs":
            dict(pdf_in_temp=pdf_in_temp,
                 pdf_debug=pdf_debug,
                 launch_browser=launch_browser)
        }
    }
    publish = IpyPubMain(config=config)
    try:
        publish(ipynb_path)
    except Exception as err:
        logger.error("Run Failed: {}".format(err))
        if print_traceback:
            raise
        return 1

    return 0
Esempio n. 10
0
    def parse(self, inputstring, document):
        # type: (Union[str, list[str]], nodes.document) -> None
        """Parse text and generate a document tree."""

        # fix for when calling on readthedocs
        self.env = self.env or document.settings.env
        self.config = self.config or document.settings.env.config

        # get file for conversion
        filepath = self.env.doc2path(self.env.docname)
        filedir = os.path.dirname(filepath)
        self.logger.info("ipypublish: converting {}".format(filepath))

        config = {
            "IpyPubMain": {
                "conversion":
                self.config.ipysphinx_export_config,
                "plugin_folder_paths":
                self.config.ipysphinx_config_folders,
                "outpath":
                filedir,
                "folder_suffix":
                self.config.ipysphinx_folder_suffix,
                "log_to_stdout":
                False,
                "log_to_file":
                False,
                "default_pporder_kwargs":
                dict(clear_existing=False, dump_files=True)
            }
        }
        if self.config.ipysphinx_preconverters:
            # NB: jupytext is already a default for .Rmd
            config["IpyPubMain"]["pre_conversion_funcs"] = (
                self.config.ipysphinx_preconverters)
        publish = IpyPubMain(config=config)
        outdata = publish(filepath)

        self.logger.info("ipypublish: successful conversion")

        # check we got back restructuredtext
        exporter = outdata["exporter"]
        if not exporter.output_mimetype == 'text/restructuredtext':
            handle_error(
                "ipypublish: the output content is not of type "
                "text/restructuredtext: {}".format(exporter.output_mimetype),
                TypeError, self.logger)

        # TODO document use of orphan
        if outdata["resources"].get("ipub", {}).get("orphan", False):
            rst.Parser.parse(self, ':orphan:', document)

        # parse a prolog
        if self.env.config.ipysphinx_prolog:
            prolog = exporter.environment.from_string(
                self.env.config.ipysphinx_prolog).render(env=self.env)
            rst.Parser.parse(self, prolog, document)

        # parse the main body of the file
        rst.Parser.parse(self, outdata["stream"], document)

        # parse an epilog
        if self.env.config.ipysphinx_epilog:
            prolog = exporter.environment.from_string(
                self.env.config.ipysphinx_epilog).render(env=self.env)
            rst.Parser.parse(self, prolog, document)
Esempio n. 11
0
def nbpresent(
        inpath,
        outformat="slides_standard",
        outpath=None,
        dump_files=True,
        ignore_prefix="_",
        clear_files=False,
        log_level="INFO",
        dry_run=False,
        print_traceback=False,
        export_paths=(),
):
    """ load reveal.js slides as a web server,
    converting from ipynb first if path extension is .ipynb

    Parameters
    ----------
    inpath: str
        path to html or ipynb file
    outformat: str
        conversion format to use
    outpath : str  or pathlib.Path
        path to output converted files
    dump_files: bool
        whether to write files from nbconvert (images, etc) to outpath
    clear_files : str
        whether to clear existing external files in outpath folder
    ignore_prefix: str
        ignore ipynb files with this prefix
    log_level: str
        the logging level (debug, info, critical, ...)

    """
    inpath_name, inpath_ext = os.path.splitext(os.path.basename(inpath))

    output_mimetype = guess_type(inpath, strict=False)[0]
    output_mimetype = "unknown" if output_mimetype is None else output_mimetype

    if output_mimetype != "text/html":

        config = {
            "IpyPubMain": {
                "conversion":
                outformat,
                "plugin_folder_paths":
                export_paths,
                "outpath":
                outpath,
                "ignore_prefix":
                ignore_prefix,
                "log_to_stdout":
                True,
                "log_level_stdout":
                log_level,
                "log_to_file":
                True,
                "log_level_file":
                log_level,
                "default_pporder_kwargs":
                dict(
                    dry_run=dry_run,
                    clear_existing=clear_files,
                    dump_files=dump_files,
                    serve_html=True,
                    slides=True,
                ),
            }
        }
        publish = IpyPubMain(config=config)
        try:
            outdata = publish(inpath)

            outpath = outdata["outpath"]
            output_mimetype = outdata["exporter"].output_mimetype

        except Exception as err:
            logger.error("Run Failed: {}".format(err))
            if print_traceback:
                raise
            return 1
    else:
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        server = RevealServer()
        if not dry_run:
            server.postprocess("", output_mimetype, os.path.abspath(inpath))

    return 0