3131from argparse import ArgumentParser
3232
3333from rich .console import Console
34+ from dataclasses import dataclass
3435import logging
3536from pathlib import Path
3637from tempfile import NamedTemporaryFile
@@ -99,7 +100,13 @@ def actual_run(notebook_path: Path, i: int, total: int) -> None:
99100 )
100101
101102
102- class NotebookFailure (TypedDict ):
103+ @dataclass
104+ class NotebookSuccess :
105+ notebook_path : Path
106+
107+
108+ @dataclass
109+ class NotebookFailure :
103110 notebook_path : Path
104111 error : str
105112
@@ -109,19 +116,17 @@ def run_notebook(
109116 i : int ,
110117 total : int ,
111118 mock : bool = True ,
112- ) -> NotebookFailure | None :
119+ ) -> NotebookFailure | NotebookSuccess :
113120 logging .info (f"Running notebook: { notebook_path .name } " )
114121 run = mock_run if mock else actual_run
115122
116123 try :
117124 run (notebook_path , i = i , total = total )
118125 except Exception as e :
119- logging .error (
120- f"{ e .__class__ .__name__ } encountered running notebook: { str (notebook_path )} "
121- )
126+ logging .error (f"{ e .__class__ .__name__ } encountered running notebook: { str (notebook_path )} " )
122127 return NotebookFailure (notebook_path = notebook_path , error = str (e ))
123128 else :
124- return
129+ return NotebookSuccess ( notebook_path = notebook_path )
125130
126131
127132class RunParams (TypedDict ):
@@ -133,35 +138,35 @@ class RunParams(TypedDict):
133138
134139def run_parameters (notebook_paths : list [Path ], mock : bool = True ) -> list [RunParams ]:
135140 def to_mock (notebook_path : Path , i : int ) -> RunParams :
136- return RunParams (
137- notebook_path = notebook_path , mock = mock , i = i , total = len (notebook_paths )
138- )
141+ return RunParams (notebook_path = notebook_path , mock = mock , i = i , total = len (notebook_paths ))
139142
140- return [
141- to_mock (notebook_path , i = i )
142- for i , notebook_path in enumerate (notebook_paths , start = 1 )
143- ]
143+ return [to_mock (notebook_path , i = i ) for i , notebook_path in enumerate (notebook_paths , start = 1 )]
144144
145145
146146def main (notebooks_to_run : list [Path ], mock : bool = True ) -> None :
147147 console = Console ()
148- errors : list [NotebookFailure ]
149148 setup_logging ()
150149 logging .info ("Starting notebook runner" )
151150 logging .info (f"Running { len (notebooks_to_run )} notebook(s)." )
152151 results = Parallel (n_jobs = - 1 )(
153152 delayed (run_notebook )(** run_params )
154153 for run_params in run_parameters (notebooks_to_run , mock = mock )
155154 )
156- errors = [result for result in results if result is not None ]
155+ errors : list [NotebookFailure ] = list (filter (lambda x : isinstance (x , NotebookFailure ), results ))
156+ successes : list [NotebookSuccess ] = list (
157+ filter (lambda x : isinstance (x , NotebookSuccess ), results )
158+ )
157159
158160 if not errors :
159- logging .info ("Notebooks run successfully!" )
161+ logging .info ("All notebooks ran successfully!" )
160162 return
161163
162164 for error in errors :
163- console .rule (f"[bold red]Error running { error ['notebook_path' ]} [/bold red]" )
164- console .print (error ["error" ])
165+ console .rule (f"[bold red]Error running { error .notebook_path } [/bold red]" )
166+ console .print (error .error )
167+
168+ for success in successes :
169+ console .print (f"[bold green]Success running { success .notebook_path } [/bold green]" )
165170
166171 logging .error (f"{ len (errors )} / { len (notebooks_to_run )} notebooks failed" )
167172
0 commit comments