def run(self): from pathlib import Path if self.args.show_vega: if not self.args.targets: logger.error("please specify a target for `--show-vega`") return 1 if len(self.args.targets) > 1: logger.error( "you can only specify one target for `--show-vega`" ) return 1 try: plots_data = self._func( targets=self.args.targets, props=self._props() ) if not plots_data: ui.error_write( "No plots were loaded, " "visualization file will not be created." ) 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) return 0 rel: str = self.args.out or "dvc_plots" path: Path = (Path.cwd() / rel).resolve() index_path = render( self.repo, plots_data, path=path, html_template_path=self.args.html_template, ) assert index_path.is_absolute() url = index_path.as_uri() ui.write(url) if self.args.open: import webbrowser opened = webbrowser.open(index_path) if not opened: ui.error_write( "Failed to open. Please try opening it manually." ) return 1 return 0 except DvcException: logger.exception("") return 1
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 plot_content["encoding"]["x"]["field"] == INDEX_FIELD assert plot_content["encoding"]["y"]["field"] == "y"
def test_render(tmp_dir, dvc): data = { "HEAD": { "data": { "file.json": { "data": [{ "y": 5 }, { "y": 6 }], "props": { "fields": {"y"} }, }, os.path.join("sub", "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": { "some.csv": { "data": [{ "y": 2 }, { "y": 3 }], "props": { "fields": {"y"} }, }, "another.gif": { "data": b"content3" }, } }, } render(dvc, data, path=os.path.join("results", "dir")) page_path = tmp_dir / "results" / "dir" index_path = page_path / "index.html" assert index_path.is_file() assert_website_has_image(page_path, "HEAD", os.path.join("sub", "other_file.jpg"), b"content") 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") assert file_vega in index_content.strip() assert some_vega in index_content.strip()