Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image: resolve abolute path from src #39

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, html_path=None) -> str:
"Return `DIV` formatted with `partial_html`."
partial = self.partial_html()
partial = self.partial_html(html_path=html_path)
if partial:

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

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

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, html_path=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 html_path
):
src = os.path.relpath(src, os.path.dirname(html_path))

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()
53 changes: 47 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,60 @@ 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("html_path", [None, "/output/dir/index.html"])
@pytest.mark.parametrize(
"src", ["relpath.jpg", "data:image;base64,encoded_image"]
)
def test_render(html_path, src):
datapoints = [
{
"filename": "file.jpg",
"rev": "workspace",
"src": src,
}
]

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

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


@pytest.mark.parametrize(
"html_path,img_path,expected_path",
[
(
os.path.join("output", "path", "index.html"),
os.path.join("output", "path", "with", "static", "file.jpg"),
os.path.join("with", "static", "file.jpg"),
),
(
os.path.join("output", "one", "path", "index.html"),
os.path.join("output", "second", "path", "file.jpg"),
os.path.join("..", "..", "second", "path", "file.jpg"),
),
],
)
def test_render_evaluate_path(tmp_dir, html_path, img_path, expected_path):
abs_html_path = tmp_dir / html_path
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(
html_path=abs_html_path
)

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