1+ import asyncio
2+ import json
13import os
24import sys
3- import json
5+
6+ import kaleido
47import plotly .io as pio
8+
59from convert_b64 import arraysToB64
610
11+
712args = []
813if len (sys .argv ) == 2 :
914 args = sys .argv [1 ].split ()
1924dirIn = os .path .join (root , 'test' , 'image' , 'mocks' )
2025dirOut = os .path .join (root , 'build' , 'test_images' )
2126
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
2328
2429if '=' in args :
2530 args = args [args .index ('=' ) + 1 :]
3136print ('output to' , dirOut )
3237
3338mathjax_version = 2
39+ mathjax = None
3440if 'mathjax3' in sys .argv or 'mathjax3=' in sys .argv :
3541 # until https://github.com/plotly/Kaleido/issues/124 is addressed
3642 # we are uanble to use local mathjax v3 installed in node_modules
3743 # 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' 3945 mathjax_version = 3
4046 print ('Kaleido using MathJax v3' )
4147
5258
5359 plotlyjs = plotlyjs_with_virtual_webgl
5460
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' )
5763pio .templates .default = 'none'
5864
5965ALL_MOCKS = [os .path .splitext (a )[0 ] for a in os .listdir (dirIn ) if a .endswith ('.json' )]
7985 sys .exit (1 )
8086
8187failed = []
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