Skip to content

Instantly share code, notes, and snippets.

@Miking98
Created September 14, 2022 10:31
Show Gist options
  • Save Miking98/cfa24654aff6422694e44c39dace028f to your computer and use it in GitHub Desktop.
Save Miking98/cfa24654aff6422694e44c39dace028f to your computer and use it in GitHub Desktop.
Convert Jupyter notebook -> Markdown file for publishing to Jekyll blog
import os
import re
import subprocess
import argparse
from bs4 import BeautifulSoup
parser = argparse.ArgumentParser()
parser.add_argument('ipynb_file_name', type=str)
args = parser.parse_args()
markdown_file_name: str = args.ipynb_file_name.replace('.ipynb', '.md')
print(f"Converting {args.ipynb_file_name} => {markdown_file_name}")
subprocess.run(["jupyter", "nbconvert", "--to", "markdown", args.ipynb_file_name])
# Clean up markdown
with open(markdown_file_name, 'r') as fd:
md = fd.read()
md_clean = md
# HTML cleanup
# Remove <style> tags
md_clean = re.sub(r'\<style scoped\>(.|\n)*\<\/style\>','', md_clean, flags=re.IGNORECASE)
# Remove <axessubplot> tags
md_clean = re.sub(r'\<\/?axessubplot:.*\n','', md_clean, flags=re.IGNORECASE)
with open(markdown_file_name, 'w') as fd:
fd.write(md_clean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment