-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathmake_baseline.py
136 lines (110 loc) · 3.95 KB
/
make_baseline.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import json
import plotly.io as pio
args = []
if len(sys.argv) == 2 :
args = sys.argv[1].split()
elif len(sys.argv) > 1 :
args = sys.argv
root = os.getcwd()
dirIn = os.path.join(root, 'test', 'image', 'mocks')
dirOut = os.path.join(root, 'build', 'test_images')
# N.B. equal is the falg to write to baselines not test_images
if '=' in args :
args = args[args.index('=') + 1:]
dirOut = os.path.join(root, 'test', 'image', 'baselines')
if 'mathjax3=' in sys.argv :
dirOut = os.path.join(root, 'test', 'image', 'baselines')
print('output to', dirOut)
mathjax_version = 2
if 'mathjax3' in sys.argv or 'mathjax3=' in sys.argv :
# until https://github.com/plotly/Kaleido/issues/124 is addressed
# we are uanble to use local mathjax v3 installed in node_modules
# for now let's download it from the internet:
pio.kaleido.scope.mathjax = 'https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js'
mathjax_version = 3
print('Kaleido using MathJax v3')
pio.templates.default = 'none'
pio.kaleido.scope.plotlyjs = os.path.join(root, 'build', 'plotly.js')
_credentials = open(os.path.join(root, 'build', 'credentials.json'), 'r')
pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN']
_credentials.close()
ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')]
ALL_MOCKS.sort()
if len(args) > 0 :
allNames = [a for a in args if a in ALL_MOCKS]
else :
allNames = ALL_MOCKS
# gl2d pointcloud and other non-regl gl2d mock(s)
# must be tested in certain order to work on CircleCI;
#
# gl-shader appears to conflict with regl.
# We suspect that the lone gl context on CircleCI is
# having issues with dealing with the two different
# program binding algorithm.
#
# The problem will be solved by switching all our
# WebGL-based trace types to regl.
#
# More info here:
# https://github.com/plotly/plotly.js/pull/1037
LAST = [
'gl2d_pointcloud-basic',
'gl2d_heatmapgl',
'gl2d_heatmapgl_discrete'
]
HAD = [item in allNames for item in LAST]
allNames = [a for a in allNames if a not in LAST]
allNames += [item for item, had_item in zip(LAST, HAD) if had_item]
# unable to generate baselines for the following mocks
blacklist = [
'mapbox_density0-legend',
'mapbox_osm-style',
'mapbox_stamen-style', # Could pass by setting mapboxAccessToken to a stadiamaps.com token
'mapbox_custom-style' # Figure out why needed this in https://github.com/plotly/plotly.js/pull/6610
]
allNames = [a for a in allNames if a not in blacklist]
if len(allNames) == 0 :
print('error: Nothing to create!')
sys.exit(1)
failed = []
for name in allNames :
outName = name
if mathjax_version == 3 :
outName = 'mathjax3___' + name
print(outName)
created = False
MAX_RETRY = 2 # 1 means retry once
for attempt in range(0, MAX_RETRY + 1) :
with open(os.path.join(dirIn, name + '.json'), 'r') as _in :
fig = json.load(_in)
width = 700
height = 500
if 'layout' in fig :
layout = fig['layout']
if 'autosize' not in layout or layout['autosize'] != True :
if 'width' in layout :
width = layout['width']
if 'height' in layout :
height = layout['height']
try :
pio.write_image(
fig=fig,
file=os.path.join(dirOut, outName + '.png'),
width=width,
height=height,
validate=False
)
created = True
except Exception as e :
print(e)
if attempt < MAX_RETRY :
print('retry', attempt + 1, '/', MAX_RETRY)
else :
failed.append(outName)
if(created) : break
if len(failed) > 0 :
print('Failed at :')
print(failed)
sys.exit(1)