From 07c80ddddc421d9449bace6aa0b23de657ff4937 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Fri, 7 Jul 2023 21:07:53 -0300 Subject: [PATCH 1/9] Allow standalone plotly.min.js when creating graphs. --- src/wily/__main__.py | 13 ++++++++++++- src/wily/commands/graph.py | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/wily/__main__.py b/src/wily/__main__.py index 6f088501..eaa058ad 100644 --- a/src/wily/__main__.py +++ b/src/wily/__main__.py @@ -375,14 +375,24 @@ def diff(ctx, files, metrics, all, detail, revision): default=False, help=_("Aggregate if path is directory"), ) +@click.option( + "--plotlyjs", + default="True", + help=_("Embed plotlyjs or put it in the graph directory."), +) @click.pass_context -def graph(ctx, path, metrics, output, x_axis, changes, aggregate): +def graph(ctx, path, metrics, output, x_axis, changes, aggregate, plotlyjs): """Output report to specified HTML path, e.g. reports/out.html.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) + if plotlyjs == "True": + plotlyjs = True + elif plotlyjs == "False": + plotlyjs = False + from wily.commands.graph import graph logger.debug(f"Running report on {path} for metrics {metrics}") @@ -394,6 +404,7 @@ def graph(ctx, path, metrics, output, x_axis, changes, aggregate): x_axis=x_axis, changes=changes, aggregate=aggregate, + plotlyjs=plotlyjs, ) diff --git a/src/wily/commands/graph.py b/src/wily/commands/graph.py index fa10fe16..464e65df 100644 --- a/src/wily/commands/graph.py +++ b/src/wily/commands/graph.py @@ -27,6 +27,7 @@ def graph( changes=True, text=False, aggregate=False, + plotlyjs=False, ): """ Graph information about the cache and runtime. @@ -149,4 +150,5 @@ def graph( }, auto_open=auto_open, filename=filename, + include_plotlyjs=plotlyjs, ) From d014097fe321a80457a89281b297c0c92ad32651 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 16 Aug 2023 10:53:13 -0300 Subject: [PATCH 2/9] Add an integration test that the --plotlyjs option works. --- test/integration/test_graph.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/integration/test_graph.py b/test/integration/test_graph.py index d6743afa..a069553a 100644 --- a/test/integration/test_graph.py +++ b/test/integration/test_graph.py @@ -37,6 +37,17 @@ def test_graph(builddir): assert result.exit_code == 0, result.stdout +def test_graph_plotlyjs(builddir): + """Test the graph feature with plotlyjs option""" + runner = CliRunner() + with patch.dict("os.environ", values=PATCHED_ENV, clear=True): + result = runner.invoke( + main.cli, + ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "directory"], + ) + assert result.exit_code == 0, result.stdout + + def test_graph_all(builddir): """Test the graph feature""" runner = CliRunner() From ff8dce3fbf073f69f58cd5691f97df78d801138b Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 16 Aug 2023 11:00:40 -0300 Subject: [PATCH 3/9] plotlyjs should default to True, as in plotly.io.to_html(). --- src/wily/commands/graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wily/commands/graph.py b/src/wily/commands/graph.py index e1847f20..47b00ba1 100644 --- a/src/wily/commands/graph.py +++ b/src/wily/commands/graph.py @@ -31,7 +31,7 @@ def graph( changes: bool = True, text: bool = False, aggregate: bool = False, - plotlyjs: Union[bool, str] = False, + plotlyjs: Union[bool, str] = True, ) -> None: """ Graph information about the cache and runtime. From a7bcfa93a851d04577c39c8f65b76d5092e5cba2 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 23 Aug 2023 22:40:35 -0300 Subject: [PATCH 4/9] Add more tests for standalone plotlyjs to increase coverage, ignore pyright error. --- src/wily/commands/graph.py | 2 +- test/integration/test_graph.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/wily/commands/graph.py b/src/wily/commands/graph.py index 47b00ba1..1b1b70d6 100644 --- a/src/wily/commands/graph.py +++ b/src/wily/commands/graph.py @@ -157,4 +157,4 @@ def graph( auto_open=auto_open, filename=filename, include_plotlyjs=plotlyjs, - ) + ) # type: ignore diff --git a/test/integration/test_graph.py b/test/integration/test_graph.py index a069553a..13d7af53 100644 --- a/test/integration/test_graph.py +++ b/test/integration/test_graph.py @@ -37,7 +37,7 @@ def test_graph(builddir): assert result.exit_code == 0, result.stdout -def test_graph_plotlyjs(builddir): +def test_graph_plotlyjs_directory(builddir): """Test the graph feature with plotlyjs option""" runner = CliRunner() with patch.dict("os.environ", values=PATCHED_ENV, clear=True): @@ -48,6 +48,28 @@ def test_graph_plotlyjs(builddir): assert result.exit_code == 0, result.stdout +def test_graph_plotlyjs_True(builddir): + """Test the graph feature with plotlyjs option""" + runner = CliRunner() + with patch.dict("os.environ", values=PATCHED_ENV, clear=True): + result = runner.invoke( + main.cli, + ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "True"], + ) + assert result.exit_code == 0, result.stdout + + +def test_graph_plotlyjs_False(builddir): + """Test the graph feature with plotlyjs option""" + runner = CliRunner() + with patch.dict("os.environ", values=PATCHED_ENV, clear=True): + result = runner.invoke( + main.cli, + ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "False"], + ) + assert result.exit_code == 0, result.stdout + + def test_graph_all(builddir): """Test the graph feature""" runner = CliRunner() From e0498eaa2df0b5a8e8324ca5602ebdd30c383589 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 23 Aug 2023 23:02:14 -0300 Subject: [PATCH 5/9] Correctly ignore pyright typing error. --- src/wily/commands/graph.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wily/commands/graph.py b/src/wily/commands/graph.py index 1b1b70d6..095b2ccf 100644 --- a/src/wily/commands/graph.py +++ b/src/wily/commands/graph.py @@ -156,5 +156,5 @@ def graph( }, auto_open=auto_open, filename=filename, - include_plotlyjs=plotlyjs, - ) # type: ignore + include_plotlyjs=plotlyjs, # type: ignore + ) From 93db5c77e20312a3b4e4e49d9e2ca58b6a998c22 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sat, 26 Aug 2023 20:26:56 -0300 Subject: [PATCH 6/9] Fix tests. --- src/wily/commands/graph.py | 2 +- test/integration/test_graph.py | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/wily/commands/graph.py b/src/wily/commands/graph.py index 55aa5c5f..5c94f4dd 100644 --- a/src/wily/commands/graph.py +++ b/src/wily/commands/graph.py @@ -5,7 +5,7 @@ """ from pathlib import Path -from typing import Optional, Tuple +from typing import Optional, Tuple, Union import plotly.graph_objs as go import plotly.offline diff --git a/test/integration/test_graph.py b/test/integration/test_graph.py index f2c25fa0..df8c2dac 100644 --- a/test/integration/test_graph.py +++ b/test/integration/test_graph.py @@ -51,7 +51,16 @@ def test_graph_plotlyjs_directory(builddir): with patch.dict("os.environ", values=PATCHED_ENV, clear=True): result = runner.invoke( main.cli, - ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "directory"], + [ + "--path", + builddir, + "graph", + _path, + "-m", + "raw.loc", + "--plotlyjs", + "directory", + ], ) assert result.exit_code == 0, result.stdout @@ -62,7 +71,7 @@ def test_graph_plotlyjs_True(builddir): with patch.dict("os.environ", values=PATCHED_ENV, clear=True): result = runner.invoke( main.cli, - ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "True"], + ["--path", builddir, "graph", _path, "-m", "raw.loc", "--plotlyjs", "True"], ) assert result.exit_code == 0, result.stdout @@ -73,7 +82,16 @@ def test_graph_plotlyjs_False(builddir): with patch.dict("os.environ", values=PATCHED_ENV, clear=True): result = runner.invoke( main.cli, - ["--path", builddir, "graph", _path, "raw.loc", "--plotlyjs", "False"], + [ + "--path", + builddir, + "graph", + _path, + "-m", + "raw.loc", + "--plotlyjs", + "False", + ], ) assert result.exit_code == 0, result.stdout From d10ad009bf8c5a1080f52077a20d6a493dac8ca3 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:24:01 -0300 Subject: [PATCH 7/9] Add --shared-js and --cdn-js to graph(). --- src/wily/__main__.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/wily/__main__.py b/src/wily/__main__.py index ad0f41b8..7a37872b 100644 --- a/src/wily/__main__.py +++ b/src/wily/__main__.py @@ -395,6 +395,10 @@ def diff(ctx, files, metrics, all, detail, revision, wrap): Graph test.py against raw.loc and raw.sloc on the x-axis $ wily graph src/test.py -m raw.loc --x-axis raw.sloc + + Graph test.py against raw.loc creating a standalone plotly.min.js file + + $ wily graph src/test.py -m raw.loc --shared-js """ ) ) @@ -421,22 +425,32 @@ def diff(ctx, files, metrics, all, detail, revision, wrap): help=_("Aggregate if path is directory"), ) @click.option( - "--plotlyjs", - default="True", - help=_("Embed plotlyjs or put it in the graph directory."), + "--shared-js/--no-shared-js", + default=False, + type=click.BOOL, + help=_("Create standalone plotly.min.js in the graph directory."), +) +@click.option( + "--cdn-js/--no-cdn-js", + default=False, + type=click.BOOL, + help=_("Point to a CDN hosted plotly.min.js."), ) @click.pass_context -def graph(ctx, path, metrics, output, x_axis, changes, aggregate, plotlyjs): +def graph(ctx, path, metrics, output, x_axis, changes, aggregate, shared_js, cdn_js): """Output report to specified HTML path, e.g. reports/out.html.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) - if plotlyjs == "True": - plotlyjs = True - elif plotlyjs == "False": - plotlyjs = False + # Embed plotly.min.js in the HTML file by default + plotlyjs = True + if shared_js: + plotlyjs = "directory" + # CDN takes precedence over directory + if cdn_js: + plotlyjs = "cdn" from wily.commands.graph import graph From ce2f8a889954bb252363fad09a89d3bc1caa30bd Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 30 Aug 2023 06:51:40 -0300 Subject: [PATCH 8/9] Update graph() tests. --- test/integration/test_graph.py | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/test/integration/test_graph.py b/test/integration/test_graph.py index df8c2dac..78b576aa 100644 --- a/test/integration/test_graph.py +++ b/test/integration/test_graph.py @@ -45,8 +45,8 @@ def test_graph(builddir): assert result.exit_code == 0, result.stdout -def test_graph_plotlyjs_directory(builddir): - """Test the graph feature with plotlyjs option""" +def test_graph_shared_js(builddir): + """Test the graph feature with --shared-js option""" runner = CliRunner() with patch.dict("os.environ", values=PATCHED_ENV, clear=True): result = runner.invoke( @@ -58,40 +58,19 @@ def test_graph_plotlyjs_directory(builddir): _path, "-m", "raw.loc", - "--plotlyjs", - "directory", + "--shared-js", ], ) assert result.exit_code == 0, result.stdout -def test_graph_plotlyjs_True(builddir): - """Test the graph feature with plotlyjs option""" +def test_graph_plotlyjs_cdn_js(builddir): + """Test the graph feature with --cdn_js option""" runner = CliRunner() with patch.dict("os.environ", values=PATCHED_ENV, clear=True): result = runner.invoke( main.cli, - ["--path", builddir, "graph", _path, "-m", "raw.loc", "--plotlyjs", "True"], - ) - assert result.exit_code == 0, result.stdout - - -def test_graph_plotlyjs_False(builddir): - """Test the graph feature with plotlyjs option""" - runner = CliRunner() - with patch.dict("os.environ", values=PATCHED_ENV, clear=True): - result = runner.invoke( - main.cli, - [ - "--path", - builddir, - "graph", - _path, - "-m", - "raw.loc", - "--plotlyjs", - "False", - ], + ["--path", builddir, "graph", _path, "-m", "raw.loc", " --cdn_js"], ) assert result.exit_code == 0, result.stdout From d1bef09a9c4df4061dc270ec61f7da585285637f Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:02:47 -0300 Subject: [PATCH 9/9] Add the JS options to graph docs, update the docs to the new metrics format. --- docs/source/commands/graph.rst | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/source/commands/graph.rst b/docs/source/commands/graph.rst index 18c7c22b..60bc4ea0 100644 --- a/docs/source/commands/graph.rst +++ b/docs/source/commands/graph.rst @@ -6,11 +6,11 @@ The graph command generates HTML graphs for metrics, trends and data in the wily Examples -------- -``wily graph`` will take 1 or 2 metrics as the 2nd and 3rd arguments. The first metric will be the Y-axis and the 3rd metric (if provided) will control the size of the bubble. +``wily graph`` will take 1 or 2 comma-separated metrics as the -m option. The first metric will be the Y-axis and the 2nd metric (if provided) will control the size of the bubble. .. code-block:: none - $ wily graph example.py loc + $ wily graph example.py -m loc .. image:: ../_static/single_metric_graph.png :align: center @@ -19,7 +19,7 @@ You can provide a second metric which will be used to control the size of the bu .. code-block:: none - $ wily graph example.py loc complexity + $ wily graph example.py loc,complexity .. image:: ../_static/two_metric_graph.png :align: center @@ -28,7 +28,7 @@ The x-axis will be the historic revisions (typically git commits) on a scale of .. code-block:: none - $ wily graph example.py loc complexity --x-axis sloc + $ wily graph example.py -m loc,complexity --x-axis sloc .. image:: ../_static/custom_x_axis_graph.png :align: center @@ -38,7 +38,15 @@ To save the output to a specific HTML file and not open it, provide the ``-o`` f .. code-block:: none - $ wily report example.py loc -o example.html + $ wily report example.py -m loc -o example.html + +By default, ``wily graph`` will create an HTML file containing all the JS necessary to render the graph. +To create a standalone plotly.min.js file in the same directory as the HTML file instead, pass the ``--shared-js`´ option. +To point the HTML file to a CDN hosted plotly.min.js instead, pass the ``--cdn-js`´ option. + +.. code-block:: none + + $ wily report example.py -m loc --shared=js Command Line Usage @@ -46,4 +54,4 @@ Command Line Usage .. click:: wily.__main__:graph :prog: wily - :show-nested: \ No newline at end of file + :show-nested: