live: Call webbrowser.open.

Use `funcy.once_per_args` decorator to prevent opening the html multiple times if user closes first time.

Closes iterative/dvclive#180
This commit is contained in:
David de la Iglesia Castro
2021-11-22 03:35:23 +02:00
committed by Ruslan Kuprieiev
parent 1176b6d875
commit 318b269ad1
2 changed files with 20 additions and 1 deletions
+18 -1
View File
@@ -1,6 +1,11 @@
import logging
import webbrowser
from pathlib import Path
from platform import uname
from typing import TYPE_CHECKING, List, Optional
from funcy import once_per_args
from dvc.render.utils import render
logger = logging.getLogger(__name__)
@@ -9,15 +14,27 @@ if TYPE_CHECKING:
from dvc.output import Output
from dvc.repo import Repo
webbrowser_open = once_per_args(webbrowser.open)
def create_summary(out):
assert out.live and out.live["html"]
metrics, plots = out.repo.live.show(out.fs_path)
html_path = out.fs_path + "_dvc_plots"
render(out.repo, plots, metrics=metrics, path=html_path, refresh_seconds=5)
index_path = render(
out.repo, plots, metrics=metrics, path=html_path, refresh_seconds=5
)
url = index_path.as_uri()
if "Microsoft" in uname().release:
url = Path(index_path) / "index.html"
webbrowser_open(url)
def summary_fs_path(out: "Output") -> Optional[str]:
+2
View File
@@ -243,6 +243,7 @@ def test_live_checkpoints_resume(
def test_dvc_generates_html_during_run(tmp_dir, dvc, mocker, live_stage):
show_spy = mocker.spy(dvc.live, "show")
webbrowser_open = mocker.patch("dvc.repo.live.webbrowser_open")
# make sure script takes more time to execute than one monitor sleep cycle
monitor_await_time = 0.01
@@ -264,6 +265,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
def test_dvclive_stage_with_different_wdir(tmp_dir, scm, dvc):