plots: introduce --json option

Fixes: #7073
This commit is contained in:
Paweł Redzyński
2021-12-15 11:05:57 +01:00
parent 25ef812ecf
commit e24de3a0f3
12 changed files with 282 additions and 139 deletions
+3 -1
View File
@@ -3,6 +3,7 @@ from pathlib import Path
from dvc.command import completion
from dvc.command.base import CmdBase, fix_subparsers
from dvc.render.utils import match_renderers
from dvc.ui import ui
@@ -16,7 +17,8 @@ class CmdLive(CmdBase):
html_path = Path.cwd() / (self.args.target + "_html")
from dvc.render.utils import render
index_path = render(self.repo, plots, metrics, html_path)
renderers = match_renderers(plots, self.repo.plots.templates)
index_path = render(self.repo, renderers, metrics, html_path)
ui.write(index_path.as_uri())
return 0
return 1
+44 -6
View File
@@ -1,16 +1,32 @@
import argparse
import json
import logging
from funcy import first
from dvc.command import completion
from dvc.command.base import CmdBase, append_doc_link, fix_subparsers
from dvc.exceptions import DvcException
from dvc.render.utils import find_vega, render
from dvc.render.utils import match_renderers, render
from dvc.render.vega import VegaRenderer
from dvc.ui import ui
from dvc.utils import format_link
logger = logging.getLogger(__name__)
def _show_json(renderers, path: None):
if any(lambda r: r.needs_output_path for r in renderers) and not path:
raise DvcException("Output path ('-o') is required!")
result = {
renderer.filename: json.loads(renderer.as_json(path=path))
for renderer in renderers
}
if result:
ui.write_json(result)
class CmdPlots(CmdBase):
def _func(self, *args, **kwargs):
raise NotImplementedError
@@ -34,8 +50,15 @@ class CmdPlots(CmdBase):
"you can only specify one target for `--show-vega`"
)
return 1
if self.args.json:
logger.error(
"'--show-vega' and '--json' are mutually exclusive "
"options."
)
return 1
try:
plots_data = self._func(
targets=self.args.targets, props=self._props()
)
@@ -46,18 +69,26 @@ class CmdPlots(CmdBase):
"visualization file will not be created."
)
renderers = match_renderers(
plots_data=plots_data, templates=self.repo.plots.templates
)
if self.args.show_vega:
target = self.args.targets[0]
plot_json = find_vega(self.repo, plots_data, target)
if plot_json:
ui.write(plot_json)
renderer = first(
filter(lambda r: isinstance(r, VegaRenderer), renderers)
)
if renderer:
ui.write_json(renderer.asdict())
return 0
if self.args.json:
_show_json(renderers, self.args.out)
return 0
rel: str = self.args.out or "dvc_plots"
path: Path = (Path.cwd() / rel).resolve()
index_path = render(
self.repo,
plots_data,
renderers,
path=path,
html_template_path=self.args.html_template,
)
@@ -251,6 +282,13 @@ def _add_output_arguments(parser):
default=False,
help="Show output in Vega format.",
)
parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help=argparse.SUPPRESS,
)
parser.add_argument(
"--open",
action="store_true",
+20 -3
View File
@@ -16,6 +16,9 @@ class BadTemplateError(DvcException):
class Renderer(abc.ABC):
REVISIONS_KEY = "revisions"
TYPE_KEY = "type"
def __init__(self, data: Dict, templates=None):
self.data = data
self.templates = templates
@@ -28,7 +31,17 @@ class Renderer(abc.ABC):
assert len(files) == 1
self.filename = files.pop()
def _convert(self, path: "StrPath"):
def partial_html(self, **kwargs):
"""
Us this method to generate partial HTML content,
that will fill self.DIV
"""
raise NotImplementedError
@property
@abc.abstractmethod
def TYPE(self):
raise NotImplementedError
@property
@@ -42,7 +55,7 @@ class Renderer(abc.ABC):
raise NotImplementedError
@abc.abstractmethod
def as_json(self):
def as_json(self, **kwargs):
raise NotImplementedError
@staticmethod
@@ -51,9 +64,13 @@ class Renderer(abc.ABC):
{ord(c): "_" for c in r"!@#$%^&*()[]{};,<>?\/:.|`~=_+"}
)
@property
def needs_output_path(self):
return False
def generate_html(self, path: "StrPath"):
"""this method might edit content of path"""
partial = self._convert(path)
partial = self.partial_html(path=path)
div_id = self._remove_special_chars(self.filename)
div_id = f"plot_{div_id}"
+61 -22
View File
@@ -1,6 +1,10 @@
import json
import os
from typing import TYPE_CHECKING
from funcy import reraise
from dvc.exceptions import DvcException
from dvc.render.base import Renderer
from dvc.render.utils import get_files
from dvc.utils import relpath
@@ -10,6 +14,7 @@ if TYPE_CHECKING:
class ImageRenderer(Renderer):
TYPE = "image"
DIV = """
<div
id="{id}"
@@ -20,49 +25,83 @@ class ImageRenderer(Renderer):
SCRIPTS = ""
@property
def needs_output_path(self):
return True
def _write_image(
self,
page_dir_path: "StrPath",
path: "StrPath",
revision: str,
filename: str,
image_data: bytes,
):
static = os.path.join(page_dir_path, "static")
os.makedirs(static, exist_ok=True)
img_path = os.path.join(
static, f"{revision}_{filename.replace(os.sep, '_')}"
path, f"{revision}_{filename.replace(os.sep, '_')}"
)
with open(img_path, "wb") as fd:
fd.write(image_data)
return """
return img_path
def _save_images(self, path: "StrPath"):
for rev, rev_data in self.data.items():
if "data" in rev_data:
for file, file_data in rev_data.get("data", {}).items():
if "data" in file_data:
if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)
yield rev, file, self._write_image(
os.path.abspath(path), rev, file, file_data["data"]
)
def partial_html(self, **kwargs):
path = kwargs.get("path", None)
if not path:
raise DvcException("Can't save here")
static = os.path.join(path, "static")
div_content = []
for rev, _, img_path in self._save_images(static):
div_content.append(
"""
<div
style="border:1px dotted black;margin:2px;display: inline-block;
overflow:hidden;margin-left:8px;">
<p>{title}</p>
<img src="{src}">
</div>""".format(
title=revision, src=(relpath(img_path, page_dir_path))
)
def _convert(self, path: "StrPath"):
div_content = []
for rev, rev_data in self.data.items():
if "data" in rev_data:
for file, file_data in rev_data.get("data", {}).items():
if "data" in file_data:
div_content.append(
self._write_image(
path, rev, file, file_data["data"]
)
)
title=rev, src=(relpath(img_path, path))
)
)
if div_content:
div_content.insert(0, f"<p>{self.filename}</p>")
return "\n".join(div_content)
return ""
def as_json(self):
raise NotImplementedError
def as_json(self, **kwargs):
with reraise(
KeyError,
DvcException(
f"{type(self).__name__} needs 'path' to store images."
),
):
path = kwargs["path"]
results = []
for revision, _, img_path in self._save_images(path):
results.append(
{
self.TYPE_KEY: self.TYPE,
self.REVISIONS_KEY: [revision],
"url": img_path,
}
)
return json.dumps(results)
@staticmethod
def matches(data):
+1 -12
View File
@@ -21,15 +21,6 @@ def group_by_filename(plots_data: Dict) -> List[Dict]:
return grouped
def find_vega(repo, plots_data, target):
found = dpath.util.search(plots_data, ["*", "*", target])
from dvc.render.vega import VegaRenderer
if found and VegaRenderer.matches(found):
return VegaRenderer(found, repo.plots.templates).as_json()
return ""
def match_renderers(plots_data, templates):
from dvc.render import RENDERERS
@@ -43,14 +34,12 @@ def match_renderers(plots_data, templates):
def render(
repo,
plots_data,
renderers,
metrics=None,
path=None,
html_template_path=None,
refresh_seconds=None,
):
# TODO we could probably remove repo usages (here and in VegaRenderer)
renderers = match_renderers(plots_data, repo.plots.templates)
if not html_template_path:
html_template_path = repo.config.get("plots", {}).get(
"html_template", None
+32 -10
View File
@@ -1,6 +1,7 @@
import json
import os
from copy import copy, deepcopy
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from typing import Dict, List, Optional, Union
from funcy import first
@@ -13,9 +14,6 @@ from dvc.render.base import (
)
from dvc.render.utils import get_files
if TYPE_CHECKING:
from dvc.types import StrPath
class PlotDataStructureError(DvcException):
def __init__(self):
@@ -92,6 +90,8 @@ def _append_revision(datapoints: List[Dict], revision) -> List[Dict]:
class VegaRenderer(Renderer):
TYPE = "vega"
DIV = """
<div id = "{id}">
<script type = "text/javascript">
@@ -115,6 +115,9 @@ class VegaRenderer(Renderer):
resolved = {**resolved, **props}
return resolved
def _revisions(self):
return list(self.data.keys())
def _datapoints(self, props: Dict):
fields = props.get("fields", set())
if fields:
@@ -139,7 +142,7 @@ class VegaRenderer(Renderer):
datapoints.extend(tmp)
return datapoints
def fill_template(self, template, datapoints, props=None):
def _fill_template(self, template, datapoints, props=None):
props = props or {}
content = deepcopy(template.content)
@@ -168,7 +171,7 @@ class VegaRenderer(Renderer):
return content
def as_json(self) -> Optional[str]:
def get_filled_template(self):
props = self._squash_props()
template = self.templates.load(props.get("template", None))
@@ -186,14 +189,33 @@ class VegaRenderer(Renderer):
props["y"] = first(
f for f in reversed(fields) if f not in skip
)
filled_template = self.fill_template(template, datapoints, props)
filled_template = self._fill_template(template, datapoints, props)
return filled_template
return None
def _convert(self, path: "StrPath"):
return self.as_json()
def asdict(self):
filled_template = self.get_filled_template()
if filled_template:
return json.loads(filled_template)
return {}
def as_json(self, **kwargs) -> Optional[str]:
content = self.asdict()
return json.dumps(
[
{
self.TYPE_KEY: self.TYPE,
self.REVISIONS_KEY: self._revisions(),
"content": content,
}
],
)
def partial_html(self, **kwargs):
return self.get_filled_template() or ""
@staticmethod
def matches(data):
+3 -2
View File
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, List, Optional
from funcy import once_per_args
from dvc.render.utils import render
from dvc.render.utils import match_renderers, render
logger = logging.getLogger(__name__)
@@ -27,8 +27,9 @@ def create_summary(out):
html_path = out.fs_path + "_dvc_plots"
renderers = match_renderers(plots, out.repo.plots.templates)
index_path = render(
out.repo, plots, metrics=metrics, path=html_path, refresh_seconds=5
out.repo, renderers, metrics=metrics, path=html_path, refresh_seconds=5
)
webbrowser_open(index_path)
+2
View File
@@ -29,6 +29,8 @@ def _id_gen(val) -> str:
(["params", "diff", "--show-json"], "json"),
(["params", "diff", "--show-md"], "markdown"),
(["status", "--show-json"], "json"),
(["plots", "show", "--show-json"], "json"),
(["plots", "diff", "--show-json"], "json"),
],
ids=_id_gen,
)
+40 -6
View File
@@ -1,3 +1,4 @@
import json
import os
import posixpath
from pathlib import Path
@@ -125,16 +126,15 @@ def test_plots_diff_vega(dvc, mocker, capsys, plots_data):
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
mocker.patch(
"dvc.command.plots.find_vega", return_value="vega_json_content"
)
render_mock = mocker.patch(
"dvc.command.plots.render", return_value="html_path"
"dvc.command.plots.VegaRenderer.asdict",
return_value={"this": "is vega json"},
)
render_mock = mocker.patch("dvc.command.plots.render")
assert cmd.run() == 0
out, _ = capsys.readouterr()
assert "vega_json_content" in out
assert json.dumps({"this": "is vega json"}) in out
render_mock.assert_not_called()
@@ -243,6 +243,8 @@ def test_should_call_render(tmp_dir, mocker, capsys, plots_data, output):
output = output or "dvc_plots"
index_path = tmp_dir / output / "index.html"
renderers = mocker.MagicMock()
mocker.patch("dvc.command.plots.match_renderers", return_value=renderers)
render_mock = mocker.patch(
"dvc.command.plots.render", return_value=index_path
)
@@ -253,5 +255,37 @@ def test_should_call_render(tmp_dir, mocker, capsys, plots_data, output):
assert index_path.as_uri() in out
render_mock.assert_called_once_with(
cmd.repo, plots_data, path=tmp_dir / output, html_template_path=None
cmd.repo, renderers, path=tmp_dir / output, html_template_path=None
)
def test_plots_diff_json(dvc, mocker, capsys):
cli_args = parse_args(
[
"plots",
"diff",
"HEAD~10",
"HEAD~1",
"--json",
"--targets",
"plot.csv",
"-o",
"out",
]
)
cmd = cli_args.func(cli_args)
data = mocker.MagicMock()
mocker.patch("dvc.repo.plots.diff.diff", return_value=data)
renderers = mocker.MagicMock()
mocker.patch("dvc.command.plots.match_renderers", return_value=renderers)
render_mock = mocker.patch("dvc.command.plots.render")
show_json_mock = mocker.patch("dvc.command.plots._show_json")
assert cmd.run() == 0
show_json_mock.assert_called_once_with(renderers, "out")
render_mock.assert_not_called()
+33
View File
@@ -1,3 +1,4 @@
import json
import os
import pytest
@@ -44,3 +45,35 @@ def test_render(tmp_dir):
assert (
f'<img src="{os.path.join("static", "workspace_file.jpg")}">' in html
)
def test_as_json(tmp_dir):
data = {
"workspace": {"data": {"file.jpg": {"data": b"content"}}},
"HEAD": {"data": {"file.jpg": {"data": b"content2"}}},
}
page_dir = os.path.join("some", "path")
json_content = json.loads(ImageRenderer(data).as_json(path=page_dir))
img1 = os.path.join(page_dir, "workspace_file.jpg")
img2 = os.path.join(page_dir, "HEAD_file.jpg")
assert {
"type": "image",
"revisions": ["workspace"],
"url": os.path.abspath(img1),
} in json_content
assert {
"type": "image",
"revisions": ["HEAD"],
"url": os.path.abspath(img2),
} in json_content
def assert_file_with_content(path, content):
assert os.path.isfile(path)
with open(path, "rb") as fd:
assert fd.read() == content
assert_file_with_content(img1, b"content")
assert_file_with_content(img2, b"content2")
+13 -8
View File
@@ -1,6 +1,9 @@
import os
from dvc.render.utils import find_vega, render
import dpath
from dvc.render.utils import match_renderers, render
from dvc.render.vega import VegaRenderer
def assert_website_has_image(page_path, revision, filename, image_content):
@@ -48,8 +51,9 @@ def test_render(tmp_dir, dvc):
}
},
}
renderers = match_renderers(data, dvc.plots.templates)
render(dvc, data, path=os.path.join("results", "dir"))
render(dvc, renderers, path=os.path.join("results", "dir"))
page_path = tmp_dir / "results" / "dir"
index_path = page_path / "index.html"
@@ -60,12 +64,13 @@ def test_render(tmp_dir, dvc):
assert_website_has_image(page_path, "v2", "other_file.jpg", b"content2")
assert_website_has_image(page_path, "v1", "another.gif", b"content3")
index_content = index_path.read_text()
file_vega = find_vega(dvc, data, "file.json")
some_vega = find_vega(dvc, data, "some.csv")
def clean(txt: str) -> str:
return txt.replace("\n", "").replace("\r", "").replace(" ", "")
assert clean(file_vega) in clean(index_content)
assert clean(some_vega) in clean(index_content)
def get_vega_string(data, filename):
file_data = dpath.util.search(data, ["*", "*", filename])
return VegaRenderer(file_data, dvc.plots.templates).partial_html()
index_content = index_path.read_text()
assert clean(get_vega_string(data, "file.json")) in clean(index_content)
assert clean(get_vega_string(data, "some.csv")) in clean(index_content)
+30 -69
View File
@@ -5,7 +5,7 @@ from collections import OrderedDict
import pytest
from funcy import first
from dvc.render.utils import find_vega, group_by_filename
from dvc.render.utils import group_by_filename
from dvc.render.vega import (
INDEX_FIELD,
REVISION_FIELD,
@@ -107,9 +107,8 @@ def test_one_column(tmp_dir, scm, dvc):
}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["title"] == "mytitle"
assert plot_content["data"]["values"] == [
{"val": 2, INDEX_FIELD: 0, REVISION_FIELD: "workspace"},
@@ -133,9 +132,8 @@ def test_multiple_columns(tmp_dir, scm, dvc):
"workspace": {"data": {"file.json": {"data": metric, "props": {}}}}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{
"val": 2,
@@ -169,9 +167,8 @@ def test_choose_axes(tmp_dir, scm, dvc):
data = {
"workspace": {"data": {"file.json": {"data": metric, "props": props}}}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{
"val": 2,
@@ -207,9 +204,8 @@ def test_confusion(tmp_dir, dvc):
}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{"predicted": "B", "actual": "A", REVISION_FIELD: "workspace"},
{"predicted": "A", "actual": "A", REVISION_FIELD: "workspace"},
@@ -245,9 +241,8 @@ def test_multiple_revs_default(tmp_dir, scm, dvc):
},
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{"y": 5, INDEX_FIELD: 0, REVISION_FIELD: "HEAD"},
{"y": 6, INDEX_FIELD: 1, REVISION_FIELD: "HEAD"},
@@ -271,9 +266,8 @@ def test_metric_missing(tmp_dir, scm, dvc, caplog):
"data": {"file.json": {"error": FileNotFoundError(), "props": {}}}
},
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{"y": 2, INDEX_FIELD: 0, REVISION_FIELD: "v2"},
{"y": 3, INDEX_FIELD: 1, REVISION_FIELD: "v2"},
@@ -291,9 +285,8 @@ def test_custom_template(tmp_dir, scm, dvc, custom_template):
"workspace": {"data": {"file.json": {"data": metric, "props": props}}}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{"a": 1, "b": 2, REVISION_FIELD: "workspace"},
{"a": 2, "b": 3, REVISION_FIELD: "workspace"},
@@ -310,7 +303,7 @@ def test_raise_on_no_template(tmp_dir, dvc):
}
with pytest.raises(TemplateNotFoundError):
VegaRenderer(data, dvc.plots.templates).as_json()
VegaRenderer(data, dvc.plots.templates).asdict()
def test_bad_template(tmp_dir, dvc):
@@ -322,7 +315,7 @@ def test_bad_template(tmp_dir, dvc):
}
with pytest.raises(BadTemplateError):
VegaRenderer(data, dvc.plots.templates).as_json()
VegaRenderer(data, dvc.plots.templates).asdict()
def test_plot_choose_columns(tmp_dir, scm, dvc, custom_template):
@@ -337,9 +330,8 @@ def test_plot_choose_columns(tmp_dir, scm, dvc, custom_template):
"workspace": {"data": {"file.json": {"data": metric, "props": props}}}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
plot_content = json.loads(plot_string)
assert plot_content["data"]["values"] == [
{"b": 2, "c": 3, REVISION_FIELD: "workspace"},
{"b": 3, "c": 4, REVISION_FIELD: "workspace"},
@@ -356,8 +348,7 @@ def test_plot_default_choose_column(tmp_dir, scm, dvc):
}
}
plot_string = VegaRenderer(data, dvc.plots.templates).as_json()
plot_content = json.loads(plot_string)
plot_content = VegaRenderer(data, dvc.plots.templates).asdict()
assert plot_content["data"]["values"] == [
{INDEX_FIELD: 0, "b": 2, REVISION_FIELD: "workspace"},
@@ -378,7 +369,7 @@ def test_raise_on_wrong_field(tmp_dir, scm, dvc):
}
with pytest.raises(NoFieldInDataError):
VegaRenderer(data, dvc.plots.templates).as_json()
VegaRenderer(data, dvc.plots.templates).asdict()
@pytest.mark.parametrize(
@@ -403,53 +394,6 @@ def test_matches(extension, matches):
assert VegaRenderer.matches(data) == matches
def test_find_vega(tmp_dir, dvc):
data = {
"HEAD": {
"data": {
"file.json": {
"data": [{"y": 5}, {"y": 6}],
"props": {"fields": {"y"}},
},
"other_file.jpg": {"data": b"content"},
}
},
"v2": {
"data": {
"file.json": {
"data": [{"y": 3}, {"y": 5}],
"props": {"fields": {"y"}},
},
"other_file.jpg": {"data": b"content2"},
}
},
"v1": {
"data": {
"file.json": {
"data": [{"y": 2}, {"y": 3}],
"props": {"fields": {"y"}},
},
"another.gif": {"data": b"content2"},
}
},
}
plot_content = json.loads(find_vega(dvc, data, "file.json"))
assert plot_content["data"]["values"] == [
{"y": 5, INDEX_FIELD: 0, REVISION_FIELD: "HEAD"},
{"y": 6, INDEX_FIELD: 1, REVISION_FIELD: "HEAD"},
{"y": 3, INDEX_FIELD: 0, REVISION_FIELD: "v2"},
{"y": 5, INDEX_FIELD: 1, REVISION_FIELD: "v2"},
{"y": 2, INDEX_FIELD: 0, REVISION_FIELD: "v1"},
{"y": 3, INDEX_FIELD: 1, REVISION_FIELD: "v1"},
]
assert (
first(plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD
)
assert first(plot_content["layer"])["encoding"]["y"]["field"] == "y"
@pytest.mark.parametrize(
"template_path, target_name",
[
@@ -474,3 +418,20 @@ def test_should_resolve_template(tmp_dir, dvc, template_path, target_name):
assert dvc.plots.templates.get_template(target_name) == os.path.abspath(
template_path
)
def test_as_json(tmp_dir, scm, dvc):
metric = [{"a": 1, "b": 2, "c": 3}, {"a": 2, "b": 3, "c": 4}]
data = {
"workspace": {
"data": {"file.json": {"data": metric, "props": {"fields": {"b"}}}}
}
}
renderer = VegaRenderer(data, dvc.plots.templates)
plot_content = renderer.asdict()
plot_as_json = first(json.loads(renderer.as_json()))
assert plot_as_json["type"] == "vega"
assert plot_as_json["revisions"] == ["workspace"]
assert plot_as_json["content"] == plot_content