import nbformat from nbconvert.preprocessors import ExecutePreprocessor import os def run_notebook(form): path = "./script/merchant_risk/notebooks/" date_arg = form.get('date') run_file(f"{path}merchant_risk_before_adj.ipynb", date_arg) run_file(f"{path}merchant_risk_after_adj.ipynb") run_file(f"{path}consolidated_mis_files.ipynb") run_file(f"{path}portfolio_cuts.ipynb") run_file(f"{path}offbook_portfolio_cuts.ipynb") run_file(f"{path}solar_portfolio_cuts.ipynb") run_file(f"{path}static_pool_script.ipynb") def run_file(file_path, date_arg=None): # Load the notebook with open(file_path) as f: nb = nbformat.read(f, as_version=4) inject_code = f'date_arg = "{date_arg}"' code_cell = nbformat.v4.new_code_cell(source=inject_code) nb.cells.insert(0, code_cell) # Prepare the notebook for execution ep = ExecutePreprocessor(timeout=600, kernel_name="python3") # Execute the notebook ep.preprocess(nb, {'metadata': {'path': './'}})