diff --git a/dvc/repo/plots/__init__.py b/dvc/repo/plots/__init__.py index 8e873e915..8a148aacb 100644 --- a/dvc/repo/plots/__init__.py +++ b/dvc/repo/plots/__init__.py @@ -55,9 +55,8 @@ class Plots: try: with tree.open(path_info) as fd: data[rev][datafile]["data"] = fd.read() - except FileNotFoundError as e: + except FileNotFoundError: # This might happen simply because cache is absent - print(e) pass return data diff --git a/dvc/repo/plots/template.py b/dvc/repo/plots/template.py index d81775bd0..1ea50abac 100644 --- a/dvc/repo/plots/template.py +++ b/dvc/repo/plots/template.py @@ -27,13 +27,7 @@ class Template: INDENT = 4 SEPARATORS = (",", ": ") EXTENSION = ".json" - ANCHOR = '""' - METRIC_DATA_ANCHOR = "" - X_ANCHOR = "" - Y_ANCHOR = "" - TITLE_ANCHOR = "" - X_LABEL_ANCHOR = "" - Y_LABEL_ANCHOR = "" + ANCHOR = "" def __init__(self, content=None, name=None): self.content = self.DEFAULT_CONTENT if content is None else content @@ -43,10 +37,10 @@ class Template: def render(self, data, props=None): props = props or {} - if self.METRIC_DATA_ANCHOR not in self.content: + if self._anchor_str("data") not in self.content: + anchor = self.anchor("data") raise BadTemplateError( - f"Template '{self.filename}' is not using " - f"'{self.METRIC_DATA_ANCHOR}' anchor" + f"Template '{self.filename}' is not using '{anchor}' anchor" ) if props.get("x"): @@ -59,19 +53,23 @@ class Template: return content + @classmethod + def anchor(cls, name): + return cls.ANCHOR.format(name.upper()) + def has_anchor(self, name): - return self._anchor(name) in self.content + return self._anchor_str(name) in self.content @classmethod def _fill_anchor(cls, content, name, value): value_str = json.dumps( value, indent=cls.INDENT, separators=cls.SEPARATORS, sort_keys=True ) - return content.replace(cls._anchor(name), value_str) + return content.replace(cls._anchor_str(name), value_str) @classmethod - def _anchor(cls, name): - return cls.ANCHOR.format(name.upper()) + def _anchor_str(cls, name): + return '"{}"'.format(cls.anchor(name)) @classmethod def _fill_metadata(cls, content, props): @@ -98,19 +96,19 @@ class DefaultLinearTemplate(Template): DEFAULT_CONTENT = { "$schema": "https://vega.github.io/schema/vega-lite/v4.json", - "data": {"values": Template.METRIC_DATA_ANCHOR}, - "title": Template.TITLE_ANCHOR, + "data": {"values": Template.anchor("data")}, + "title": Template.anchor("title"), "mark": {"type": "line"}, "encoding": { "x": { - "field": Template.X_ANCHOR, + "field": Template.anchor("x"), "type": "quantitative", - "title": Template.X_LABEL_ANCHOR, + "title": Template.anchor("x_label"), }, "y": { - "field": Template.Y_ANCHOR, + "field": Template.anchor("y"), "type": "quantitative", - "title": Template.Y_LABEL_ANCHOR, + "title": Template.anchor("y_label"), "scale": {"zero": False}, }, "color": {"field": "rev", "type": "nominal"}, @@ -122,21 +120,21 @@ class DefaultConfusionTemplate(Template): DEFAULT_NAME = "confusion" DEFAULT_CONTENT = { "$schema": "https://vega.github.io/schema/vega-lite/v4.json", - "data": {"values": Template.METRIC_DATA_ANCHOR}, - "title": Template.TITLE_ANCHOR, + "data": {"values": Template.anchor("data")}, + "title": Template.anchor("title"), "mark": "rect", "encoding": { "x": { - "field": Template.X_ANCHOR, + "field": Template.anchor("x"), "type": "nominal", "sort": "ascending", - "title": Template.X_LABEL_ANCHOR, + "title": Template.anchor("x_label"), }, "y": { - "field": Template.Y_ANCHOR, + "field": Template.anchor("y"), "type": "nominal", "sort": "ascending", - "title": Template.Y_LABEL_ANCHOR, + "title": Template.anchor("y_label"), }, "color": {"aggregate": "count", "type": "quantitative"}, "facet": {"field": "rev", "type": "nominal"}, @@ -148,19 +146,19 @@ class DefaultScatterTemplate(Template): DEFAULT_NAME = "scatter" DEFAULT_CONTENT = { "$schema": "https://vega.github.io/schema/vega-lite/v4.json", - "data": {"values": Template.METRIC_DATA_ANCHOR}, - "title": Template.TITLE_ANCHOR, + "data": {"values": Template.anchor("data")}, + "title": Template.anchor("title"), "mark": "point", "encoding": { "x": { - "field": Template.X_ANCHOR, + "field": Template.anchor("x"), "type": "quantitative", - "title": Template.X_LABEL_ANCHOR, + "title": Template.anchor("x_label"), }, "y": { - "field": Template.Y_ANCHOR, + "field": Template.anchor("y"), "type": "quantitative", - "title": Template.Y_LABEL_ANCHOR, + "title": Template.anchor("y_label"), "scale": {"zero": False}, }, "color": {"field": "rev", "type": "nominal"}, @@ -173,27 +171,27 @@ class SmoothLinearTemplate(Template): DEFAULT_CONTENT = { "$schema": "https://vega.github.io/schema/vega-lite/v4.json", - "data": {"values": Template.METRIC_DATA_ANCHOR}, - "title": Template.TITLE_ANCHOR, + "data": {"values": Template.anchor("data")}, + "title": Template.anchor("title"), "mark": {"type": "line"}, "encoding": { "x": { - "field": Template.X_ANCHOR, + "field": Template.anchor("x"), "type": "quantitative", - "title": Template.X_LABEL_ANCHOR, + "title": Template.anchor("x_label"), }, "y": { - "field": Template.Y_ANCHOR, + "field": Template.anchor("y"), "type": "quantitative", - "title": Template.Y_LABEL_ANCHOR, + "title": Template.anchor("y_label"), "scale": {"zero": False}, }, "color": {"field": "rev", "type": "nominal"}, }, "transform": [ { - "loess": Template.Y_ANCHOR, - "on": Template.X_ANCHOR, + "loess": Template.anchor("y"), + "on": Template.anchor("x"), "groupby": ["rev"], "bandwidth": 0.3, }