Skip to content

Instantly share code, notes, and snippets.

@quadrismegistus
Created July 22, 2021 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quadrismegistus/575dfe3e65748afd1612af1f3697ed9a to your computer and use it in GitHub Desktop.
Save quadrismegistus/575dfe3e65748afd1612af1f3697ed9a to your computer and use it in GitHub Desktop.
Convert jupyter to markdown (github compatible)
#!/usr/bin/env python3
import sys,os,bs4
def nb2py(fn):
if not os.path.exists(fn): return
os.system(f'jupyter nbconvert --to markdown {fn}')
fn_md=os.path.splitext(fn)[0]+'.md'
if not os.path.exists(fn_md): return
with open(fn_md) as f: txt=f.read()
dom=bs4.BeautifulSoup(txt,'lxml')
for x in dom('style'): x.extract()
ostr=str(dom).split('<body>')[-1].split('</body>')[0]
with open(fn_md,'w') as of: of.write(ostr)
print('>> saved:',fn_md)
if __name__=='__main__':
if len(sys.argv[1])>1:
fn=sys.argv[1]
nb2py(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment