1
1
import base64
2
2
import json
3
3
import os
4
- from .card import MetaflowCard , MetaflowCardComponent
4
+ from .card import MetaflowCard , MetaflowCardComponent , with_default_component_id
5
5
from .convert_to_native_type import TaskToDict
6
6
import uuid
7
+ import inspect
7
8
8
9
ABS_DIR_PATH = os .path .dirname (os .path .abspath (__file__ ))
9
10
RENDER_TEMPLATE_PATH = os .path .join (ABS_DIR_PATH , "base.html" )
@@ -236,9 +237,28 @@ def __init__(self, data=None):
236
237
super ().__init__ (title = None , subtitle = None )
237
238
self ._data = data
238
239
240
+ @with_default_component_id
239
241
def render (self ):
240
242
datadict = super ().render ()
241
243
datadict ["data" ] = self ._data
244
+ if self .component_id is not None :
245
+ datadict ["id" ] = self .component_id
246
+ return datadict
247
+
248
+
249
+ class PythonCodeComponent (DefaultComponent ):
250
+
251
+ type = "pythonCode"
252
+
253
+ def __init__ (self , data = None ):
254
+ super ().__init__ (title = None , subtitle = None )
255
+ self ._data = data
256
+
257
+ def render (self ):
258
+ datadict = super ().render ()
259
+ datadict ["data" ] = self ._data
260
+ if self .component_id is not None :
261
+ datadict ["id" ] = self .component_id
242
262
return datadict
243
263
244
264
@@ -343,6 +363,7 @@ def __init__(
343
363
graph = None ,
344
364
components = [],
345
365
runtime = False ,
366
+ flow = None ,
346
367
):
347
368
self ._task = task
348
369
self ._only_repr = only_repr
@@ -352,6 +373,7 @@ def __init__(
352
373
self .final_component = None
353
374
self .page_component = None
354
375
self .runtime = runtime
376
+ self .flow = flow
355
377
356
378
def render (self ):
357
379
"""
@@ -475,6 +497,16 @@ def render(self):
475
497
contents = [param_component ],
476
498
).render ()
477
499
500
+ step_func = getattr (self .flow , self ._task .parent .id )
501
+ code_table = SectionComponent (
502
+ title = "Task Code" ,
503
+ contents = [
504
+ TableComponent (
505
+ data = [[PythonCodeComponent (inspect .getsource (step_func )).render ()]]
506
+ )
507
+ ],
508
+ ).render ()
509
+
478
510
# Don't include parameter ids + "name" in the task artifacts
479
511
artifactlist = [
480
512
task_data_dict ["data" ][k ]
@@ -500,6 +532,7 @@ def render(self):
500
532
page_contents .extend (
501
533
[
502
534
metadata_table ,
535
+ code_table ,
503
536
parameter_table ,
504
537
artifact_section ,
505
538
]
@@ -546,7 +579,7 @@ class ErrorCard(MetaflowCard):
546
579
547
580
RELOAD_POLICY = MetaflowCard .RELOAD_POLICY_ONCHANGE
548
581
549
- def __init__ (self , options = {}, components = [], graph = None ):
582
+ def __init__ (self , options = {}, components = [], graph = None , ** kwargs ):
550
583
self ._only_repr = True
551
584
self ._graph = None if graph is None else transform_flow_graph (graph )
552
585
self ._components = components
@@ -602,9 +635,17 @@ class DefaultCardJSON(MetaflowCard):
602
635
603
636
type = "default_json"
604
637
605
- def __init__ (self , options = dict (only_repr = True ), components = [], graph = None ):
638
+ def __init__ (
639
+ self ,
640
+ options = dict (only_repr = True ),
641
+ components = [],
642
+ graph = None ,
643
+ flow = None ,
644
+ ** kwargs
645
+ ):
606
646
self ._only_repr = True
607
647
self ._graph = None if graph is None else transform_flow_graph (graph )
648
+ self ._flow = flow
608
649
if "only_repr" in options :
609
650
self ._only_repr = options ["only_repr" ]
610
651
self ._components = components
@@ -615,6 +656,7 @@ def render(self, task):
615
656
only_repr = self ._only_repr ,
616
657
graph = self ._graph ,
617
658
components = self ._components ,
659
+ flow = self ._flow ,
618
660
).render ()
619
661
return json .dumps (final_component_dict )
620
662
@@ -629,9 +671,17 @@ class DefaultCard(MetaflowCard):
629
671
630
672
type = "default"
631
673
632
- def __init__ (self , options = dict (only_repr = True ), components = [], graph = None ):
674
+ def __init__ (
675
+ self ,
676
+ options = dict (only_repr = True ),
677
+ components = [],
678
+ graph = None ,
679
+ flow = None ,
680
+ ** kwargs
681
+ ):
633
682
self ._only_repr = True
634
683
self ._graph = None if graph is None else transform_flow_graph (graph )
684
+ self ._flow = flow
635
685
if "only_repr" in options :
636
686
self ._only_repr = options ["only_repr" ]
637
687
self ._components = components
@@ -646,6 +696,7 @@ def render(self, task, runtime=False):
646
696
graph = self ._graph ,
647
697
components = self ._components ,
648
698
runtime = runtime ,
699
+ flow = self ._flow ,
649
700
).render ()
650
701
pt = self ._get_mustache ()
651
702
data_dict = dict (
@@ -688,7 +739,7 @@ class BlankCard(MetaflowCard):
688
739
689
740
type = "blank"
690
741
691
- def __init__ (self , options = dict (title = "" ), components = [], graph = None ):
742
+ def __init__ (self , options = dict (title = "" ), components = [], graph = None , ** kwargs ):
692
743
self ._graph = None if graph is None else transform_flow_graph (graph )
693
744
self ._title = ""
694
745
if "title" in options :
0 commit comments