!jupyter nbconvert --execute --inplace execute_*.ipynb
[NbConvertApp] Converting notebook execute_a.ipynb to notebook
[NbConvertApp] Executing notebook with kernel:
[NbConvertApp] Writing 17626 bytes to execute_a.ipynb
[NbConvertApp] Converting notebook execute_b.ipynb to notebook
[NbConvertApp] Executing notebook with kernel:
[NbConvertApp] Writing 34666 bytes to execute_b.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}')
Executing notebook:
[+] execute_a.ipynb
Arguments:
[+] {'run_on': '2022-05-09', 'run_every_days': 2}
Next scheduled run:
[+] 2022-05-11
----------------------------------------------------
Executing notebook:
[+] execute_b.ipynb
Arguments:
[+] {'run_on': '2021-07-01', 'run_every_days': 1}
Next scheduled run:
[+] 2022-05-10
----------------------------------------------------