Skip to content

Commit b736792

Browse files
siranipourcomane
authored andcommitted
Saving plotly figures for interactive plots
1 parent f393ee9 commit b736792

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/reportengine/environment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def init_output(self):
121121
self.table_folder = (self.output_path/'tables')
122122
self.table_folder.mkdir(exist_ok=True)
123123

124+
def get_interactive_figure_paths(self, handle):
125+
yield self.figure_folder / (handle + '.html')
126+
124127
def get_figure_paths(self, handle):
125128
for fmt in self.figure_formats:
126129
yield self.figure_folder / (handle + '.' + fmt)

src/reportengine/figure.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,41 @@ def as_markdown(self):
5454
retmd = f'![{links}]({self.paths[0]}){{{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

5893
def 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

Comments
 (0)