!jupyter nbconvert --execute --inplace execute_*.ipynb
import json
import nbformat
import datetime
from nbconvert.preprocessors import ExecutePreprocessor
# Load configuration file and today's date
today = str(datetime.date.today())
with open('config.json', 'r') as jfile:
config = json.load(jfile)
# Execute notebooks if they're scheduled to run today
for notebook, args in config.items():
scheduled_date = args['run_on']
if scheduled_date <= today:
print(f'Executing notebook:')
print(f' [+] {notebook}')
print(f'Arguments:')
print(f' [+] {args}')
# Execute notebook via nbconvert API
with open(notebook) as ntbook_a:
nb = nbformat.read(ntbook_a, as_version=4)
ep = ExecutePreprocessor()
ep.preprocess(nb)
# Dump configuration file with next run
next_run = datetime.date.today() + datetime.timedelta(days=args['run_every_days'])
config['execute_a.ipynb']['run_on'] = str(next_run)
with open('config.json', 'w') as jfile:
json.dump(config, jfile, indent=4)
print(f'Next scheduled run:')
print(f' [+] {next_run}')
print('-' * 52, end='\n\n')
else:
print(f'Notebook {notebook} scheduled for {scheduled_date}')