diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 76971cd1d..30c5b019a 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -13,6 +13,7 @@ DEFAULT_PRECISION = 5 def _show_metrics( metrics, + markdown=False, all_branches=False, all_tags=False, all_commits=False, @@ -65,7 +66,7 @@ def _show_metrics( if missing: raise BadMetricError(missing) - return table(header, rows, markdown=False) + return table(header, rows, markdown) class CmdMetricsBase(CmdBase): @@ -90,6 +91,7 @@ class CmdMetricsShow(CmdMetricsBase): else: table = _show_metrics( metrics, + self.args.show_md, self.args.all_branches, self.args.all_tags, self.args.all_commits, @@ -228,6 +230,12 @@ def add_parser(subparsers, parent_parser): default=False, help="Show output in JSON format.", ) + metrics_show_parser.add_argument( + "--show-md", + action="store_true", + default=False, + help="Show tabulated output in the Markdown format (GFM).", + ) metrics_show_parser.add_argument( "-R", "--recursive", diff --git a/tests/unit/command/test_metrics.py b/tests/unit/command/test_metrics.py index ac06eeeff..8abd9ab59 100644 --- a/tests/unit/command/test_metrics.py +++ b/tests/unit/command/test_metrics.py @@ -19,6 +19,7 @@ def test_metrics_diff(dvc, mocker): "-R", "--all", "--show-json", + "--show-md", "--targets", "target1", "target2", @@ -330,3 +331,42 @@ def test_metrics_show_precision(): Revision Path a b.ad b.bc branch_1 metrics.json 1.0987654 1.5342673 2.9877255""" ) + + +def test_metrics_show_default(): + assert _show_metrics( + { + "metrics.yaml": { + "x.b": {"old": 5, "new": 6}, + "a.d.e": {"old": 3, "new": 4, "diff": 1}, + "a.b.c": {"old": 1, "new": 2, "diff": 1}, + } + }, + ) == textwrap.dedent( + """\ + Path diff new old + x.b — 6 5 + a.d.e 1 4 3 + a.b.c 1 2 1""" + ) + + +def test_metrics_show_md(): + assert _show_metrics( + { + "metrics.yaml": { + "x.b": {"old": 5, "new": 6}, + "a.d.e": {"old": 3, "new": 4, "diff": 1}, + "a.b.c": {"old": 1, "new": 2, "diff": 1}, + } + }, + markdown=True, + ) == textwrap.dedent( + """\ + | Path | diff | new | old | + |--------|--------|-------|-------| + | x.b | — | 6 | 5 | + | a.d.e | 1 | 4 | 3 | + | a.b.c | 1 | 2 | 1 | + """ + )