Skip to content

Instantly share code, notes, and snippets.

@mw3i
Last active March 9, 2023 13:49
Show Gist options
  • Save mw3i/1778e4d89340dde613689bd8a20baf00 to your computer and use it in GitHub Desktop.
Save mw3i/1778e4d89340dde613689bd8a20baf00 to your computer and use it in GitHub Desktop.
Hastly thrown together way of executing all python codeblocks in a markdown file
#!/usr/local/bin/python3
'''
This site is very useful for regex testing: https://pythex.org/
'''
import re, argparse
args = argparse.ArgumentParser(description = 'Execute all the blocks in a markdown script')
args.add_argument('file_path', help = 'File you want to execute')
args = args.parse_args()
with open(args.file_path, 'r') as file:
data = re.findall(r"(\`{3}[\w|\W]+?\`{3})", file.read())
for block in data:
block = re.sub(r"(?m)^\```(.*)$", "", block)
exec(block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment