From 5643bdf610c341a9e79d0e01bc2a587097f5a79f Mon Sep 17 00:00:00 2001 From: David de la Iglesia Castro Date: Wed, 15 Dec 2021 20:17:51 +0100 Subject: [PATCH] env: Add `DVCLIVE_OPEN` Use in `live` for optionally disable opening a web browser on HTML generation background task. Defaults True. Closes iterative/dvclive#201 --- dvc/env.py | 1 + dvc/repo/live.py | 10 ++++++++-- tests/func/test_live.py | 12 ++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/dvc/env.py b/dvc/env.py index 2839772c68..81885ccb3e 100644 --- a/dvc/env.py +++ b/dvc/env.py @@ -5,6 +5,7 @@ DVCLIVE_PATH = "DVCLIVE_PATH" DVCLIVE_SUMMARY = "DVCLIVE_SUMMARY" DVCLIVE_HTML = "DVCLIVE_HTML" +DVCLIVE_OPEN = "DVCLIVE_OPEN" DVCLIVE_RESUME = "DVCLIVE_RESUME" DVC_IGNORE_ISATTY = "DVC_IGNORE_ISATTY" DVC_EXP_GIT_REMOTE = "DVC_EXP_GIT_REMOTE" diff --git a/dvc/repo/live.py b/dvc/repo/live.py index 265b5c71c6..2855e806c2 100644 --- a/dvc/repo/live.py +++ b/dvc/repo/live.py @@ -1,8 +1,10 @@ import logging +import os from typing import TYPE_CHECKING, List, Optional from funcy import once_per_args +from dvc.env import DVCLIVE_OPEN from dvc.render.utils import match_renderers, render logger = logging.getLogger(__name__) @@ -20,6 +22,7 @@ def webbrowser_open(url: str) -> int: def create_summary(out): + from dvc.config import Config, to_bool assert out.live and out.live["html"] @@ -31,8 +34,11 @@ def create_summary(out): index_path = render( out.repo, renderers, metrics=metrics, path=html_path, refresh_seconds=5 ) - - webbrowser_open(index_path) + auto_open = to_bool( + Config(validate=False).get("plots", {}).get("auto_open", "false") + ) + if auto_open and int(os.environ.get(DVCLIVE_OPEN, "1")): + webbrowser_open(index_path) def summary_fs_path(out: "Output") -> Optional[str]: diff --git a/tests/func/test_live.py b/tests/func/test_live.py index 9f702f5872..ddbbe406c4 100644 --- a/tests/func/test_live.py +++ b/tests/func/test_live.py @@ -241,7 +241,15 @@ 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 + else: + mocker.patch.dict(os.environ, {"DVCLIVE_OPEN": "0"}) show_spy = mocker.spy(dvc.live, "show") webbrowser_open = mocker.patch("dvc.repo.live.webbrowser_open") @@ -265,7 +273,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):