Skip to content

Commit

Permalink
Order image per step plots
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Sep 14, 2023
1 parent cad3a3f commit d81a40e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/dvc_render/html.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional

from dvc_render.image import ImageRenderer

from .exceptions import DvcRenderException

if TYPE_CHECKING:
Expand Down Expand Up @@ -76,6 +79,25 @@ def embed(self) -> str:
return self.template


def _order_image_per_step(renderer: "Renderer") -> tuple:
is_image_renderer = isinstance(renderer, ImageRenderer)

if not is_image_renderer:
return (is_image_renderer, None, None, None)

path = renderer.name
basename = os.path.basename(path)
filename = os.path.splitext(basename)[0]
image_number = int(filename) if filename.isdigit() else None

return (
is_image_renderer,
os.path.dirname(path),
image_number,
basename,
)


def render_html(
renderers: List["Renderer"],
output_file: "StrPath",
Expand All @@ -94,7 +116,12 @@ def render_html(

document = HTML(page_html, refresh_seconds=refresh_seconds)

for renderer in renderers:
sorted_renderers = sorted(
renderers,
key=_order_image_per_step,
)

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

Expand Down
51 changes: 50 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# pylint: disable=missing-function-docstring, R0801
import os

import pytest

from dvc_render.html import HTML, PAGE_HTML, MissingPlaceholderError, render_html
from dvc_render.html import (
HTML,
PAGE_HTML,
MissingPlaceholderError,
_order_image_per_step,
render_html,
)
from dvc_render.image import ImageRenderer
from dvc_render.vega import VegaRenderer

CUSTOM_PAGE_HTML = """<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -83,6 +93,45 @@ def test_render_html_with_custom_template(mocker, tmp_dir):
assert output_file.read_text() == CUSTOM_PAGE_HTML.format(plot_divs="")


def test_order_image_per_step():
image_per_step_dir = "dvclive"
other_image_dir = "static"

def create_renderer(filename: str) -> ImageRenderer:
return ImageRenderer(
[
{
"filename": filename,
"rev": "workspace",
"src": filename,
}
],
filename,
)

r1 = VegaRenderer([], "dvc.yaml::Loss")
r2 = VegaRenderer([], "dvc.yaml::Accuracy")
r3 = create_renderer(os.path.join(image_per_step_dir, "0.jpg"))
r4 = create_renderer(os.path.join(image_per_step_dir, "1.jpg"))
r5 = create_renderer(os.path.join(image_per_step_dir, "2.jpg"))
r6 = create_renderer(os.path.join(image_per_step_dir, "10.jpg"))
r7 = create_renderer(os.path.join(other_image_dir, "a_file.jpg"))
r8 = create_renderer(os.path.join(other_image_dir, "z_file.jpg"))

renderers = [r7, r3, r5, r8, r1, r6, r4, r2]

assert sorted(renderers, key=_order_image_per_step) == [
r1,
r2,
r3,
r4,
r5,
r6,
r7,
r8,
]


def test_no_placeholder():
template = "<head></head><body></body>"

Expand Down

0 comments on commit d81a40e

Please sign in to comment.