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
25 changes: 17 additions & 8 deletions dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def _func(self, *args, **kwargs):
raise NotImplementedError

def run(self):
if self.args.show_vega:
if not self.args.targets:
logger.error("please specify a target for `--show-vega`")
return 1
if len(self.args.targets) > 1:
logger.error(
"you can only specify one target for `--show-vega`"
)
return 1

try:
plots = self._func(
targets=self.args.targets,
Expand All @@ -45,10 +55,9 @@ def run(self):
y_title=self.args.ylab,
)

if self.args.show_json:
import json

logger.info(json.dumps(plots))
if self.args.show_vega:
target = self.args.targets[0]
logger.info(plots[target])
return 0

divs = [
Expand Down Expand Up @@ -138,10 +147,10 @@ def add_parser(subparsers, parent_parser):
help="Required when CSV or TSV datafile does not have a header.",
)
plots_show_parser.add_argument(
"--show-json",
"--show-vega",
action="store_true",
default=False,
help="Show output in JSON format.",
help="Show output in VEGA format.",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
help="Show output in VEGA format.",
help="Show output in Vega format.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same in line 216

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, these are fixed. Probably did that after.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm seeing this in the output of dvc plots show -h in 1.0.0a7+12172f which I think is the latest master version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jorgeorpinel Oh, sorry, I'm blind. I looked at the bottom part that is the patch itself and not your suggestion. Will fix, thank you!

)
plots_show_parser.add_argument("--title", default=None, help="Plot title.")
plots_show_parser.add_argument(
Expand Down Expand Up @@ -201,10 +210,10 @@ def add_parser(subparsers, parent_parser):
help="Provided CSV ot TSV datafile does not have a header.",
)
plots_diff_parser.add_argument(
"--show-json",
"--show-vega",
action="store_true",
default=False,
help="Show output in JSON format.",
help="Show output in VEGA format.",
)
plots_diff_parser.add_argument("--title", default=None, help="Plot title.")
plots_diff_parser.add_argument(
Expand Down
26 changes: 19 additions & 7 deletions tests/unit/command/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_metrics_diff(dvc, mocker):
"template",
"--targets",
"datafile",
"--show-json",
"--show-vega",
"-x",
"x_field",
"-y",
Expand All @@ -32,7 +32,9 @@ def test_metrics_diff(dvc, mocker):
assert cli_args.func == CmdPlotsDiff

cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.repo.plots.diff.diff", return_value={})
m = mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"}
)

assert cmd.run() == 0

Expand All @@ -59,7 +61,7 @@ def test_metrics_show(dvc, mocker):
"result.extension",
"-t",
"template",
"--show-json",
"--show-vega",
"--no-csv-header",
"datafile",
]
Expand All @@ -68,7 +70,9 @@ def test_metrics_show(dvc, mocker):

cmd = cli_args.func(cli_args)

m = mocker.patch("dvc.repo.plots.show.show", return_value={})
m = mocker.patch(
"dvc.repo.plots.show.show", return_value={"datafile": "filledtemplate"}
)

assert cmd.run() == 0

Expand All @@ -85,13 +89,21 @@ def test_metrics_show(dvc, mocker):
)


def test_plots_show_json(dvc, mocker, caplog):
def test_plots_show_vega(dvc, mocker, caplog):
cli_args = parse_args(
["plots", "diff", "HEAD~10", "HEAD~1", "--show-json"]
[
"plots",
"diff",
"HEAD~10",
"HEAD~1",
"--show-vega",
"--targets",
"plots.csv",
]
)
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"plots.csv": "plothtml"}
)
assert cmd.run() == 0
assert '{"plots.csv": "plothtml"}\n' in caplog.text
assert "plothtml" in caplog.text