Last active
November 15, 2023 18:37
-
-
Save tos-kamiya/c49b051db3014f9d386fc75b45ccd4ec to your computer and use it in GitHub Desktop.
A tool to convert Jupyter Notebook (.ipynb) to Markdown (.md)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# https://chat.openai.com/share/448d1592-749e-49f4-8e44-948b0207d075 | |
def to_markdown(jypyter_notebook_json): | |
cells = jypyter_notebook_json["cells"] | |
for cell in cells: | |
ct = cell["cell_type"] | |
if ct == "markdown": | |
src = cell["source"] | |
for text in src: | |
text = text.rstrip() | |
yield text | |
yield "" | |
elif ct == "code": | |
src = cell["source"] | |
yield "```" | |
for text in src: | |
text = text.rstrip() | |
yield text | |
yield "```" | |
yield "" | |
outputs = cell["outputs"] | |
for output in outputs: | |
if "data" in output: | |
src = output["data"]["text/plain"] | |
for text in src: | |
text = text.rstrip() | |
yield "> " + text | |
yield "" | |
else: | |
yield "> ***NODATA**" + text | |
yield "" | |
else: | |
assert False, f"unknown cell type: {ct}" | |
def main(): | |
import argparse | |
import json | |
import sys | |
jupyter_notebook_ext = ".ipynb" | |
parser = argparse.ArgumentParser(description='Convert Jupyter notebook to Markdown.') | |
parser.add_argument('input_jupyter_note', type=str, help='Input Jupyter notebook file or "-" for standard input.') | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('-o', '--output', type=str, help='Output file name. If omitted, output to standard output.') | |
group.add_argument('-O', '--auto-output', action='store_true', help='Automatically generate output file name based on the input file name.') | |
args = parser.parse_args() | |
# read Jupyter notebook | |
if args.input_jupyter_note == '-': | |
if args.auto_output: | |
parser.error("Cannot use -O option with standard input.") | |
obj = json.load(sys.stdin) | |
else: | |
if not args.input_jupyter_note.endswith(jupyter_notebook_ext): | |
parser.error(f"Expected a Jupyter notebook file ending with {jupyter_notebook_ext}") | |
with open(args.input_jupyter_note, encoding="utf-8") as inp: | |
obj = json.load(inp) | |
# get output file name | |
if args.output: | |
output_file = args.output | |
elif args.auto_output: | |
output_file = args.input_jupyter_note[:-len(jupyter_notebook_ext)] + ".md" | |
else: | |
output_file = None | |
# convert to markdown text and output it | |
if output_file: | |
with open(output_file, "w", encoding="utf-8") as outp: | |
for line in to_markdown(obj): | |
print(line, file=outp) | |
else: | |
for line in to_markdown(obj): | |
print(line) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment