Skip to content

Commit 49b8f8a

Browse files
authored
vega: Add as_string argument. Default True. (#125)
`0.3.0` release broke `dvc plots --json` for older DVC installations because `dvc-render` dep didn't had an upper bound.
1 parent ba87a2e commit 49b8f8a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/dvc_render/vega.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from pathlib import Path
3-
from typing import Any, Dict, List, Optional
3+
from typing import Any, Dict, List, Optional, Union
44
from warnings import warn
55

66
from .base import Renderer
@@ -38,8 +38,11 @@ def __init__(self, datapoints: List, name: str, **properties):
3838
)
3939

4040
def get_filled_template(
41-
self, skip_anchors: Optional[List[str]] = None, strict: bool = True
42-
) -> Dict[str, Any]:
41+
self,
42+
skip_anchors: Optional[List[str]] = None,
43+
strict: bool = True,
44+
as_string: bool = True,
45+
) -> Union[str, Dict[str, Any]]:
4346
"""Returns a functional vega specification"""
4447
self.template.reset()
4548
if not self.datapoints:
@@ -80,10 +83,13 @@ def get_filled_template(
8083
value = self.template.escape_special_characters(value)
8184
self.template.fill_anchor(name, value)
8285

86+
if as_string:
87+
return json.dumps(self.template.content)
88+
8389
return self.template.content
8490

8591
def partial_html(self, **kwargs) -> str:
86-
return json.dumps(self.get_filled_template())
92+
return self.get_filled_template() # type: ignore
8793

8894
def generate_markdown(self, report_path=None) -> str:
8995
if not isinstance(self.template, LinearTemplate):

tests/test_vega.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_default_template_mark():
4040
{"first_val": 200, "second_val": 300, "val": 3},
4141
]
4242

43-
plot_content = VegaRenderer(datapoints, "foo").get_filled_template()
43+
plot_content = VegaRenderer(datapoints, "foo").get_filled_template(as_string=False)
4444

4545
assert plot_content["layer"][0]["mark"] == "line"
4646

@@ -57,7 +57,9 @@ def test_choose_axes():
5757
{"first_val": 200, "second_val": 300, "val": 3},
5858
]
5959

60-
plot_content = VegaRenderer(datapoints, "foo", **props).get_filled_template()
60+
plot_content = VegaRenderer(datapoints, "foo", **props).get_filled_template(
61+
as_string=False
62+
)
6163

6264
assert plot_content["data"]["values"] == [
6365
{
@@ -82,7 +84,9 @@ def test_confusion():
8284
]
8385
props = {"template": "confusion", "x": "predicted", "y": "actual"}
8486

85-
plot_content = VegaRenderer(datapoints, "foo", **props).get_filled_template()
87+
plot_content = VegaRenderer(datapoints, "foo", **props).get_filled_template(
88+
as_string=False
89+
)
8690

8791
assert plot_content["data"]["values"] == [
8892
{"predicted": "B", "actual": "A"},
@@ -170,7 +174,7 @@ def test_escape_special_characters():
170174
]
171175
props = {"template": "simple", "x": "foo.bar[0]", "y": "foo.bar[1]"}
172176
renderer = VegaRenderer(datapoints, "foo", **props)
173-
filled = renderer.get_filled_template()
177+
filled = renderer.get_filled_template(as_string=False)
174178
# data is not escaped
175179
assert filled["data"]["values"][0] == datapoints[0]
176180
# field and title yes

0 commit comments

Comments
 (0)