From bb797d601c2b4a6a4a087ed9a1ad0a972ed038e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Tue, 9 Mar 2021 22:01:11 +0545 Subject: [PATCH 1/3] plots: support opening plots file directly in the browser --- dvc/command/plots.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 62ead59ad4..8deb5aff2f 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -45,13 +45,20 @@ def run(self): path = os.path.join(os.getcwd(), path) write(path, plots) - - logger.info(f"file://{path}") - + url = f"file://{path}" except DvcException: logger.exception("") return 1 + logger.info(url) + if self.args.open: + import webbrowser + + opened = webbrowser.open(url) + if not opened: + logger.error("Failed to open. Please try opening it manually.") + return 1 + return 0 @@ -229,3 +236,9 @@ def _add_output_arguments(parser): default=False, help="Show output in Vega format.", ) + parser.add_argument( + "--open", + action="store_true", + default=False, + help="Open plot file directly in the browser.", + ) From 749f52fc96be7f18087ecd3b7272a63c1f5c8799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Wed, 10 Mar 2021 08:50:24 +0545 Subject: [PATCH 2/3] add tests --- tests/unit/command/test_plots.py | 42 ++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/unit/command/test_plots.py b/tests/unit/command/test_plots.py index 219b9d543d..f3a0ad88b3 100644 --- a/tests/unit/command/test_plots.py +++ b/tests/unit/command/test_plots.py @@ -1,3 +1,5 @@ +import logging + from dvc.cli import parse_args from dvc.command.plots import CmdPlotsDiff, CmdPlotsShow @@ -55,7 +57,7 @@ def test_plots_diff(dvc, mocker): ) -def test_plots_show(dvc, mocker): +def test_plots_show_vega(dvc, mocker): cli_args = parse_args( [ "plots", @@ -85,7 +87,7 @@ def test_plots_show(dvc, mocker): ) -def test_plots_show_vega(dvc, mocker, caplog): +def test_plots_diff_vega(dvc, mocker, caplog): cli_args = parse_args( [ "plots", @@ -103,3 +105,39 @@ def test_plots_show_vega(dvc, mocker, caplog): ) assert cmd.run() == 0 assert "plothtml" in caplog.text + + +def test_plots_diff_open(tmp_dir, dvc, mocker, caplog): + mocked_open = mocker.patch("webbrowser.open", return_value=True) + cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"]) + cmd = cli_args.func(cli_args) + mocker.patch( + "dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"} + ) + + assert cmd.run() == 0 + + expected_url = f"file://{tmp_dir}/plots.html" + assert expected_url in caplog.text + + mocked_open.assert_called_once_with(expected_url) + + +def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog): + mocked_open = mocker.patch("webbrowser.open", return_value=False) + cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"]) + cmd = cli_args.func(cli_args) + mocker.patch( + "dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"} + ) + + assert cmd.run() == 1 + + expected_url = f"file://{tmp_dir}/plots.html" + mocked_open.assert_called_once_with(expected_url) + + error_message = "Failed to open. Please try opening it manually." + assert caplog.record_tuples == [ + ("dvc.command.plots", logging.INFO, expected_url), + ("dvc.command.plots", logging.ERROR, error_message), + ] From 19112e340a8df70aaa3ce6a4637d7f1f9d4b5b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Wed, 10 Mar 2021 10:56:43 +0545 Subject: [PATCH 3/3] fix path on windows tests --- tests/unit/command/test_plots.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/command/test_plots.py b/tests/unit/command/test_plots.py index f3a0ad88b3..a2f5b1cc60 100644 --- a/tests/unit/command/test_plots.py +++ b/tests/unit/command/test_plots.py @@ -117,7 +117,7 @@ def test_plots_diff_open(tmp_dir, dvc, mocker, caplog): assert cmd.run() == 0 - expected_url = f"file://{tmp_dir}/plots.html" + expected_url = f"file://{tmp_dir / 'plots.html'}" assert expected_url in caplog.text mocked_open.assert_called_once_with(expected_url) @@ -133,7 +133,7 @@ def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog): assert cmd.run() == 1 - expected_url = f"file://{tmp_dir}/plots.html" + expected_url = f"file://{tmp_dir / 'plots.html'}" mocked_open.assert_called_once_with(expected_url) error_message = "Failed to open. Please try opening it manually."