Exemple #1
0
def test_match_renderers_with_out(tmp_dir, mocker):
    from dvc import render

    image_encode = mocker.spy(render.image_converter.ImageConverter,
                              "_encode_image")
    image_write = mocker.spy(render.image_converter.ImageConverter,
                             "_write_image")

    error = FileNotFoundError()
    data = {
        "v2": {
            "data": {
                "file.json": {
                    "data": [{
                        "y": 2
                    }, {
                        "y": 3
                    }],
                    "props": {}
                },
                "other_file.jpg": {
                    "data": b"content"
                },
            }
        },
        "v1": {
            "data": {
                "file.json": {
                    "data": [{
                        "y": 4
                    }, {
                        "y": 5
                    }],
                    "props": {}
                }
            }
        },
        "workspace": {
            "data": {
                "file.json": {
                    "error": error,
                    "props": {}
                },
                "other_file.jpg": {
                    "data": b"content2"
                },
            }
        },
    }

    match_renderers(data, out=tmp_dir / "foo")

    image_encode.assert_not_called()
    image_write.assert_called()

    assert (tmp_dir / "foo" / "v2_other_file.jpg").read_bytes() == b"content"
    assert (tmp_dir / "foo" /
            "workspace_other_file.jpg").read_bytes() == b"content2"
Exemple #2
0
def test_match_renderers_no_out(mocker):
    from dvc import render

    vega_convert = mocker.spy(render.vega_converter.VegaConverter, "convert")
    image_convert = mocker.spy(render.image_converter.ImageConverter,
                               "convert")
    image_encode = mocker.spy(render.image_converter.ImageConverter,
                              "_encode_image")
    image_write = mocker.spy(render.image_converter.ImageConverter,
                             "_write_image")

    error = FileNotFoundError()
    data = {
        "v2": {
            "data": {
                "file.json": {
                    "data": [{
                        "y": 2
                    }, {
                        "y": 3
                    }],
                    "props": {}
                },
                "other_file.jpg": {
                    "data": b"content"
                },
            }
        },
        "v1": {
            "data": {
                "file.json": {
                    "data": [{
                        "y": 4
                    }, {
                        "y": 5
                    }],
                    "props": {}
                }
            }
        },
        "workspace": {
            "data": {
                "file.json": {
                    "error": error,
                    "props": {}
                },
                "other_file.jpg": {
                    "data": b"content2"
                },
            }
        },
    }

    renderers = match_renderers(data)

    assert {r.TYPE for r in renderers} == {"vega", "image"}
    vega_convert.assert_called()
    image_convert.assert_called()
    image_encode.assert_called()
    image_write.assert_not_called()
Exemple #3
0
def test_match_renderers_template_dir(mocker):
    from dvc_render import vega

    vega_render = mocker.spy(vega.VegaRenderer, "__init__")
    data = {
        "v1": {
            "data": {
                "file.json": {
                    "data": [{
                        "y": 4
                    }, {
                        "y": 5
                    }],
                    "props": {}
                }
            }
        },
    }

    match_renderers(data, templates_dir="foo")

    assert vega_render.call_args[1]["template_dir"] == "foo"
Exemple #4
0
    def run(self):
        from pathlib import Path

        from dvc.render.match import match_renderers
        from dvc_render import render_html

        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
            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())

            if not plots_data:
                ui.error_write("No plots were loaded, "
                               "visualization file will not be created.")

            out: str = self.args.out or self.repo.config.get("plots", {}).get(
                "out_dir", "dvc_plots")

            renderers_out = (out if self.args.json else os.path.join(
                out, "static"))
            renderers = match_renderers(
                plots_data=plots_data,
                out=renderers_out,
                templates_dir=self.repo.plots.templates_dir,
            )

            if self.args.show_vega:
                renderer = first(filter(lambda r: r.TYPE == "vega", renderers))
                if renderer:
                    ui.write_json(json.loads(renderer.get_filled_template()))
                return 0
            if self.args.json:
                _show_json(renderers, self.args.split)
                return 0

            html_template_path = self.args.html_template
            if not html_template_path:
                html_template_path = self.repo.config.get("plots", {}).get(
                    "html_template", None)
                if html_template_path and not os.path.isabs(
                        html_template_path):
                    html_template_path = os.path.join(self.repo.dvc_dir,
                                                      html_template_path)

            output_file: Path = (Path.cwd() / out).resolve() / "index.html"

            render_html(
                renderers=renderers,
                output_file=output_file,
                template_path=html_template_path,
            )

            ui.write(output_file.as_uri())
            auto_open = self.repo.config["plots"].get("auto_open", False)
            if self.args.open or auto_open:
                if not auto_open:
                    ui.write("To enable auto opening, you can run:\n"
                             "\n"
                             "\tdvc config plots.auto_open true")
                return ui.open_browser(output_file)

            return 0

        except DvcException:
            logger.exception("")
            return 1