Skip to content
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
18 changes: 3 additions & 15 deletions dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,11 @@ def run(self):
html_template_path=self.args.html_template,
)

assert index_path.is_absolute()
url = index_path.as_uri()
ui.write(url)
ui.write(index_path.as_uri())

if self.args.open:
import webbrowser
from platform import uname

if "Microsoft" in uname().release:
url = Path(rel) / "index.html"

opened = webbrowser.open(url)
if not opened:
ui.error_write(
"Failed to open. Please try opening it manually."
)
return 1
return ui.open_browser(index_path)

return 0

except DvcException:
Expand Down
13 changes: 3 additions & 10 deletions dvc/repo/live.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
from pathlib import Path
from platform import uname
from typing import TYPE_CHECKING, List, Optional

from funcy import once_per_args
Expand All @@ -16,9 +14,9 @@

@once_per_args
def webbrowser_open(url: str) -> int:
import webbrowser
from dvc.ui import ui

return webbrowser.open(url)
return ui.open_browser(url)


def create_summary(out):
Expand All @@ -33,12 +31,7 @@ def create_summary(out):
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)
webbrowser_open(index_path)


def summary_fs_path(out: "Output") -> Optional[str]:
Expand Down
23 changes: 23 additions & 0 deletions dvc/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from rich.text import Text as RichText

from dvc.progress import Tqdm
from dvc.types import StrPath
from dvc.ui.table import Headers, Styles, TableData


Expand Down Expand Up @@ -260,6 +261,28 @@ def isatty(self) -> bool:

return sys.stdout.isatty()

def open_browser(self, file: "StrPath") -> int:
import webbrowser
from pathlib import Path
from platform import uname

from dvc.utils import relpath

path = Path(file).resolve()
url = (
relpath(path) if "Microsoft" in uname().release else path.as_uri()
)

opened = webbrowser.open(url)

if not opened:
ui.error_write(
f"Failed to open {url}. " "Please try opening it manually."
)
return 1

return 0


ui = Console()

Expand Down
7 changes: 5 additions & 2 deletions tests/unit/command/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_plots_diff_open_WSL(tmp_dir, dvc, mocker, plots_data):
mocker.patch("dvc.command.plots.render", return_value=index_path)

assert cmd.run() == 0
mocked_open.assert_called_once_with(Path("dvc_plots") / "index.html")
mocked_open.assert_called_once_with(str(Path("dvc_plots") / "index.html"))


def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys, plots_data):
Expand All @@ -189,7 +189,10 @@ def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys, plots_data):
expected_url = tmp_dir / "dvc_plots" / "index.html"
mocked_open.assert_called_once_with(expected_url.as_uri())

error_message = "Failed to open. Please try opening it manually."
error_message = (
f"Failed to open {expected_url.as_uri()}. "
"Please try opening it manually."
)

out, err = capsys.readouterr()
assert expected_url.as_uri() in out
Expand Down