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
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ warn_redundant_casts = true
warn_unreachable = true
files = ["src", "tests"]

[[tool.mypy.overrides]]
module = "tabulate"
ignore_missing_imports = true

Comment on lines +66 to +69
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's types-tabulate package that you can install.

[tool.pylint.message_control]
enable = ["c-extension-no-member", "no-else-return"]
disable = ["missing-module-docstring", "missing-class-docstring", "invalid-name", "R0801"]
Expand Down
9 changes: 6 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ zip_safe = False
package_dir=
=src
packages = find:
install_requires=
funcy>=1.17
tabulate>=0.8.7


[options.extras_require]
table =
tabulate>=0.8.7
docs =
mkdocs==1.3.0
mkdocs-gen-files==0.3.4
mkdocs-material==8.2.15
mkdocs-section-index==0.3.4
mkdocstrings-python==0.7.0
tests =
%(table)s
funcy>=1.17
pytest==7.1.2
pytest-sugar==0.9.4
pytest-cov==3.0.0
Expand All @@ -43,6 +45,7 @@ tests =
mypy==0.961
pytest-test-utils>=0.0.6
dev =
%(table)s
%(tests)s
%(docs)s

Expand Down
1 change: 0 additions & 1 deletion src/dvc_render/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def generate_html(self, html_path=None) -> str:
if partial:

div_id = self.remove_special_chars(self.name)
div_id = f"plot_{div_id}"

return self.DIV.format(id=div_id, partial=partial)
return ""
Expand Down
23 changes: 5 additions & 18 deletions src/dvc_render/html.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional

import tabulate # type: ignore

from .exceptions import DvcRenderException

if TYPE_CHECKING:
Expand All @@ -15,6 +13,11 @@
{refresh_tag}
<title>DVC Plot</title>
{scripts}
<style>
table {
border-spacing: 15px;
}
</style>
</head>
<body>
{plot_divs}
Expand Down Expand Up @@ -50,21 +53,6 @@ def __init__(
if refresh_seconds is not None:
self.refresh_tag = self.REFRESH_TAG.format(refresh_seconds)

def with_metrics(self, metrics: Dict[str, Dict]) -> "HTML":
"Adds metrics element."
header: List[str] = []
rows: List[List[str]] = []

for _, rev_data in metrics.items():
for _, data in rev_data.items():
if not header:
header.extend(sorted(data.keys()))

rows.append([data[key] for key in header])

self.elements.append(tabulate.tabulate(rows, header, tablefmt="html"))
return self

def with_scripts(self, scripts: str) -> "HTML":
"Extend scripts element."
if scripts not in self.scripts:
Expand Down Expand Up @@ -108,7 +96,6 @@ def render_html(

document = HTML(page_html, refresh_seconds=refresh_seconds)
if metrics:
document.with_metrics(metrics)
document.with_element("<br>")

for renderer in renderers:
Expand Down
33 changes: 33 additions & 0 deletions src/dvc_render/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from .base import Renderer

try:
from tabulate import tabulate
except ImportError:
tabulate = None


class TableRenderer(Renderer):
"""Renderer for tables."""

TYPE = "table"
DIV = """
<div id="{id}" style="text-align: center; padding: 10x">
<p>{id}</p>
<div style="display: flex;justify-content: center;">
{partial}
</div>
</div>"""

SCRIPTS = ""

EXTENSIONS = {".yml", ".yaml", ".json"}

def partial_html(self, **kwargs) -> str:
# From list of dicts to dict of lists
data = {
k: [datapoint[k] for datapoint in self.datapoints]
for k in self.datapoints[0]
}
if tabulate is None:
raise ImportError(f"{self.__class__} requires `tabulate`.")
return tabulate(data, headers="keys", tablefmt="html")
2 changes: 2 additions & 0 deletions src/dvc_render/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class VegaRenderer(Renderer):
EXTENSIONS = {".yml", ".yaml", ".json", ".csv", ".tsv"}

def __init__(self, datapoints: List, name: str, **properties):
if name and not name.startswith("plot_"):
name = f"plot_{name}"
super().__init__(datapoints, name, **properties)
self.template = get_template(
self.properties.get("template", None),
Expand Down
4 changes: 3 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
(
None,
["content"],
PAGE_HTML.format(plot_divs="content", refresh_tag="", scripts=""),
PAGE_HTML.replace("{plot_divs}", "content")
.replace("{scripts}", "")
.replace("{refresh_tag}", ""),
),
(
CUSTOM_PAGE_HTML,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parallel_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_write_parallel_coordinates(tmp_dir):
assert ParallelCoordinatesRenderer.SCRIPTS in html_text

div = ParallelCoordinatesRenderer.DIV.format(
id="plot_pcp", partial=renderer.partial_html()
id="pcp", partial=renderer.partial_html()
)
assert div in html_text

Expand Down
15 changes: 15 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from dvc_render.table import TableRenderer

# pylint: disable=missing-function-docstring


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