Beispiel #1
0
def write_notebook_output(notebook, output_dir, notebook_name):
    """Extract output from notebook cells and write to files in output_dir.

    This also modifies 'notebook' in-place, adding metadata to each cell that
    maps output mime-types to the filenames the output was saved under.
    """
    resources = dict(unique_key=os.path.join(output_dir, notebook_name),
                     outputs={})

    # Modifies 'resources' in-place
    ExtractOutputPreprocessor().preprocess(notebook, resources)
    # Write the cell outputs to files where we can (images and PDFs),
    # as well as the notebook file.
    FilesWriter(build_directory=output_dir).write(
        nbformat.writes(notebook),
        resources,
        os.path.join(output_dir, notebook_name + ".ipynb"),
    )
    # Write a script too.  Note that utf-8 is the de facto
    # standard encoding for notebooks.
    ext = notebook.metadata.language_info.file_extension
    contents = "\n\n".join(cell.source for cell in notebook.cells)
    with open(os.path.join(output_dir, notebook_name + ext),
              "w",
              encoding="utf8") as f:
        f.write(contents)
Beispiel #2
0
def convert_documentation(nb_path):
    """Run only the document conversion portion of the notebook conversion

      The final document will not be completel
    """

    with open(nb_path) as f:
        nb = nbformat.reads(f.read(), as_version=4)

    doc = ExtractInlineMetatabDoc(package_url="metapack+file:" +
                                  dirname(nb_path)).run(nb)

    package_name = doc.as_version(None)

    output_dir = join(getcwd(), package_name)

    de = DocumentationExporter(config=Config(),
                               log=logger,
                               metadata=doc_metadata(doc))
    prt('Converting documentation')
    output, resources = de.from_filename(nb_path)

    fw = FilesWriter()

    fw.build_directory = join(output_dir, 'docs')
    fw.write(output, resources, notebook_name='notebook')
    prt("Wrote documentation to {}".format(fw.build_directory))
Beispiel #3
0
def notebook_to_rst(nbfilename):
    nbfilepath = os.path.join(EXPATH, nbfilename)
    rstfilename = get_rstfilename(nbfilename)
    output_files_dir = only_filename_no_ext(rstfilename)
    metadata_path = os.path.dirname(rstfilename)
    unique_key = nbfilename.rstrip('.ipynb')

    resources = {
        'metadata': {'path': metadata_path},
        'output_files_dir': output_files_dir,
        # Prefix for the output image filenames
        'unique_key': unique_key
    }

    # Read notebook
    with open(nbfilepath, 'r') as f:
        nb = nbformat.read(f, as_version=4)

    # Export
    exporter = nbsphinx.Exporter(execute='never', allow_errors=True)
    (body, resources) = exporter.from_notebook_node(nb, resources)

    # Correct path for the resources
    for filename in list(resources['outputs'].keys()):
        tmp = os.path.join(RST_PATH, filename)
        resources['outputs'][tmp] = resources['outputs'].pop(filename)

    fw = FilesWriter()
    fw.build_directory = RST_PATH
    # Prevent "not in doctree" complains
    resources['output_extension'] = ''
    body = 'Examples\n--------\n' + body
    fw.write(body, resources, notebook_name=rstfilename)
Beispiel #4
0
def write_notebook_output(notebook, output_dir, notebook_name, location=None):
    """Extract output from notebook cells and write to files in output_dir.

    This also modifies 'notebook' in-place, adding metadata to each cell that
    maps output mime-types to the filenames the output was saved under.
    """
    resources = dict(unique_key=os.path.join(output_dir, notebook_name), outputs={})

    # Modifies 'resources' in-place
    ExtractOutputPreprocessor().preprocess(notebook, resources)
    # Write the cell outputs to files where we can (images and PDFs),
    # as well as the notebook file.
    FilesWriter(build_directory=output_dir).write(
        nbformat.writes(notebook),
        resources,
        os.path.join(output_dir, notebook_name + ".ipynb"),
    )
    # Write a script too.  Note that utf-8 is the de facto
    # standard encoding for notebooks. 
    ext = notebook.metadata.get("language_info", {}).get("file_extension", None)
    if ext is None:
        ext = ".txt"
        js.logger.warning(
            "Notebook code has no file extension metadata, " "defaulting to `.txt`",
            location=location,
        )
    contents = "\n\n".join(cell.source for cell in notebook.cells)

    notebook_file = notebook_name + ext
    output_dir = Path(output_dir)
    (output_dir / notebook_file).write_text(contents, encoding = "utf8")
