Created
July 22, 2021 23:31
-
-
Save quadrismegistus/575dfe3e65748afd1612af1f3697ed9a to your computer and use it in GitHub Desktop.
Convert jupyter to markdown (github compatible)
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 | |
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