Skip to content

Instantly share code, notes, and snippets.

@Ddedalus
Last active March 30, 2020 21:37
Show Gist options
  • Save Ddedalus/40aff31106250efcb880b2830fed96b6 to your computer and use it in GitHub Desktop.
Save Ddedalus/40aff31106250efcb880b2830fed96b6 to your computer and use it in GitHub Desktop.
Jupyterify a VS Code interactive python file
# usage: jupyterify in_file.py out_file.py
import re
import sys
try:
in_path = sys.argv[1]
out_path = sys.argv[2]
except:
print('Please pass input and output path!')
with open(in_path, 'r') as inf:
code = inf.readlines()
re_block = r'# ?%% ([^[].*)'
markdown_block = '# %% [markdown]\n"""\n{}\n"""\n# %%'
def repl(m):
return markdown_block.format(m.group(1).capitalize())
with open(out_path, 'w') as opt:
for line in code:
l = re.sub(re_block, repl, line)
opt.write(l)
print('Coverted!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment