forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and use separate package for rendering logic. Closes iterative#6944
- Loading branch information
Showing
43 changed files
with
755 additions
and
2,726 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .image import ImageRenderer | ||
from .vega import VegaRenderer | ||
|
||
RENDERERS = [ImageRenderer, VegaRenderer] | ||
INDEX_FIELD = "step" | ||
REVISION_FIELD = "rev" | ||
REVISIONS_KEY = "revisions" | ||
TYPE_KEY = "type" | ||
SRC_FIELD = "src" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
from collections import defaultdict | ||
from typing import Dict, List, Union | ||
|
||
from dvc.render import REVISION_FIELD, REVISIONS_KEY, SRC_FIELD, TYPE_KEY | ||
from dvc.render.image_converter import ImageConverter | ||
from dvc.render.vega_converter import VegaConverter | ||
|
||
|
||
def get_converter( | ||
renderer_class, props | ||
) -> Union[VegaConverter, ImageConverter]: | ||
from dvc_render import ImageRenderer, VegaRenderer | ||
|
||
if renderer_class.TYPE == VegaRenderer.TYPE: | ||
return VegaConverter(props) | ||
if renderer_class.TYPE == ImageRenderer.TYPE: | ||
return ImageConverter(props) | ||
|
||
raise ValueError(f"Invalid renderer class {renderer_class}") | ||
|
||
|
||
def to_datapoints(renderer_class, data: Dict, props: Dict): | ||
converter = get_converter(renderer_class, props) | ||
datapoints: List[Dict] = [] | ||
for revision, rev_data in data.items(): | ||
for filename, file_data in rev_data.get("data", {}).items(): | ||
if "data" in file_data: | ||
processed, final_props = converter.convert( | ||
revision, filename, file_data.get("data") | ||
) | ||
datapoints.extend(processed) | ||
return datapoints, final_props | ||
|
||
|
||
def _group_by_rev(datapoints): | ||
grouped = defaultdict(list) | ||
for datapoint in datapoints: | ||
rev = datapoint.pop(REVISION_FIELD) | ||
grouped[rev].append(datapoint) | ||
return dict(grouped) | ||
|
||
|
||
def to_json(renderer, split: bool = False) -> List[Dict]: | ||
if renderer.TYPE == "vega": | ||
grouped = _group_by_rev(renderer.datapoints) | ||
if split: | ||
content = renderer.get_filled_template(skip_anchors=["data"]) | ||
else: | ||
content = renderer.get_filled_template() | ||
return [ | ||
{ | ||
TYPE_KEY: renderer.TYPE, | ||
REVISIONS_KEY: sorted(grouped.keys()), | ||
"content": json.loads(content), | ||
"datapoints": grouped, | ||
} | ||
] | ||
if renderer.TYPE == "image": | ||
return [ | ||
{ | ||
TYPE_KEY: renderer.TYPE, | ||
REVISIONS_KEY: datapoint.get(REVISION_FIELD), | ||
"url": datapoint.get(SRC_FIELD), | ||
} | ||
for datapoint in renderer.datapoints | ||
] | ||
raise ValueError(f"Invalid renderer: {renderer.TYPE}") |
Oops, something went wrong.