1
+ import asyncio
2
+ import json
1
3
import os
2
4
import sys
3
- import json
5
+
6
+ import kaleido
4
7
import plotly .io as pio
8
+
5
9
from convert_b64 import arraysToB64
6
10
11
+
7
12
args = []
8
13
if len (sys .argv ) == 2 :
9
14
args = sys .argv [1 ].split ()
19
24
dirIn = os .path .join (root , 'test' , 'image' , 'mocks' )
20
25
dirOut = os .path .join (root , 'build' , 'test_images' )
21
26
22
- # N.B. equal is the falg to write to baselines not test_images
27
+ # N.B. equal is the flag to write to baselines not test_images
23
28
24
29
if '=' in args :
25
30
args = args [args .index ('=' ) + 1 :]
31
36
print ('output to' , dirOut )
32
37
33
38
mathjax_version = 2
39
+ mathjax = None
34
40
if 'mathjax3' in sys .argv or 'mathjax3=' in sys .argv :
35
41
# until https://github.com/plotly/Kaleido/issues/124 is addressed
36
42
# we are uanble to use local mathjax v3 installed in node_modules
37
43
# for now let's download it from the internet:
38
- pio . kaleido . scope . mathjax = 'https://cdn.jsdelivr.net/npm/[email protected] /es5/tex-svg.js'
44
+ mathjax = 'https://cdn.jsdelivr.net/npm/[email protected] /es5/tex-svg.js'
39
45
mathjax_version = 3
40
46
print ('Kaleido using MathJax v3' )
41
47
52
58
53
59
plotlyjs = plotlyjs_with_virtual_webgl
54
60
55
- pio .kaleido . scope .plotlyjs = plotlyjs
56
- pio .kaleido . scope .topojson = "file://" + os .path .join (root , 'topojson' , 'dist' )
61
+ pio .defaults .plotlyjs = plotlyjs
62
+ pio .defaults .topojson = "file://" + os .path .join (root , 'topojson' , 'dist' )
57
63
pio .templates .default = 'none'
58
64
59
65
ALL_MOCKS = [os .path .splitext (a )[0 ] for a in os .listdir (dirIn ) if a .endswith ('.json' )]
79
85
sys .exit (1 )
80
86
81
87
failed = []
82
- for name in allNames :
83
- outName = name
84
- if mathjax_version == 3 :
85
- outName = 'mathjax3___' + name
86
-
87
- print (outName )
88
-
89
- created = False
90
-
91
- MAX_RETRY = 2 # 1 means retry once
92
- for attempt in range (0 , MAX_RETRY + 1 ) :
93
- with open (os .path .join (dirIn , name + '.json' ), 'r' ) as _in :
94
- fig = json .load (_in )
95
-
96
- width = 700
97
- height = 500
98
- if 'layout' in fig :
99
- layout = fig ['layout' ]
100
- if 'autosize' not in layout or layout ['autosize' ] != True :
101
- if 'width' in layout :
102
- width = layout ['width' ]
103
- if 'height' in layout :
104
- height = layout ['height' ]
105
-
106
- if 'b64' in sys .argv or 'b64=' in sys .argv or 'b64-json' in sys .argv :
107
- newFig = dict ()
108
- arraysToB64 (fig , newFig )
109
- fig = newFig
110
- if 'b64-json' in sys .argv and attempt == 0 : print (json .dumps (fig , indent = 2 ))
111
-
112
- try :
113
- pio .write_image (
114
- fig = fig ,
115
- file = os .path .join (dirOut , outName + '.png' ),
116
- width = width ,
117
- height = height ,
118
- validate = False
119
- )
120
- created = True
121
- except Exception as e :
122
- print (e )
123
- if attempt < MAX_RETRY :
124
- print ('retry' , attempt + 1 , '/' , MAX_RETRY )
125
- else :
126
- failed .append (outName )
127
-
128
- if (created ) : break
129
-
130
- if len (failed ) > 0 :
131
- print ('Failed at :' )
132
- print (failed )
133
- sys .exit (1 )
88
+
89
+ async def make_baselines_async ():
90
+
91
+ kopts = dict (
92
+ plotlyjs = plotlyjs ,
93
+ )
94
+ if mathjax is not None :
95
+ kopts ['mathjax' ] = mathjax
96
+
97
+ async with kaleido .Kaleido (n = 1 , ** kopts ) as k :
98
+ for name in allNames :
99
+ outName = name
100
+ if mathjax_version == 3 :
101
+ outName = 'mathjax3___' + name
102
+
103
+ print (outName )
104
+
105
+ created = False
106
+
107
+ MAX_RETRY = 2 # 1 means retry once
108
+ for attempt in range (0 , MAX_RETRY + 1 ) :
109
+ with open (os .path .join (dirIn , name + '.json' ), 'r' ) as _in :
110
+ fig = json .load (_in )
111
+
112
+ width = 700
113
+ height = 500
114
+ if 'layout' in fig :
115
+ layout = fig ['layout' ]
116
+ if 'autosize' not in layout or layout ['autosize' ] != True :
117
+ if 'width' in layout :
118
+ width = layout ['width' ]
119
+ if 'height' in layout :
120
+ height = layout ['height' ]
121
+
122
+ if 'b64' in sys .argv or 'b64=' in sys .argv or 'b64-json' in sys .argv :
123
+ newFig = dict ()
124
+ arraysToB64 (fig , newFig )
125
+ fig = newFig
126
+ if 'b64-json' in sys .argv and attempt == 0 : print (json .dumps (fig , indent = 2 ))
127
+
128
+ try :
129
+ bytes = await k .calc_fig (
130
+ fig ,
131
+ path = None ,
132
+ opts = dict (
133
+ format = "png" ,
134
+ width = width ,
135
+ height = height ,
136
+ ),
137
+ )
138
+ filename = os .path .join (dirOut , outName + '.png' )
139
+ with open (filename , "wb" ) as f :
140
+ f .write (bytes )
141
+ created = True
142
+ except Exception as e :
143
+ print (e )
144
+ if attempt < MAX_RETRY :
145
+ print ('retry' , attempt + 1 , '/' , MAX_RETRY )
146
+ else :
147
+ failed .append (outName )
148
+
149
+ if (created ): break
150
+
151
+ if len (failed ) > 0 :
152
+ print ('Failed at :' )
153
+ print (failed )
154
+ sys .exit (1 )
155
+
156
+
157
+ if __name__ == "__main__" :
158
+ asyncio .run (make_baselines_async ())
0 commit comments