wadehuang36
6/24/2016 - 9:01 PM

Convert Zeppelin Notebook to Python code

Convert Zeppelin Notebook to Python code

import sys, json


def convert(input_path, output_path):
    br = "\r\n"
    with open(input_path, "r") as reader, open(output_path, "w") as writer:
        f = json.load(reader)

        for p in f["paragraphs"]:
            if p.get("title"):
                writer.write("# " + p["title"] + br)

            if p.get("text"):
                text = p.get("text")
                if text.startswith("%md"):
                    text = text[4:]
                    writer.write('"""' + br + text + br + '"""' + br)
                else:
                    writer.write(text + br)

            writer.write(br + br)

        writer.flush()


if __name__ == "__main__":
    if len(sys.argv) == 3:
        convert(*sys.argv[1:])
    else:
        sys.stderr.write("The first argument is input json path, the second argument is output path\r\n")