Skip to content

Commit

Permalink
image: resolve abolute path from src
Browse files Browse the repository at this point in the history
  • Loading branch information
pared committed Apr 29, 2022
1 parent 369396f commit b1b1808
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/dvc_render/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
self.properties = properties

@abc.abstractmethod
def partial_html(self) -> str:
def partial_html(self, **kwargs) -> str:
"""
Us this method to generate HTML content,
to fill `{partial}` inside self.DIV.
Expand All @@ -51,9 +51,9 @@ def remove_special_chars(string: str) -> str:
{ord(c): "_" for c in r"!@#$%^&*()[]{};,<>?\/:.|`~=_+"}
)

def generate_html(self) -> str:
def generate_html(self, **kwargs) -> str:
"Return `DIV` formatted with `partial_html`."
partial = self.partial_html()
partial = self.partial_html(**kwargs)
if partial:

div_id = self.remove_special_chars(self.name)
Expand Down
4 changes: 3 additions & 1 deletion src/dvc_render/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def render_html(

for renderer in renderers:
document.with_scripts(renderer.SCRIPTS)
document.with_element(renderer.generate_html())
document.with_element(
renderer.generate_html(output_dir=output_path.parent)
)

output_path.write_text(document.embed(), encoding="utf8")

Expand Down
15 changes: 13 additions & 2 deletions src/dvc_render/image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from .base import Renderer


Expand All @@ -20,17 +22,26 @@ class ImageRenderer(Renderer):

EXTENSIONS = {".jpg", ".jpeg", ".gif", ".png"}

def partial_html(self) -> str:
def partial_html(self, output_dir=None, **kwargs) -> str:
div_content = []
for datapoint in self.datapoints:
src = datapoint[self.SRC_FIELD]

if (
not src.startswith("data:image;base64")
and os.path.isabs(src)
and output_dir
):
src = os.path.relpath(src, output_dir)

div_content.append(
f"""
<div
style="border:1px dotted black;margin:2px;display:
inline-block;
overflow:hidden;margin-left:8px;">
<p>{datapoint[self.TITLE_FIELD]}</p>
<img src="{datapoint[self.SRC_FIELD]}">
<img src="{src}">
</div>
"""
)
Expand Down
2 changes: 1 addition & 1 deletion src/dvc_render/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
self.name = name
self.fill_value = fill_value

def partial_html(self) -> str:
def partial_html(self, **kwargs) -> str:
return json.dumps(self._get_plotly_data())

def _get_plotly_data(self):
Expand Down
2 changes: 1 addition & 1 deletion src/dvc_render/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ def get_filled_template(

return content

def partial_html(self) -> str:
def partial_html(self, **kwargs) -> str:
return self.get_filled_template()
55 changes: 49 additions & 6 deletions tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

from dvc_render.image import ImageRenderer
Expand All @@ -23,21 +25,62 @@ def test_matches(extension, matches):
assert ImageRenderer.matches(filename, {}) == matches


def test_render(tmp_dir):
tmp_dir.gen("workspace_file.jpg", b"content")
@pytest.mark.parametrize("output_dir", [None, "/output/dir"])
@pytest.mark.parametrize(
"src", ["relpath.jpg", "data:image;base64,encoded_image"]
)
def test_render(output_dir, src):
datapoints = [
{
"filename": "file.jpg",
"rev": "workspace",
"src": src,
}
]

html = ImageRenderer(datapoints, "file.jpg").generate_html(
output_dir=output_dir
)

assert "<p>file.jpg</p>" in html
assert f'<img src="{src}">' in html


@pytest.mark.parametrize(
"output_dir,img_path,expected_path",
[
(
os.path.join("output", "path"),
os.path.join("output", "path", "with", "static"),
os.path.join("with", "static"),
),
(
os.path.join("output", "one", "path"),
os.path.join("output", "second", "path"),
os.path.join("..", "..", "second", "path"),
),
],
)
def test_render_evaluate_relative_path(
tmp_dir, output_dir, img_path, expected_path
):
abs_output_dir = tmp_dir / output_dir
abs_img_path = tmp_dir / img_path

datapoints = [
{
"filename": "file.jpg",
"rev": "workspace",
"src": "workspace_file.jpg",
"src": str(abs_img_path),
}
]
filename = "file.jpg"

html = ImageRenderer(datapoints, filename).generate_html()
html = ImageRenderer(datapoints, "file.jpg").generate_html(
output_dir=abs_output_dir
)

assert "<p>file.jpg</p>" in html
assert '<img src="workspace_file.jpg">' in html
assert f'<img src="{expected_path}">' in html


def test_render_empty():
Expand Down

0 comments on commit b1b1808

Please sign in to comment.