Last active
March 30, 2020 21:37
-
-
Save Ddedalus/40aff31106250efcb880b2830fed96b6 to your computer and use it in GitHub Desktop.
Jupyterify a VS Code interactive python file
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
# 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