Skip to content

Commit

Permalink
config: Add plots.auto_open.
Browse files Browse the repository at this point in the history
Option for enabling automatically open webbrowser with generated html.
Defaults False.

Closes #6091
  • Loading branch information
daavoo committed Dec 15, 2021
1 parent 30d6839 commit b19d7e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 8 additions & 2 deletions dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion dvc/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/command/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b19d7e9

Please sign in to comment.