Beispiel #5
0
def convert_hugo(nb_path, hugo_path):
    from os import environ
    from os.path import abspath

    # Total hack. Would like the -H to be allowed to have no arg, and then use the env var,
    # but I don't know how to do that. This is the case where the user types
    # -H nb_path, so just go with it.
    if hugo_path and not nb_path:
        nb_path = hugo_path
        hugo_path = environ.get('METAPACK_HUGO_DIR')

    if not hugo_path:
        err("Must specify value for -H or the METAPACK_HUGO_DIR environment var"
            )

    if not exists(nb_path):
        err("Notebook path does not exist: '{}' ".format(nb_path))

    c = Config()
    c.HugoExporter.hugo_dir = abspath(
        hugo_path)  # Exports assume rel path is rel to notebook
    he = HugoExporter(config=c, log=logger)

    output, resources = he.from_filename(nb_path)

    prt('Writing Notebook to Hugo Markdown')

    prt('    Writing ',
        resources['unique_key'] + resources['output_extension'])
    for k, v in resources['outputs'].items():
        prt('    Writing ', k)

    fw = FilesWriter()
    fw.write(output, resources, notebook_name=resources['unique_key'])
def write_files(export_list, nb_node, file_name):
    """
    Export and write files from a notebook node.

    Args:
        export_list (list of strings) -- name of valid nbconvert exporters
        nb_node(nbformat node object) -- notebook to be my_converted
        file_name (str) -- base name of file to be written

    Returns:
        None

    """
    try:
        # export and write file.
        for export in export_list:
            if export == "html":
                exporter = HTMLExporter()
            elif export == "py":
                exporter = PythonExporter()

            (body, resources) = exporter.from_notebook_node(nb_node)
            write_file = FilesWriter()
            write_file.write(
                output=body,
                resources=resources,
                notebook_name=file_name
            )
    except Exception as e:
        print(f"There was a problem exporting the file(s): {e}")
    return None
Beispiel #7
0
def export_tex(
    combined_nb: NotebookNode, output_file: Path, pdf=False, template_file=None
):
    """
    A function that exports a .tex file from a notebook node object
    """
    resources = {}
    resources["unique_key"] = "combined"
    resources["output_files_dir"] = "combined_files"

    # log.info('Converting to %s', 'pdf' if pdf else 'latex')
    exporter = MyLatexPDFExporter() if pdf else MyLatexExporter()
    if template_file is not None:
        exporter.template_file = str(template_file)
    mypreprocessor = (
        RegexRemovePreprocessor()
    )  # Create an instance of the RegexRemovePreprocessor
    mypreprocessor.patterns = [
        "\s*\Z"
    ]  # supply a re pattern (in a list) to the preprocessor's .patterns attribute
    exporter.register_preprocessor(
        mypreprocessor, enabled=True
    )  # apply the preprocessor to the exporter
    writer = FilesWriter(build_directory=str(output_file.parent))
    output, resources = exporter.from_notebook_node(combined_nb, resources)
    writer.write(output, resources, notebook_name=output_file.stem)
Beispiel #8
0
def write_notebook_output(notebook, output_dir, notebook_name, location=None):
    """Extract output from notebook cells and write to files in output_dir.

    This also modifies 'notebook' in-place, adding metadata to each cell that
    maps output mime-types to the filenames the output was saved under.
    """
    resources = dict(unique_key=os.path.join(output_dir, notebook_name),
                     outputs={})

    # Modifies 'resources' in-place
    ExtractOutputPreprocessor().preprocess(notebook, resources)
    # Write the cell outputs to files where we can (images and PDFs),
    # as well as the notebook file.
    FilesWriter(build_directory=output_dir).write(
        nbformat.writes(notebook),
        resources,
        os.path.join(output_dir, notebook_name + ".ipynb"),
    )

    exporter = nbconvert.exporters.ScriptExporter(
        log=LoggerAdapterWrapper(js.logger))
    with warnings.catch_warnings():
        # See https://github.com/jupyter/nbconvert/issues/1388
        warnings.simplefilter('ignore', DeprecationWarning)
        contents, resources = exporter.from_notebook_node(notebook)

    notebook_file = notebook_name + resources['output_extension']
    output_dir = Path(output_dir)
    # utf-8 is the de-facto standard encoding for notebooks.
    (output_dir / notebook_file).write_text(contents, encoding="utf8")
