Skip to content

Commit

Permalink
table: show nested metrics as separate columns (#130)
Browse files Browse the repository at this point in the history
* table: show nested metrics as separate columns

* add docstring

* use flatten_dict

* ignore mypy error
  • Loading branch information
dberenbaum authored May 9, 2023
1 parent 8aab523 commit a597b7c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ packages = find:
[options.extras_require]
table =
tabulate>=0.8.7
flatten_dict<1,>=0.4.1
markdown =
%(table)s
matplotlib
Expand Down
6 changes: 5 additions & 1 deletion src/dvc_render/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from flatten_dict import flatten # type: ignore


def list_dict_to_dict_list(list_dict):
"""Convert from list of dictionaries to dictionary of lists."""
if not list_dict:
return {}
return {k: [x[k] for x in list_dict] for k in list_dict[0]}
flat_list_dict = [flatten(d, reducer="dot") for d in list_dict]
return {k: [x[k] for x in flat_list_dict] for k in flat_list_dict[0]}
8 changes: 8 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ def test_generate_markdown():
assert "| foo | bar |" in md
assert "|-------|-------|" in md
assert "| 1 | 2 |" in md


def test_nested():
datapoints = [{"foo": {"bar": 1}}]
html = TableRenderer(datapoints, "metrics.json").generate_html()
assert "<p>metrics_json</p>" in html
assert '<tr><th style="text-align: right;"> foo.bar</th></tr>' in html
assert '<tr><td style="text-align: right;"> 1</td></tr>' in html

0 comments on commit a597b7c

Please sign in to comment.