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

Add initial version of PlotlyRenderer. #88

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
plotly: Expose properties in layout.
  • Loading branch information
daavoo committed Sep 5, 2023
commit fefb4406571fc712f46fa7affdddf57141974e49
4 changes: 2 additions & 2 deletions src/dvc_render/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
from .html import render_html # noqa: F401
from .image import ImageRenderer
from .plotly import ParallelCoordinatesRenderer # noqa: F401
from .plotly import PlotlyRenderer # noqa: F401
from .vega import VegaRenderer
from .vega_templates import TEMPLATES # noqa: F401

RENDERERS = [ImageRenderer, VegaRenderer]
RENDERERS = [ImageRenderer, PlotlyRenderer, VegaRenderer]
15 changes: 13 additions & 2 deletions src/dvc_render/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,22 @@ def convert_datapoints(self, datapoints):
value = datapoint[self.properties[axis]]
traces[revision][axis].append(value)
traces = list(traces.values())
template = self.properties["template"]

template = self.properties.pop("template", "linear")
for trace in traces:
trace["type"] = template

return {"data": traces}
layout = {
"title": self.properties.get("title", ""),
"xaxis": {
"title": self.properties.get("x_label", self.properties.get("x"))
},
"yaxis": {
"title": self.properties.get("y_label", self.properties.get("y"))
},
}

return {"data": traces, "layout": layout}

def partial_html(self, **kwargs) -> str:
return json.dumps(self.convert_datapoints(self.datapoints))
28 changes: 15 additions & 13 deletions tests/test_plotly.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import json

import pytest

from dvc_render.plotly import PlotlyRenderer

# pylint: disable=missing-function-docstring,
Expand All @@ -16,19 +14,21 @@ def test_plotly_partial_html():
{"first_val": 7, "second_val": 8, "rev": "HEAD"},
]

plot_content = json.loads(
PlotlyRenderer(datapoints, "foo", **props).partial_html()
)
plot_content = json.loads(PlotlyRenderer(datapoints, "foo", **props).partial_html())

assert plot_content == {
"data": [
{"x": [1, 3], "y": [2, 4], "type": "scatter", "name": "workspace"},
{"x": [5, 7], "y": [6, 8], "type": "scatter", "name": "HEAD"},
]
],
"layout": {
"title": "",
"xaxis": {"title": "first_val"},
"yaxis": {"title": "second_val"},
},
}


@pytest.mark.xfail(reason="TODO")
def test_plotly_layout():
props = {"x": "first_val", "y": "second_val", "title": "TITLE"}
datapoints = [
Expand All @@ -38,14 +38,16 @@ def test_plotly_layout():
{"first_val": 7, "second_val": 8, "rev": "HEAD"},
]

plot_content = json.loads(
PlotlyRenderer(datapoints, "foo", **props).partial_html()
)
plot_content = json.loads(PlotlyRenderer(datapoints, "foo", **props).partial_html())

assert plot_content == {
"data": [
{"x": [1, 3], "y": [2, 4], "type": "scatter", "name": "workspace"},
{"x": [5, 7], "y": [6, 8], "type": "scatter", "name": "HEAD"},
{"x": [1, 3], "y": [2, 4], "type": "linear", "name": "workspace"},
{"x": [5, 7], "y": [6, 8], "type": "linear", "name": "HEAD"},
],
"layout": {"title": "TITLE"},
"layout": {
"title": "TITLE",
"xaxis": {"title": "first_val"},
"yaxis": {"title": "second_val"},
},
}