Beispiel #1
0
def convert_notebook_to_markdown(filename):
    global ROOT_DIR
    with open(filename, 'r') as f:
        notebook = nbformat.read(f, 4)

    # Metadata 설정하기
    fname = os.path.splitext(os.path.split(filename)[1])[0]
    metadata = [name.strip() for name in fname.split('|')]

    notebook.metadata['title'] = metadata[0]
    if len(metadata) >= 2:
        notebook.metadata['categories'] = metadata[1].strip()
    if len(metadata) >= 3:
        notebook.metadata['tags'] = metadata[2].strip()

    curr_time = datetime.now() - timedelta(days=1)
    notebook.metadata['modified_date'] = str(curr_time)[:-7]

    exporter = MarkdownExporter()
    exporter.template_file = os.path.join(ROOT_DIR, 'jekyll.tpl')
    body, resource = exporter.from_notebook_node(notebook)

    curr_date = curr_time.strftime("%Y-%m-%d")
    markdown_path = os.path.join(ROOT_DIR, "_posts",
                                 curr_date + "-" + metadata[0] + ".md")

    with open(markdown_path, "w") as f:
        f.write(body)
Beispiel #2
0
def convert_notebook_to_presentation(notebook_path, markdown_path):

    notebook_filename = notebook_path
    with open(notebook_filename, encoding="utf8") as f:
        nb = nbformat.read(f, as_version=4)

    path = os.path.split(os.path.abspath(__file__))[0]
    c = Config()
    c.MarkdownExporter.preprocessors = [ChangeIbynbLink]

    # 2. Instantiate the exporter. We use the `basic` template for now; we'll get into more details
    # later about how to customize the exporter further.
    markdown_exporter = MarkdownExporter(config=c)
    markdown_exporter.template_file = os.path.join(path, 'hidecode.tplx')

    # 3. Process the notebook we loaded earlier
    (body, resources) = markdown_exporter.from_notebook_node(nb)

    writer = nbconvert.writers.FilesWriter()
    writer.write(body, resources, markdown_path)