@@ -284,11 +284,14 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
284
284
raise FileNotFoundError (f"File { path } does not exist" )
285
285
286
286
with open (path , "r" ) as f :
287
- dict_ = json .load (f )
287
+ context_dict = json .load (f )
288
288
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 ):
289
292
ret = ICortexContext (scope = scope )
290
293
291
- for cell_dict in dict_ ["cells" ]:
294
+ for cell_dict in context_dict ["cells" ]:
292
295
if cell_dict ["metadata" ]["source_type" ] == "code" :
293
296
cell = CodeCell .from_dict (cell_dict )
294
297
elif cell_dict ["metadata" ]["source_type" ] == "var" :
@@ -301,7 +304,7 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
301
304
)
302
305
ret ._cells .append (cell )
303
306
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" ]]
305
308
306
309
return ret
307
310
@@ -333,33 +336,32 @@ def run(self, notebook_args: t.List[str]):
333
336
# Execute the returned code
334
337
exec (code , scope )
335
338
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 """
338
341
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
346
346
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 "
354
353
355
354
for cell in self .iter_cells ():
356
355
if cell .success :
357
356
if isinstance (cell , VarCell ):
358
357
var = cell .var
359
358
# Change the value to that of the parsed argument
360
359
# 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
+
363
365
elif isinstance (cell , CodeCell ):
364
366
if not is_magic (cell .get_code ()):
365
367
code = cell .get_code ().rstrip () + "\n \n "
@@ -371,9 +373,25 @@ def bake(self, dest_path: str, format=True):
371
373
# Execute the returned code
372
374
output += code
373
375
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
+
374
391
# Run black over output
375
392
if format :
376
393
import black
394
+
377
395
try :
378
396
output = black .format_str (output , mode = black .FileMode ())
379
397
except :
0 commit comments