From 428d1927650a2c1ee8d53397777e7b296a065b54 Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Wed, 12 Feb 2020 18:13:11 +0000 Subject: [PATCH] lisa.analysis: Add interactive html output format. Can be enabled using output='interactive_html' in plot methods, or with: lisa-plot --interactive-html --- lisa/analysis/base.py | 10 +++++++++- setup.py | 1 + tools/lisa-plot | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lisa/analysis/base.py b/lisa/analysis/base.py index b16ee6bbf..fb9c0cf0c 100644 --- a/lisa/analysis/base.py +++ b/lisa/analysis/base.py @@ -39,6 +39,7 @@ from matplotlib.figure import Figure from cycler import cycler as make_cycler import mplcursors +import mpld3 from ipywidgets import widgets from IPython.display import display @@ -539,6 +540,7 @@ class AnalysisHelpers(Loggable, abc.ABC): format_map = { 'rst': cls._get_rst_content, 'html': cls._get_html, + 'interactive_html': cls._get_interactive_html, } try: return format_map[fmt] @@ -563,7 +565,7 @@ class AnalysisHelpers(Loggable, abc.ABC): out = resolve_formatter(output)(f, args, f_kwargs, axis) if filepath: - if img_format in ('html', 'rst'): + if img_format in ('html', 'interactive_html', 'rst'): content = resolve_formatter(img_format)(f, args, f_kwargs, axis) with open(filepath, 'wt', encoding='utf-8') as fd: @@ -664,6 +666,12 @@ class AnalysisHelpers(Loggable, abc.ABC): parts = cls._docutils_render(writer='html', rst=rst, doctitle_xform=True) return parts['whole'] + @classmethod + def _get_interactive_html(cls, f, args, kwargs, axis): + fig = axis.get_figure() + html = mpld3.fig_to_html(fig, template_type='general') + return html + class TraceAnalysisBase(AnalysisHelpers): """ diff --git a/setup.py b/setup.py index 61917116a..abc7184d7 100755 --- a/setup.py +++ b/setup.py @@ -73,6 +73,7 @@ setup( "ipython", "ipywidgets", "mplcursors", + "mpld3", # Depdendencies that are shipped as part of the LISA repo as # subtree/submodule diff --git a/tools/lisa-plot b/tools/lisa-plot index 5e4f94502..340388e1b 100755 --- a/tools/lisa-plot +++ b/tools/lisa-plot @@ -239,6 +239,10 @@ Available plots: help='Platform information, necessary for some plots', ) + parser.add_argument('--interactive-html', action='store_true', + help='Use mpld3 to generate an interactive HTML file when html format is in use', + ) + parser.add_argument('--xkcd', action='store_true', help='Graphs will look like XKCD plots', ) @@ -332,6 +336,9 @@ Available plots: kwargs = make_plot_kwargs(f, file_path, extra_options=args.option) + if args.interactive_html: + kwargs['img_format'] = 'interactive_html' + with handle_plot_excep(exit_on_error=not args.best_effort): with xkcd_cm: TraceAnalysisBase.call_on_trace(f, trace, kwargs) -- GitLab