Beispiel #9
0
 def start(self):
     self.init_notebooks()
     self.writer = FilesWriter(parent=self, config=self.config)
     self.exporter = self.exporter_class(parent=self, config=self.config)
     for pp in self.preprocessors:
         self.exporter.register_preprocessor(pp)
     self.convert_notebooks()
Beispiel #10
0
def export_tex(nbnode, outfile="export_tex_out", template="classicm"):
    latex_exporter = LatexExporter()
    latex_exporter.template_file = template
    (body, resources) = latex_exporter.from_notebook_node(nbnode)
    writer = FilesWriter()
    writer.write(body, resources,
                 notebook_name=outfile)  # will end up with .tex extension
Beispiel #11
0
def write_page(html, path_out, resources, standalone=False,
               custom_css=None, custom_js=None):
    """
    Write an HTML page to disk and extract images if desired.
    Meant for running after converting a page with `page_html`.
    This uses the nbconvert `FilesWriter` class to write the HTML
    content.

    html : string
        The HTML to be written to disk.
    path_out : string
        The path to the folder where the HTML will be output.
    resources : dictionary
        NBConvert resources to be used in the conversion process. These are
        generated from the `build_book` function.
    standalone : bool
        Whether to write the page as a full standalone HTML file with its own
        <head> and <body> sections. If False, just the converted HTML will
        be written with the expectation that it will be compiled to "full"
        HTML by Jupyter Book later.
    custom_css : string of css | path to css file
        A collection of custom CSS rules to include in the output HTML, or a
        path to a CSS file. Only used if `standalone=True`.
    custom_js : string of javascript | path to js file
        A collection of custom Javascript to include in the output HTML, or a
        path to a CSS file. Only used if `standalone=True`.
    """
    c = Config()
    c.FilesWriter.build_directory = path_out
    notebook_name = op.split(resources.get("unique_key", "notebook"))[-1]

    if custom_css is None:
        custom_css = ''
    elif op.exists(custom_css):
        with open(custom_css, 'r') as ff:
            custom_css = ff.read()
    if custom_js is None:
        custom_js = ''
    elif op.exists(custom_js):
        with open(custom_js, 'r') as ff:
            custom_js = ff.read()

    # If standalone, add a head and body
    if standalone is True:
        head = page_head(custom_css=custom_css, custom_js=custom_js)
        html = f"""
        <!DOCTYPE html>
        {head}

        <body>
        {html}
        <nav class="onthispage"></nav>
        </body>
        </html>\n
        """
    # Now write the html and resources
    writer = FilesWriter(config=c)
    path_html = writer.write(html, resources, notebook_name=notebook_name)
    return path_html
    def convert(self, file):
        assert os.path.exists(
            file), f"this should not happen, path {file} must exist"
        body, resources = self.export(file)

        fw = FilesWriter()
        fw.build_directory = os.path.dirname(file)
        f_name = os.path.basename(file).replace(".ipynb", "")
        fw.write(body, resources, notebook_name=f_name)
Beispiel #13
0
    def _convert(self, tmpdir: Path, entry: Path, outdir: Path, depth: int):
        """Convert a notebook.

        Args:
            tmpdir: Temporary working directory
            entry: notebook to convert
            outdir: output directory for .html and .rst files
            depth: depth below root, for fixing image paths
        """
        test_mode = self.s.get("test_mode")
        # strip special cells.
        if self._has_tagged_cells(entry, set(self._cell_tags.values())):
            _log.debug(f"notebook '{entry.name}' has test cell(s)")
            orig_entry, entry = entry, self._strip_tagged_cells(
                tmpdir, entry, ("remove", "exercise"), "testing")
            notify(f"Stripped tags from: {orig_entry.name}", 3)
        else:
            # copy to temporary directory just to protect from output cruft
            tmp_entry = tmpdir / entry.name
            shutil.copy(entry, tmp_entry)
            orig_entry, entry = entry, tmp_entry

        # convert all tag-stripped versions of the notebook.
        # before running, check if converted result is newer than source file
        if self._already_converted(orig_entry, entry, outdir):
            notify(
                f"Skip notebook conversion, output is newer, for: {entry.name}",
                3)
            self._results.cached.append(entry)
            return
        notify(f"Running notebook: {entry.name}", 3)
        nb = self._parse_and_execute(entry)
        if test_mode:  # don't do conversion in test mode
            return
        notify(f"Exporting notebook '{entry.name}' to directory {outdir}", 3)
        wrt = FilesWriter()
        # export each notebook into multiple target formats
        created_wrapper = False
        for (exp, postprocess_func, pp_args) in (
            (RSTExporter(), self._postprocess_rst, ()),
            (HTMLExporter(), self._postprocess_html, (depth, )),
        ):
            _log.debug(
                f"export '{orig_entry}' with {exp} to notebook '{entry}'")
            (body, resources) = exp.from_notebook_node(nb)
            body = postprocess_func(body, *pp_args)
            wrt.build_directory = str(outdir)
            wrt.write(body, resources, notebook_name=entry.stem)
            # create a 'wrapper' page
            if not created_wrapper:
                _log.debug(
                    f"create wrapper page for '{entry.name}' in '{outdir}'")
                self._create_notebook_wrapper_page(entry.stem, outdir)
                created_wrapper = True
            # move notebooks into docs directory
            _log.debug(f"move notebook '{entry} to output directory: {outdir}")
            shutil.copy(entry, outdir / entry.name)
