Repository: iterative/dvc

Issue:
plots: replace --show-json with --show-vega
Requested by @dmpetrov for cml. `--show-vega` should require a target and return a filled vega template. `--show-json` is not needed, let's delete it.


Discussion:

Resolving patch:
diff --git a/dvc/command/plots.py b/dvc/command/plots.py
--- a/dvc/command/plots.py
+++ b/dvc/command/plots.py
@@ -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,
@@ -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 = [
@@ -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.",
     )
     plots_show_parser.add_argument("--title", default=None, help="Plot title.")
     plots_show_parser.add_argument(
@@ -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(


Test patch:
diff --git a/tests/unit/command/test_plots.py b/tests/unit/command/test_plots.py
--- a/tests/unit/command/test_plots.py
+++ b/tests/unit/command/test_plots.py
@@ -13,7 +13,7 @@ def test_metrics_diff(dvc, mocker):
             "template",
             "--targets",
             "datafile",
-            "--show-json",
+            "--show-vega",
             "-x",
             "x_field",
             "-y",
@@ -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
 
@@ -59,7 +61,7 @@ def test_metrics_show(dvc, mocker):
             "result.extension",
             "-t",
             "template",
-            "--show-json",
+            "--show-vega",
             "--no-csv-header",
             "datafile",
         ]
@@ -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
 
@@ -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