diff --git a/dvc/command/live.py b/dvc/command/live.py index f0488a93d..2e97d9fb6 100644 --- a/dvc/command/live.py +++ b/dvc/command/live.py @@ -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 diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 84132d7e3..47fba416f 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -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", diff --git a/dvc/render/base.py b/dvc/render/base.py index 60506f946..75f25d09c 100644 --- a/dvc/render/base.py +++ b/dvc/render/base.py @@ -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}" diff --git a/dvc/render/image.py b/dvc/render/image.py index 1ef2e45f2..f7db58949 100644 --- a/dvc/render/image.py +++ b/dvc/render/image.py @@ -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 = """
{title}
{self.filename}
") 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): diff --git a/dvc/render/utils.py b/dvc/render/utils.py index cc6d4e1bb..dd0e2db9b 100644 --- a/dvc/render/utils.py +++ b/dvc/render/utils.py @@ -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 diff --git a/dvc/render/vega.py b/dvc/render/vega.py index fb8930cee..cd898bfbc 100644 --- a/dvc/render/vega.py +++ b/dvc/render/vega.py @@ -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 = """