Skip to content

plots: support opening plots file directly in the browser #5584

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

Merged
merged 3 commits into from
Mar 12, 2021

Conversation

skshetry
Copy link
Collaborator

@skshetry skshetry commented Mar 9, 2021

Closes #4179

$ dvc plots show --open

I don't know if this is that useful, but I think this does save some time (I was feeling the need when I was going through example-get-started).

Alternative:

No way to be compatible across different platforms. For me on Linux, it's following:

$ dvc plots show | xargs xdg-open

Working on macOS and on Windows (cc @jorgeorpinel?) confirmation would be great.

Thank you for the contribution - we'll try to review it as soon as possible. 🙏

@skshetry skshetry added the feature is a feature label Mar 9, 2021
@skshetry skshetry requested review from efiop and pared March 9, 2021 16:20
@skshetry skshetry self-assigned this Mar 9, 2021
@efiop
Copy link
Contributor

efiop commented Mar 9, 2021

@skshetry There doesn't seem to be any comments/requests from users thus far, worried that we are adding dead code that no one is going to use. Though it is possible that people don't know they want it.

@shcheklein
Copy link
Member

My workflow for this is to use Cmd (~Ctrl on Mac) + click the link DVC prints - it opens it automatically in the browser. Not saying that this option is not needed, just sharing my thoughts on why this feature didn't cross my mind I guess. I wonder how many users are familiar with hot keys in their terminals.

Copy link
Contributor

@pared pared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add --open to tests/unit/command/test_plot.py. Also, we have live command that produces HTML. Though the latter can be handled in other issue, as it seems we could share some options between plots and live.

@dberenbaum
Copy link
Contributor

I think it's a nice idea, although I guess it's a bit of an anomaly since no other commands open up anything outside the terminal. I would use it! Anything to avoid using my trackpad/mouse. It's also possible that some users are a bit confused by seeing a file:// url and don't realize it should be opened in the web browser. But I have no strong feelings on this, so feel free to drop it if it feels unnecessary.


logger.info(f"file://{path}")

url = f"file://{path}"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need to be quoted, will do in a separate PR.

@efiop efiop requested a review from pared March 10, 2021 09:30
Comment on lines +110 to +143
def test_plots_diff_open(tmp_dir, dvc, mocker, caplog):
mocked_open = mocker.patch("webbrowser.open", return_value=True)
cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"])
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"}
)

assert cmd.run() == 0

expected_url = f"file://{tmp_dir / 'plots.html'}"
assert expected_url in caplog.text

mocked_open.assert_called_once_with(expected_url)


def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog):
mocked_open = mocker.patch("webbrowser.open", return_value=False)
cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"])
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"}
)

assert cmd.run() == 1

expected_url = f"file://{tmp_dir / 'plots.html'}"
mocked_open.assert_called_once_with(expected_url)

error_message = "Failed to open. Please try opening it manually."
assert caplog.record_tuples == [
("dvc.command.plots", logging.INFO, expected_url),
("dvc.command.plots", logging.ERROR, error_message),
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about adding --open option to test_plots_diff, this exceeds expectations!

@efiop efiop merged commit d245008 into iterative:master Mar 12, 2021
@efiop
Copy link
Contributor

efiop commented Mar 12, 2021

@skshetry Btw, looks like even though the checkbox is ticket, the doc is missing?

@skshetry skshetry deleted the open-plots-directly branch March 12, 2021 02:33
@skshetry
Copy link
Collaborator Author

@skshetry Btw, looks like even though the checkbox is ticket, the doc is missing?

I was not sure if this was going to merged. I have created the docs, thanks.

@jorgeorpinel
Copy link
Contributor

jorgeorpinel commented Mar 17, 2021

Working on macOS and on Windows (cc @jorgeorpinel?) confirmation would be great.

I'm on Windows but on WSL (non-GUI Ubuntu) and it fails there unfort:

$ dvc plots show --open train.json
file:///home/jop/DVC-repos/test/plots.html
Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start "file:///home/.../plots.html"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

@jorgeorpinel
Copy link
Contributor

On Windows / Cmder (Git Bash) it works for me! But only from a DVC project dir.

Minor: for some reason if you run it outside a DVC project it get's stuck (while on Linux that seems to work regardless of where you are).

@jorgeorpinel
Copy link
Contributor

jorgeorpinel commented Mar 17, 2021

Same on regular Command Prompt (cmd):

  • plots show (in general) gets stuck if run outside a DVC project dir.
  • plots show --open works from within a DVC project 🙂

@skshetry
Copy link
Collaborator Author

@jorgeorpinel, opened #5670 to address the WSL related issue.

To open into a browser in the host computer, you have to specify BROWSER envvar, something like follows:

export BROWSER=/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe

and then you can do --open and it'll open in the specified browser. For me, it's terribly slow.

@skshetry
Copy link
Collaborator Author

@jorgeorpinel, for other issues, please open an issue as it needs more information on what's happening. Thanks. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plots diff/show: --open flag to directly open it in the default browser
6 participants