@@ -54,6 +54,41 @@ def as_markdown(self):
5454 retmd = f'{{{ anchor_link_target } }} \n '
5555 return retmd
5656
57+ class InteractiveFigure ():
58+ def __init__ (self , paths , graph_obj ):
59+ self .paths = paths
60+ self .graph_obj = graph_obj
61+
62+ @property
63+ def as_markdown (self ):
64+ # Prepare the anchor
65+ anchor_link_target = f'#{ self .paths [0 ].stem } '
66+ # Prepare the link to the actual figures
67+ plot = self .graph_obj .to_html (include_plotlyjs = 'cdn' , full_html = False )
68+ plot = ' ' .join (plot .split ())
69+ links = ' ' .join (_generate_markdown_link (path ) for path in self .paths ) + ' '
70+ links += _generate_markdown_link (anchor_link_target , "#" )
71+ # https://stackoverflow.com/questions/14051715/markdown-native-text-alignment#comment60291976_19938870
72+ retmd = f'{ plot } \n <p style="text-align: center;">{ links } </p>\n '
73+ return retmd
74+
75+ def saveinteractivefig (fig , * , paths , output , suffix = '' ):
76+ """Save a plotly figure as html for interactive plots
77+ """
78+ outpaths = []
79+ for path in paths :
80+ if suffix :
81+ suffix = normalize_name (suffix )
82+ path = path .with_name ('_' .join ((path .stem , suffix )) + path .suffix )
83+ fig .write_html (path , include_plotlyjs = 'cdn' )
84+ outpaths .append (path .relative_to (output ))
85+ return InteractiveFigure (outpaths , fig )
86+
87+
88+ def prepare_interactive_paths (* , spec , namespace , environment , ** kwargs ):
89+ paths = environment .get_interactive_figure_paths (spec_to_nice_name (namespace , spec ))
90+ return {'paths' : list (paths ), 'output' : environment .output_path }
91+
5792
5893def prepare_paths (* ,spec , namespace , environment ,** kwargs ):
5994 paths = environment .get_figure_paths (spec_to_nice_name (namespace , spec ))
@@ -108,6 +143,11 @@ def savefiglist(figures, paths, output):
108143 res .append ("</div>" )
109144 return res
110145
146+ @add_highlight
147+ def interactive_figure (f ):
148+ f .prepare = prepare_interactive_paths
149+ f .final_action = saveinteractivefig
150+ return f
111151
112152# note: @add_highlight makes figure and figuregen be decorators
113153
0 commit comments