コード例 #1
0
def export_notebook(notebook_filename, resources):
    """Step 2: Export the notebook
        Exports the notebook to a particular format according to the specified
        exporter. This function returns the output and (possibly modified)
        resources from the exporter.
        Parameters
        ----------
        notebook_filename : str
            name of notebook file.
        resources : dict
        Returns
        -------
        output
        dict
            resources (possibly modified)
        """
    config = Config()
    basePath = os.path.dirname(__file__)
    exporter = MarkdownExporter(
        config=config,
        template_path=[os.path.join(basePath, 'templates/')],
        template_file='Jekyll_template.tpl',
        filters={'jekyllpath': jekyllpath})
    content, resources = exporter.from_filename(notebook_filename,
                                                resources=resources)
    content = parse_html(content)
    return content, resources
コード例 #2
0
    def get_output_resources(self, output_path=None):
        '''
        Markdown Exporter
        '''
        if output_path is None:
            output_path = os.path.join(os.path.sep, "assets", "images",
                                       self.type, self.notebook_name)
            try:
                # We are creating nested directories, hence
                os.makedirs(self.blogpath + output_path)
            except:
                pass
        self.output_path = self.blogpath + "/" + output_path
        md = MarkdownExporter()

        # Extract config dictionary
        nbapp = nbconvertapp.NbConvertApp()
        self.config = nbapp.init_single_notebook_resources(self.notebook_path)
        self.config['output_files_dir'] = output_path
        '''
        Of type:

        {'config_dir': '/Users/Ankivarun/.jupyter', \
         'unique_key': self.notebook_name,\
         'output_files_dir': output_path}
        '''

        self.output, self.resources = md.from_filename(self.notebook_path,
                                                       self.config)
コード例 #3
0
ファイル: jupyter.py プロジェクト: nimbus-bp-1729/lrrr
class Jupyter:
    def __init__(self, template, root):
        self.root = root
        # self.exporter = HTMLExporter()
        self.mexporter = MarkdownExporter()
        self.markdown = Markdown(template, root)
        # self.exporter.template_name = 'base'
        # self.exporter.theme = "light"
        # self.anchor_link_text = ""
        self.template = template

    def to_markdown(self, dest, file, to_main):
        (mark, resources) = self.mexporter.from_filename(file)

        # change: ![png](file) -> ![](file)
        # otherwise you have png everywhere
        mark = mark.replace("![png]", "![]")

        # Save embedded images as png files
        if "outputs" in resources:
            for k,v in resources["outputs"].items():
                with open(f"{dest}/{k}", "wb") as fd:
                    fd.write(v)

        fname, ext = os.path.splitext(file)
        with open(f"{dest}/{fname}.md", "w") as fd:
            fd.write(mark)

    def to_html(self, dest, file, to_main):
        fname, ext = os.path.splitext(file)

        self.to_markdown(dest, file, to_main)
        self.markdown.process(dest, f"{dest}/{fname}.md", to_main)

        # remove the temporary markdown file ... all done
        rm(f"{dest}/{fname}.md")

        print(f"{Fore.GREEN}>> Jupyter: {file}{Fore.RESET}")
コード例 #4
0
ファイル: convert_ipynb.py プロジェクト: lumnsix/haystack
import re

from nbconvert import MarkdownExporter
import os
from pathlib import Path
from headers import headers


def atoi(text):
    return int(text) if text.isdigit() else text


def natural_keys(text):
    test = [atoi(c) for c in re.split("(\d+)", text)]
    return test


dir = Path("../../../../tutorials")

notebooks = [x for x in os.listdir(dir) if x[-6:] == ".ipynb"]
# sort notebooks based on numbers within name of notebook
notebooks = sorted(notebooks, key=lambda x: natural_keys(x))

e = MarkdownExporter(exclude_output=True)
for i, nb in enumerate(notebooks):
    body, resources = e.from_filename(dir / nb)
    with open(str(i + 1) + ".md", "w") as f:
        f.write(headers[i + 1] + "\n\n")
        f.write(body)
コード例 #5
0
ファイル: convert.py プロジェクト: abnowack/ButteredPost
class Page(object):
    def __init__(self, filename, input_directory, output_directory):
        self.md_converter = markdown.Markdown(
            extensions=[MetaExtension(), MathJaxExtension(), CodeHiliteExtension(), FencedCodeExtension(),
                        ChecklistExtension()])
        self.jupyter_converter = MarkdownExporter()

        self.filename = filename
        self.input_directory = input_directory
        self.output_directory = output_directory
        self.markdown = ''
        self.html = ''
        self.info = {}
        self.additional_files = {}

        name, extension = os.path.splitext(filename)
        self.html_filename = name + '.html'

        input_filename = os.path.join(input_directory, filename)
        if extension in MARKDOWN_FILES:
            self.read_markdown(input_filename)
        elif extension in JUPYTER_FILES:
            self.read_jupyter(input_filename)
        else:
            pass

        # do a throwaway markdown conversion to extract the metadata
        self.convert_markdown_to_html()
        self.html = ''

    @property
    def title(self):
        name, ext = os.path.splitext(self.filename)
        return self.info.get('title', [name])[0]

    @property
    def url(self):
        split_path = self.output_directory.split(os.path.sep)
        relative_directory = r''
        if split_path[-1] != '':
            relative_directory = os.path.join(*split_path[1:])
            url_path = os.path.join(relative_directory + os.path.sep, self.html_filename)
        else:
            url_path = self.html_filename
        return url_path

    @property
    def date(self):
        d = self.info.get('date', [''])[0]
        if d == '':
            return None
        for fmt in ('%Y-%m-%d', '%m/%d/%Y', '%B %d, %Y'):
            try:
                dtime = datetime.strptime(d, fmt)
            except ValueError:
                pass
            else:
                return dtime

    def read_markdown(self, markdown_filepath):
        with open(markdown_filepath, 'r') as markdown_file:
            self.markdown = markdown_file.read()

    def read_jupyter(self, jupyter_filepath):
        (self.markdown, resources) = self.jupyter_converter.from_filename(jupyter_filepath)

        # copy over the other resources
        for key, value in resources['outputs'].items():
            output_filepath = os.path.join(self.output_directory, key)
            self.additional_files[output_filepath] = value

    def convert_markdown_to_html(self):
        self.html = self.md_converter.convert(self.markdown)
        if hasattr(self.md_converter, 'Meta'):
            for key, val in self.md_converter.Meta.items():
                self.info[key] = val
        self.md_converter.reset()
コード例 #6
0
        if not executed_notebook.exists():

            t = time.time()

            with open(str(notebook)) as f:
                nb = nbformat.read(f, as_version=4)

            ep.preprocess(nb, {'metadata': {'path': str(notebook_pth)}})

            print(f"Elapsed time : {time.time()-t}")

            with open(str(executed_notebook), "w") as f:
                nbformat.write(nb, f)

        # 3. Process the notebook we loaded earlier
        body, resources = html_exporter.from_filename(str(executed_notebook))

        writer = FilesWriter()
        writer.build_directory = str(html_pth)

        nb_name = executed_notebook.stem.split(".")[0]

        writer.write(body, resources, notebook_name=nb_name)


        snippet_map_to_file[nb_name] = Path("notebooks_md") / (str(nb_name)+".md")

    with open("snippets.yaml", "w") as f:
        yaml.dump(snippet_map_to_file, f)

    with open("snippets.md", "w") as f: