Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone plotly.min.js for graph (fix #189) #209

Merged
merged 15 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions docs/source/commands/graph.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -56,7 +56,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
Expand Down
27 changes: 26 additions & 1 deletion src/wily/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,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
"""
)
)
Expand All @@ -422,14 +426,34 @@ def diff(ctx, files, metrics, all, detail, revision, wrap):
default=False,
help=_("Aggregate if path is directory"),
)
@click.option(
"--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):
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)

# 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

logger.debug("Running report on %s for metrics %s", path, metrics)
Expand All @@ -441,6 +465,7 @@ def graph(ctx, path, metrics, output, x_axis, changes, aggregate):
x_axis=x_axis,
changes=changes,
aggregate=aggregate,
plotlyjs=plotlyjs,
)


Expand Down
5 changes: 4 additions & 1 deletion src/wily/commands/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,6 +38,7 @@ def graph(
changes: bool = True,
text: bool = False,
aggregate: bool = False,
plotlyjs: Union[bool, str] = True,
) -> None:
"""
Graph information about the cache and runtime.
Expand All @@ -50,6 +51,7 @@ def graph(
:param changes: Only graph changes.
:param text: Show commit message inline in graph.
:param aggregate: Aggregate values for graph.
:param plotlyjs: How to include plotly.min.js.
"""
logger.debug("Running graph command")

Expand Down Expand Up @@ -169,4 +171,5 @@ def graph(
},
auto_open=auto_open,
filename=filename,
include_plotlyjs=plotlyjs, # type: ignore
)
30 changes: 30 additions & 0 deletions test/integration/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ def test_graph(builddir):
assert result.exit_code == 0, result.stdout


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(
main.cli,
[
"--path",
builddir,
"graph",
_path,
"-m",
"raw.loc",
"--shared-js",
],
)
assert result.exit_code == 0, result.stdout


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", " --cdn_js"],
)
assert result.exit_code == 0, result.stdout


def test_graph_all(builddir):
"""Test the graph feature"""
runner = CliRunner()
Expand Down
Loading