Beispiel #14
0
def convert(input_fn, output_fn=None):
    """Execute and save notebook as html
    
    Executes notebook, extracts packagelist and saves everything to html
    
    Arguments:
        input_fn {str} -- Input filename of notebook
        output_fn {str} -- Output filename for html file
    """

    _logger.info(f'Reading notebook "{input_fn}"')
    with open(input_fn, encoding="utf-8") as f:
        nb = nbformat.read(f, as_version=4)

    # Execute notebook
    _logger.info(f'Executing notebook...')
    ep = ExecutePreprocessor(timeout=-1)
    starttime = datetime.datetime.today()
    ep.preprocess(nb)
    endtime = datetime.datetime.today()
    _logger.info(f'Executed notebook')

    timestamp = "Executed {} in {}.".format(
        starttime.strftime('%Y-%m-%d %H:%M:%S'),
        pretty_duration((endtime - starttime).total_seconds()))

    # Extract .ipynb file
    scrubbed_nb = scrub_output(copy.deepcopy(nb))
    ipynb_data = nbformat.writes(scrubbed_nb).encode('utf-8')
    ipynb_link = create_embedded_link(os.path.basename(input_fn), ipynb_data)

    # Get package list
    _logger.info(f'Getting package list...')
    packages_data = get_package_list()
    if packages_data:
        packages_link = create_embedded_link('packages.txt', packages_data)
    else:
        packages_link = ''

    # Add files / links
    md = f"---\n {timestamp} {ipynb_link} {packages_link}"
    nb['cells'].append(new_markdown_cell(md))

    # Export to html
    _logger.info(f'Exporting to "{output_fn}"')
    exporter = HTMLExporter()
    (body, resources) = exporter.from_notebook_node(nb)

    if not output_fn:
        output_fn = build_output_filename(input_fn)

    writer = FilesWriter()
    resources['output_extension'] = None
    writer.write(output=body, resources=resources, notebook_name=output_fn)

    _logger.info(f'Finished')
Beispiel #15
0
def write_body_resources(notebook_filename, body, resources, output_dir=None):
    """Write actual notebook and files to output dir.
    Use notebook directory if output dir is none"""
    output_dir = determine_output_dir(notebook_filename, output_dir)
    config = Config()
    config.FilesWriter.build_directory = output_dir
    file_writer = FilesWriter(config=config)
    file_writer.write(body,
                      resources,
                      notebook_name=to_notebook_basename(notebook_filename))
Beispiel #16
0
def export(combined_nb: NotebookNode, output_file: Path, pdf=False):
    resources = {}
    resources['unique_key'] = 'combined'
    resources['output_files_dir'] = 'combined_files'

    log.info('Converting to %s', 'pdf' if pdf else 'latex')
    exporter = MyLatexPDFExporter() if pdf else MyLatexExporter()
    writer = FilesWriter(build_directory=str(output_file.parent))
    output, resources = exporter.from_notebook_node(combined_nb, resources)
    writer.write(output, resources, notebook_name=output_file.stem)
