Working experimental notebook generator

This commit is contained in:
Satwik 2019-11-03 00:54:27 +05:30
parent 2cd7e3fccd
commit e18e56eba3
1 changed files with 18 additions and 10 deletions

View File

@ -21,7 +21,7 @@ import json
import os import os
import pprint import pprint
fpath = os.path.join(os.path.dirname( __file__ ), '..', 'test_nb.md') fpath = os.path.join(os.path.dirname( __file__ ), '..', 'README.md')
examples = [] examples = []
# The globals # The globals
@ -73,11 +73,12 @@ def parse_example_parts(lines, example_title_line):
output_so_far = [] output_so_far = []
next_line = example_title_line next_line = example_title_line
# store build_up till an H4 (explanation) is encountered # store build_up till an H4 (explanation) is encountered
while not next_line.startswith("#### "): while not (next_line.startswith("#### ")or next_line.startswith('---')):
# Watching out for the snippets # Watching out for the snippets
if next_line.startswith("```"): if next_line.startswith("```py"):
# It's a snippet, whatever found until now is text # It's a snippet, whatever found until now is text
is_interactive = False is_interactive = False
output_encountered = False
if content: if content:
parts["build_up"].append(generate_markdown_block(content)) parts["build_up"].append(generate_markdown_block(content))
content = [] content = []
@ -95,6 +96,8 @@ def parse_example_parts(lines, example_title_line):
# can be either output or normal code # can be either output or normal code
if is_interactive: if is_interactive:
output_so_far.append(next_line) output_so_far.append(next_line)
elif output_encountered:
output_so_far.append(next_line)
else: else:
statements_so_far.append(next_line) statements_so_far.append(next_line)
next_line = next(lines) next_line = next(lines)
@ -119,7 +122,7 @@ def parse_example_parts(lines, example_title_line):
# store lines again until --- or another H3 is encountered # store lines again until --- or another H3 is encountered
while not (next_line.startswith("---") or while not (next_line.startswith("---") or
next_line.startswith("### ")): next_line.startswith("### ")):
if next_line.lstrip().startswith("```"): if next_line.lstrip().startswith("```py"):
# It's a snippet, whatever found until now is text # It's a snippet, whatever found until now is text
is_interactive = False is_interactive = False
if content: if content:
@ -231,18 +234,22 @@ def convert_to_cells(cell_contents):
return cells return cells
def convert_to_notebook(parsed_json): def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content):
result = { result = {
"cells": [], "cells": [],
"metadata": {}, "metadata": {},
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 2 "nbformat_minor": 2
} }
notebook_path = "test.ipynb"
result["cells"] += convert_to_cells([generate_markdown_block(pre_examples_content)])
for example in parsed_json: for example in parsed_json:
parts = example["parts"] parts = example["parts"]
build_up = parts.get("build_up") build_up = parts.get("build_up")
explanation = parts.get("explanation") explanation = parts.get("explanation")
notebook_path = "test.ipynb"
if build_up: if build_up:
result["cells"] += convert_to_cells(build_up) result["cells"] += convert_to_cells(build_up)
@ -250,6 +257,8 @@ def convert_to_notebook(parsed_json):
if explanation: if explanation:
result["cells"] += convert_to_cells(explanation) result["cells"] += convert_to_cells(explanation)
result["cells"] += convert_to_cells([generate_markdown_block(post_examples_content)])
pprint.pprint(result, indent=2) pprint.pprint(result, indent=2)
with open(notebook_path, "w") as f: with open(notebook_path, "w") as f:
json.dump(result, f) json.dump(result, f)
@ -271,7 +280,7 @@ with open(fpath, 'r+', encoding="utf-8") as f:
section_text = [] section_text = []
line = next(lines) line = next(lines)
# Until a new section is encountered # Until a new section is encountered
while not (line.startswith("## " )): while not (line.startswith("## ") or line.startswith("# ")):
# check if it's a H3 # check if it's a H3
if line.startswith("### "): if line.startswith("### "):
# An example is encountered # An example is encountered
@ -294,7 +303,6 @@ with open(fpath, 'r+', encoding="utf-8") as f:
post_stuff.append(line) post_stuff.append(line)
line = next(lines) line = next(lines)
except StopIteration: except StopIteration as e:
pprint.pprint(result, indent=2) pprint.pprint(result, indent=2)
print("POST", post_stuff) convert_to_notebook(pre_stuff, result, post_stuff)
convert_to_notebook(result)