Skip to content

Commit

Permalink
env: Add DVCLIVE_OPEN
Browse files Browse the repository at this point in the history
Use in `live` for optionally disable opening a web
browser on HTML generation background task.

Defaults True.

Closes iterative/dvclive#201
  • Loading branch information
daavoo committed Dec 17, 2021
1 parent 2e8f1a5 commit 138c2a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions dvc/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions dvc/repo/live.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand All @@ -20,7 +22,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 +32,9 @@ def create_summary(out):
index_path = render(
out.repo, renderers, metrics=metrics, path=html_path, refresh_seconds=5
)

webbrowser_open(index_path)
auto_open = out.repo.config["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]:
Expand Down
12 changes: 10 additions & 2 deletions tests/func/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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):
Expand Down

0 comments on commit 138c2a8

Please sign in to comment.