def nbnode_to_ipynb(nb_node, filename="notebookout"):
    """
    function to export a .ipynb file given a notebookNode object as input
    :param nb_node: notebookNode object
    :param filename: str, the name of the output .ipynb file. Don't need extension
    :return: nothing returned, but fuction will output a new .ipynb file
    """
    e = nbconvert.NotebookExporter()
    body, resources = e.from_notebook_node(nb_node)
    writer = FilesWriter()
    writer.write(body, resources, notebook_name=filename)
Beispiel #18
0
def main(input, output, name):
    exporter = MarkdownExporter(template_file=HIDE_TEMPLATE)
    writer = FilesWriter()

    with open(input, 'rb') as f:
        nb = nbformat.read(f, as_version=4)

    (body, res) = exporter.from_notebook_node(nb)
    writer.write(MdFormatter().clear(body),
                 PostProcesser().res_path(res, output),
                 notebook_name=output + name)
Beispiel #19
0
def write_only_body(notebook_filename, body, output_dir=None):
    output_dir = determine_output_dir(notebook_filename, output_dir)
    config = Config()
    config.FilesWriter.build_directory = output_dir
    file_writer = FilesWriter(config=config)
    # no resources since we don't want files written, but add .pdf extension
    resources = dict(output_extension='.pdf')
    # add pdf to filename
    file_writer.write(body,
                      resources,
                      notebook_name=to_notebook_basename(notebook_filename))
Beispiel #20
0
    def convert(self, file):
        assert os.path.exists(
            file), f"this should not happen, path {file} must exist"
        body, resources = self.export(file)

        fw = FilesWriter()
        fw._makedir(self.dst_folder(file))
        fw.build_directory = self.dst_folder(file)
        fw.write(body,
                 resources,
                 notebook_name=self.dest_file(file, withFormat=False))
def nbnode_to_tex(nb_node, filename="texout"):
    """
    function to export a .tex  file given a notebookNode object as input
    :param nb_node: notebookNode object
    :param filename: str, the name of the output .tex file. Don't need .tex extension
    :return: nothing returned, but function will output a new .pdf file
    """
    e = MyLatexExporter
    body, resources = e.from_notebook_node(nb_node)
    writer = FilesWriter()
    writer.write(body, resources, notebook_name=filename)
Beispiel #22
0
 def start(self) -> None:
     self.init_notebooks()
     self.writer = FilesWriter(parent=self, config=self.config)
     self.exporter = self.exporter_class(parent=self, config=self.config)
     for pp in self.preprocessors:
         self.exporter.register_preprocessor(pp)
     currdir = os.getcwd()
     os.chdir(self.coursedir.root)
     try:
         self.convert_notebooks()
     finally:
         os.chdir(currdir)
Beispiel #23
0
def export(filename):

    c = Config()

    # Configure our tag removal
    c.TagRemovePreprocessor.remove_cell_tags = ("hide_cell", )
    c.TagRemovePreprocessor.remove_all_outputs_tags = ('hide_output', )
    c.TagRemovePreprocessor.remove_input_tags = ('hide_input', )

    # Configure and run out exporter
    c.LatexExporter.preprocessors = [
        'nbconvert.preprocessors.TagRemovePreprocessor'
    ]

    targetDirectoryFull_loc = targetDirectoryFull + "\\" + filename.replace(
        ' ', '_')[:-6]
    targetFolder = filename.replace(' ', '_')[:-6]
    writer = FilesWriter()
    writer.build_directory = targetDirectoryFull_loc
    test = writer.write(*CustomLatexExporter(config=c).from_filename(filename),
                        notebook_name=filename[:-6])

    createdFile = targetDirectoryFull_loc + '\\' + filename[:-6] + '.tex'
    toBeCreatedFile = targetDirectoryCore + '\\' + filename[:-6] + '.tex'
    coreFileName = targetDirectoryCore + '\\' + filename[:-6] + '_Core.tex'

    tag = 'maketitle'
    lines_to_write = []
    tag_found = False

    with open(createdFile, encoding="utf8") as in_file:
        for line in in_file:
            if tag in line:
                tag_found = True
            elif tag_found:
                line = re.sub(r"{In}{incolor}{\d*}", r"{In}{incolor}{In}",
                              line)
                line = re.sub(r"{Out}{outcolor}{\d*}", r"{Out}{outcolor}{Out}",
                              line)
                string = r'JupyterNotebooksFull/' + targetFolder + '/'
                line = re.sub(r"(output.*jpg})", string + r"\g<1>", line)
                if r'\prompt{Out}{outcolor}{Out}{}' in line:
                    lines_to_write.append(
                        r'            \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]'
                        + '\n')
                    line = r'\prompt{Out}{outcolor}{Out}{\boxspacing}'
                lines_to_write.append(line)
                if r'{ \hspace*{\fill} \\}' in line:
                    lines_to_write.append(r'\end{tcolorbox}' + '\n')

    lines_to_write = lines_to_write[1:-5]
    with open(toBeCreatedFile, 'w+', encoding="utf8") as out_file:
        out_file.writelines(lines_to_write)
