Skip to content

Commit

Permalink
Fill template anchors inside custom template strings (#145)
Browse files Browse the repository at this point in the history
* Fill template anchors inserted into custom template strings

* Appease mypy
  • Loading branch information
mattseddon committed Oct 6, 2023
1 parent 6d045d7 commit e04c5de
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/dvc_render/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,23 @@ def generate_markdown(self, report_path=None) -> str:
data[x] = list(map(float, data[x]))
data[y] = list(map(float, data[y]))

plt.title(self.properties.get("title", Path(self.name).stem))
plt.xlabel(self.properties.get("x_label", x))
plt.ylabel(self.properties.get("y_label", y))
plt.plot(x, y, data=data)
plt.tight_layout()
plt.savefig(output_file)
plt.close()

if report_path:
return f"\n![{self.name}]({output_file.relative_to(report_folder)})"

base64_str = base64.b64encode(
output_file.getvalue() # type: ignore
).decode()
src = f"data:image/png;base64,{base64_str}"

return f"\n![{self.name}]({src})"
if x is not None and y is not None:
plt.title(self.properties.get("title", Path(self.name).stem))
plt.xlabel(self.properties.get("x_label", x))
plt.ylabel(self.properties.get("y_label", y))
plt.plot(x, y, data=data)
plt.tight_layout()
plt.savefig(output_file)
plt.close()

if report_path:
return f"\n![{self.name}]({output_file.relative_to(report_folder)})"

base64_str = base64.b64encode(
output_file.getvalue() # type: ignore
).decode()
src = f"data:image/png;base64,{base64_str}"

return f"\n![{self.name}]({src})"

return ""
2 changes: 2 additions & 0 deletions src/dvc_render/vega_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def dict_replace_value(d: dict, name: str, value: Any) -> dict:
if v == name:
x[k] = value
continue
if name in v and isinstance(value, str):
v = v.replace(name, value)
x[k] = v
return x

Expand Down
38 changes: 38 additions & 0 deletions tests/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,41 @@ def test_escape_special_characters():
assert filled["encoding"]["x"]["title"] == "foo.bar[0]"
assert filled["encoding"]["y"]["field"] == "foo\\.bar\\[1\\]"
assert filled["encoding"]["y"]["title"] == "foo.bar[1]"


def test_fill_anchor_in_string(tmp_dir):
y = "lab"
x = "SR"
tmp_dir.gen(
"custom.json",
json.dumps(
{
"data": {"values": Template.anchor("data")},
"transform": [
{"joinaggregate": [{"op": "mean", "field": "lab", "as": "mean_y"}]},
{
"calculate": "pow("
+ "datum.<DVC_METRIC_Y> - datum.<DVC_METRIC_X>,2"
+ ")",
"as": "SR",
},
{"joinaggregate": [{"op": "sum", "field": "SR", "as": "SSR"}]},
],
"encoding": {
"x": {"field": Template.anchor("x")},
"y": {"field": Template.anchor("y")},
},
},
),
)
datapoints = [
{x: "B", y: "A"},
{x: "A", y: "A"},
]
props = {"template": "custom.json", "x": x, "y": y}

renderer = VegaRenderer(datapoints, "foo", **props)
filled = renderer.get_filled_template(as_string=False)
assert filled["transform"][1]["calculate"] == "pow(datum.lab - datum.SR,2)"
assert filled["encoding"]["x"]["field"] == x
assert filled["encoding"]["y"]["field"] == y

0 comments on commit e04c5de

Please sign in to comment.