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

Add config option plots.auto_open #7159

Merged
merged 2 commits into from
Jan 3, 2022
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
10 changes: 8 additions & 2 deletions dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ def run(self):
)

ui.write(index_path.as_uri())

if self.args.open:
auto_open = self.repo.config["plots"].get("auto_open", False)
if self.args.open or auto_open:
if not auto_open:
ui.write(
"To enable auto opening, you can run:\n"
"\n"
"\tdvc config plots.auto_open true"
)
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
5 changes: 2 additions & 3 deletions dvc/repo/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def webbrowser_open(url: str) -> int:


def create_summary(out):

assert out.live and out.live["html"]

metrics, plots = out.repo.live.show(out.fs_path)
Expand All @@ -31,8 +30,8 @@ def create_summary(out):
index_path = render(
out.repo, renderers, metrics=metrics, path=html_path, refresh_seconds=5
)

webbrowser_open(index_path)
if out.repo.config["plots"].get("auto_open", False):
webbrowser_open(index_path)


def summary_fs_path(out: "Output") -> Optional[str]:
Expand Down
11 changes: 9 additions & 2 deletions tests/func/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ def test_live_checkpoints_resume(
assert checkpoints_metric(results, "logs.json", "metric2") == [8, 6, 4, 2]


def test_dvc_generates_html_during_run(tmp_dir, dvc, mocker, live_stage):
@pytest.mark.parametrize("auto_open", [False, True])
def test_dvc_generates_html_during_run(
tmp_dir, dvc, mocker, live_stage, auto_open
):
if auto_open:
with dvc.config.edit() as conf:
conf["plots"]["auto_open"] = True

show_spy = mocker.spy(dvc.live, "show")
webbrowser_open = mocker.patch("dvc.repo.live.webbrowser_open")

Expand All @@ -265,7 +272,7 @@ def test_dvc_generates_html_during_run(tmp_dir, dvc, mocker, live_stage):
live_stage(summary=True, live="logs", code=script)

assert show_spy.call_count == 2
assert webbrowser_open.call_count == 2
assert webbrowser_open.call_count == (2 if auto_open else 0)


def test_dvclive_stage_with_different_wdir(tmp_dir, scm, dvc):
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