Beispiel #24
0
def main_report_generation_function():
    print('START: downloading and executing notebook from github')
    pm.execute_notebook(
       'https://raw.githubusercontent.com/arkulkarni/COVID-19-Analysis/master/COVID-19-Analysis.ipynb',
       SCRATCH_DIR + '/output.ipynb'
    )

    print('SUCCESS: downloading and executing notebook from github')


    print('START: convert to html')
    #jupyter nbconvert output.ipynb --no-input
    nb = nbformat.read(SCRATCH_DIR + '/output.ipynb', as_version=4)

    # Instantiate the exporter. 
    html_exporter = HTMLExporter()
    html_exporter.template_file = 'nbhtml'

    (body, resources) = html_exporter.from_notebook_node(nb)

    print('SUCCESS: convert to html')

    print('START: save html file')

    write_file = FilesWriter()
    write_file.write(
        output=body,
        resources=resources,
        notebook_name=SCRATCH_DIR + '/index'
        )

    print('SUCCESS: save html file')

    print('START: uploading html file to Cloud Storage')

    storage_client = storage.Client()
    bucket = storage_client.bucket(BUCKET_NAME)
    
    # upload the index.html file
    blob = bucket.blob('index.html')
    blob.upload_from_filename(SCRATCH_DIR + '/index.html')
    print('File index.html uploaded')

    # upload the custom.css file
    blob = bucket.blob('custom.css')
    blob.upload_from_filename('./custom.css')
    print('File custom.css uploaded')

    print('SUCCESS: uploading html and css file to Cloud Storage')

    gc.collect()
    return 'Done processing and updating the report'
def convertNotebooktoLaTeX(notebookPath, outfilePath='latex_out1', template='classicm'):
    REG_nb = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')
    base_nb_filename = os.path.basename(notebookPath)
    if REG_nb.match(base_nb_filename):
        with open(notebookPath) as fh:
            nbnode = nbformat.read(fh, as_version=4)

        exporter = LatexExporter()
        exporter.template_file = template  # classicm style if not specified
        exporter.file_extension = '.tex'
        exporter.register_preprocessor(RegexRemovePreprocessor, enabled=False)  ###########################
        body, resources = exporter.from_notebook_node(nbnode)
        writer = FilesWriter()
        writer.write(body, resources, notebook_name=outfilePath)  # will end up with .tex extension
Beispiel #26
0
    def convert(self, remove_executed=False):
        """
        Convert the executed notebook to a restructured text (RST) file.

        Parameters
        ----------
        delete_executed : bool, optional
            Controls whether to remove the executed notebook or not.
        """

        if not path.exists(self._executed_nb_path):
            raise IOError(
                "Executed notebook file doesn't exist! Expected: {0}".format(
                    self._executed_nb_path))

        if path.exists(self._rst_path) and not self.overwrite:
            logger.debug(
                "RST version of notebook already exists at {0}. Use "
                "overwrite=True or --overwrite (at cmd line) to re-run".format(
                    self._rst_path))
            return self._rst_path

        # Initialize the resources dict - see:
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/nbconvertapp.py#L327
        resources = {}
        resources['config_dir'] = ''  # we don't need to specify config
        resources['unique_key'] = self.nb_name

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        # Exports the notebook to RST
        logger.debug('Exporting notebook to RST...')
        exporter = RSTExporter()

        if self.template_file:
            exporter.template_file = self.template_file
        output, resources = exporter.from_filename(self._executed_nb_path,
                                                   resources=resources)

        # Write the output RST file
        writer = FilesWriter()
        output_file_path = writer.write(output,
                                        resources,
                                        notebook_name=self.nb_name)

        if remove_executed:  # optionally, clean up the executed notebook file
            remove(self._executed_nb_path)

        return output_file_path
