@@ -284,11 +284,14 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
284284 raise FileNotFoundError (f"File { path } does not exist" )
285285
286286 with open (path , "r" ) as f :
287- dict_ = json .load (f )
287+ context_dict = json .load (f )
288288
289+ return ICortexContext .from_dict (context_dict , scope )
290+
291+ def from_dict (context_dict : t .Dict [str , t .Any ], scope : t .Dict [str , t .Any ] = None ):
289292 ret = ICortexContext (scope = scope )
290293
291- for cell_dict in dict_ ["cells" ]:
294+ for cell_dict in context_dict ["cells" ]:
292295 if cell_dict ["metadata" ]["source_type" ] == "code" :
293296 cell = CodeCell .from_dict (cell_dict )
294297 elif cell_dict ["metadata" ]["source_type" ] == "var" :
@@ -301,7 +304,7 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
301304 )
302305 ret ._cells .append (cell )
303306
304- ret ._vars = [Var .from_dict (v ) for v in dict_ ["metadata" ]["variables" ]]
307+ ret ._vars = [Var .from_dict (v ) for v in context_dict ["metadata" ]["variables" ]]
305308
306309 return ret
307310
@@ -333,33 +336,32 @@ def run(self, notebook_args: t.List[str]):
333336 # Execute the returned code
334337 exec (code , scope )
335338
336- def bake (self , dest_path : str , format = True ):
337- """Bake the notebook to a Python script """
339+ def get_code (self , argparsify = False ):
340+ """Aggregates the code for the notebook """
338341
339- # Warn if the extension is not .py
340- if not dest_path .endswith (".py" ):
341- print (
342- f"Warning: { dest_path } does not have the .py extension. "
343- "It is recommended that you use the .py extension for "
344- "frozen files."
345- )
342+ output = ""
343+ if argparsify :
344+ # scope = locals()
345+ vars = self .vars
346346
347- vars = self .vars
348- scope = locals ()
349-
350- output = "import argparse\n \n parser = argparse.ArgumentParser()\n "
351- for var in vars :
352- output += f"parser.add_argument({ var .arg !r} , type={ var ._type .__name__ } )\n "
353- output += "args = parser.parse_args()\n \n "
347+ output += "import argparse\n \n parser = argparse.ArgumentParser()\n "
348+ for var in vars :
349+ output += (
350+ f"parser.add_argument({ var .arg !r} , type={ var ._type .__name__ } )\n "
351+ )
352+ output += "args = parser.parse_args()\n \n "
354353
355354 for cell in self .iter_cells ():
356355 if cell .success :
357356 if isinstance (cell , VarCell ):
358357 var = cell .var
359358 # Change the value to that of the parsed argument
360359 # var.value = var._type(getattr(parsed_args, var.arg))
361- code = f"{ var .name } = args.{ var .arg } \n \n "
362- # code = var.get_code()
360+ if argparsify :
361+ code = f"{ var .name } = args.{ var .arg } \n \n "
362+ else :
363+ code = var .get_code ()
364+
363365 elif isinstance (cell , CodeCell ):
364366 if not is_magic (cell .get_code ()):
365367 code = cell .get_code ().rstrip () + "\n \n "
@@ -371,9 +373,25 @@ def bake(self, dest_path: str, format=True):
371373 # Execute the returned code
372374 output += code
373375
376+ return output
377+
378+ def bake (self , dest_path : str , format = True ):
379+ """Bake the notebook to a Python script"""
380+
381+ # Warn if the extension is not .py
382+ if not dest_path .endswith (".py" ):
383+ print (
384+ f"Warning: { dest_path } does not have the .py extension. "
385+ "It is recommended that you use the .py extension for "
386+ "frozen files."
387+ )
388+
389+ output = self .get_code (argparsify = True )
390+
374391 # Run black over output
375392 if format :
376393 import black
394+
377395 try :
378396 output = black .format_str (output , mode = black .FileMode ())
379397 except :
0 commit comments