-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly_chart.py
More file actions
92 lines (76 loc) · 2.46 KB
/
Copy pathplotly_chart.py
File metadata and controls
92 lines (76 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import json
import plotly
import plotly.graph_objects as go
import hyperdiv as hd
class PlotlyFigDef(hd.HyperdivType):
def parse(self, value):
if not isinstance(value, go.Figure):
raise ValueError(
f"Expected a `plotly.graph_objects.Figure` but got {type(value)}"
)
return value
def render(self, value):
return json.loads(plotly.io.to_json(value))
class DictDef(hd.HyperdivType):
def parse(self, value):
if not isinstance(value, dict):
raise ValueError(f"Expected a `dict` but got {type(value)}")
return value
PlotlyFig = PlotlyFigDef()
Dict = DictDef()
class plotly_chart(hd.Plugin):
_assets_root = os.path.join(os.path.dirname(__file__), "assets")
_assets = [
"plotly.css",
"plotly-2.29.1.min.js",
"hyperdiv-plotly-plugin.js",
]
fig = hd.Prop(PlotlyFig)
browser_config = hd.Prop(Dict)
def __init__(self, fig, browser_config=None, **kwargs):
super().__init__(
fig=fig,
browser_config=(browser_config or self.get_default_browser_config()),
**kwargs,
)
@staticmethod
def get_default_browser_config(**overrides):
return (
dict(
responsive=True,
displayModeBar=False,
modeBarButtonsToRemove=["lasso2d", "select2d"],
)
| overrides
)
@staticmethod
def get_default_layout(**overrides):
theme = hd.theme()
if theme.is_dark:
label_color = "rgba(255, 255, 255, 0.5)"
color = "rgba(255, 255, 255, 0.1)"
else:
label_color = "rgba(0, 0, 0, 0.5)"
color = "rgba(0, 0, 0, 0.1)"
return (
dict(
autosize=True,
margin=dict(l=0, r=0, t=0, b=0),
plot_bgcolor="rgba(0, 0, 0, 0)", # Transparent background
paper_bgcolor="rgba(0, 0, 0, 0)", # Transparent paper
font_color=label_color, # Text color
xaxis=dict(
color=label_color,
gridcolor=color, # Gridline color
linecolor=color,
),
yaxis=dict(
color=label_color,
gridcolor=color,
linecolor=color,
zeroline=False,
),
)
| overrides
)