def export_nbnode(
    combined_nb: NotebookNode, output_file: Path, pdf=False, template_file=None
):
    resources = {}
    resources["unique_key"] = "combined"
    resources["output_files_dir"] = "combined_files"

    # log.info('Converting to %s', 'pdf' if pdf else 'latex')
    exporter = MyLatexPDFExporter() if pdf else MyLatexExporter()
    if template_file is not None:
        exporter.template_file = str(template_file)
    writer = FilesWriter(build_directory=str(output_file.parent))
    output, resources = exporter.from_notebook_node(combined_nb, resources)
    writer.write(output, resources, notebook_name=output_file.stem)
Beispiel #28
0
def convert_notebook(nb_path):
    prt('Convert notebook to Metatab source package')

    if not exists(nb_path):
        err("Notebook path does not exist: '{}' ".format(nb_path))

    c = Config()

    pe = NotebookExecutor(config=c, log=logger)

    prt('Running the notebook')
    output, resources = pe.from_filename(nb_path)

    fw = FilesWriter()
    fw.build_directory = pe.output_dir

    fw.write(output, resources, notebook_name=DEFAULT_METATAB_FILE)

    de = DocumentationExporter(config=c,
                               log=logger,
                               metadata=doc_metadata(pe.doc))

    prt('Exporting documentation')
    output, resources = de.from_filename(nb_path)

    fw.build_directory = join(pe.output_dir, 'docs')
    fw.write(output, resources, notebook_name='notebook')

    new_mt_file = join(pe.output_dir, DEFAULT_METATAB_FILE)

    doc = MetapackDoc(new_mt_file)

    de.update_metatab(doc, resources)

    for lib_dir in pe.lib_dirs:
        lib_dir = normpath(lib_dir).lstrip('./')

        doc['Resources'].new_term("Root.PythonLib", lib_dir)

        path = abspath(lib_dir)
        dest = join(pe.output_dir, lib_dir)

        ensure_dir(dest)
        copytree(path, join(pe.output_dir, lib_dir))

    doc.write_csv()

    # Reset the input to use the new data

    prt('Running with new package file: {}'.format(new_mt_file))
Beispiel #29
0
    def from_meeting(self, meeting: Meeting):
        notebook_path = repositories.local_meeting_root(meeting) / "".join(
            [repr(meeting), FileExtensions.Solutionbook])

        # TODO concatenate front matter to notebook output
        front_matter = templates.load("meeting/hugo-front-matter.md.j2")
        front_matter = front_matter.render(
            **{
                "group": repr(meeting.group),
                "meeting": {
                    "title": meeting.required["title"],
                    "date": meeting.meta.date.isoformat(),
                    # TODO decide on what date qualifies to be `lastmod`
                    "lastmod": meeting.meta.date.isoformat(),
                    "authors": meeting.required["instructors"],
                    "tags": meeting.optional["tags"],
                    "description": meeting.required["description"],
                    "weight": meeting.number,
                    "room": meeting.meta.room,
                    "cover": meeting.required["cover"],
                },
                "semester": {
                    "full": str(meeting.group.semester),
                    "short": repr(meeting.group.semester),
                },
                "urls": {
                    "youtube": urlgen.youtube(meeting),
                    "slides": urlgen.slides(meeting),
                    "github": urlgen.github(meeting),
                    "kaggle": urlgen.kaggle(meeting),
                    "colab": urlgen.colab(meeting),
                },
            })

        # the notebook is output as a string, so treat it as such when concatenating
        notebook, resources = self.from_filename(str(notebook_path),
                                                 resources=None)
        resources.update({"output_extension": ".md"})

        writer = FilesWriter(
            build_directory=str(paths.site_group_folder_from_meeting(meeting)))

        front_matter_plus_notebook = f"{front_matter}\n{notebook}"

        writer.write(front_matter_plus_notebook, resources,
                     meeting.required["filename"])
        # writer.write(notebook, resources, meeting.required["filename"])

        return notebook, resources
Beispiel #30
0
def ipynb_to_jupyter(path):
    """Replace given ``.ipynb`` file with a ``.jupyter`` file.

    WARNING: This deletes the original file!

    :param path: Path to ``.ipynb`` file.
    :type path: os.PathLike or str

    """
    path = Path(path)
    exporter = JupyterExporter()
    nb, resources = exporter.from_filename(str(path))
    writer = FilesWriter()
    writer.write(nb, resources, notebook_name=path.with_suffix('').name)
    path.unlink()