diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 47fba416fb..f61fe1db59 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -41,6 +41,8 @@ def _props(self): def run(self): from pathlib import Path + from dvc.config import Config, to_bool + if self.args.show_vega: if not self.args.targets: logger.error("please specify a target for `--show-vega`") @@ -94,8 +96,12 @@ def run(self): ) ui.write(index_path.as_uri()) - - if self.args.open: + auto_open = to_bool( + Config(validate=False) + .get("plots", {}) + .get("auto_open", "false") + ) + if self.args.open or auto_open: return ui.open_browser(index_path) return 0 diff --git a/dvc/config_schema.py b/dvc/config_schema.py index 24bb165ee3..723fec3dae 100644 --- a/dvc/config_schema.py +++ b/dvc/config_schema.py @@ -256,7 +256,10 @@ class RelPath(str): # enabled by default. It's of no use, kept for backward compatibility. Optional("parametrization", default=True): Bool, }, - "plots": {"html_template": str}, + "plots": { + "html_template": str, + Optional("auto_open", default=False): Bool, + }, "exp": { "code": str, "data": str, diff --git a/tests/unit/command/test_plots.py b/tests/unit/command/test_plots.py index 8950f36f80..c581af57f1 100644 --- a/tests/unit/command/test_plots.py +++ b/tests/unit/command/test_plots.py @@ -138,11 +138,19 @@ def test_plots_diff_vega(dvc, mocker, capsys, plots_data): render_mock.assert_not_called() -def test_plots_diff_open(tmp_dir, dvc, mocker, capsys, plots_data): +@pytest.mark.parametrize("auto_open", [True, False]) +def test_plots_diff_open(tmp_dir, dvc, mocker, capsys, plots_data, auto_open): mocked_open = mocker.patch("webbrowser.open", return_value=True) - cli_args = parse_args( - ["plots", "diff", "--targets", "plots.csv", "--open"] - ) + + args = ["plots", "diff", "--targets", "plots.csv"] + + if auto_open: + with dvc.config.edit() as conf: + conf["plots"]["auto_open"] = True + else: + args.append("--open") + + cli_args = parse_args(args) cmd = cli_args.func(cli_args) mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)