Skip to content

Commit b1f16e0

Browse files
committed
use single Kaleido instance for making baselines (faster performance)
1 parent a426afa commit b1f16e0

File tree

1 file changed

+79
-55
lines changed

1 file changed

+79
-55
lines changed

test/image/make_baseline.py

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import asyncio
12
import os
23
import sys
34
import json
5+
46
import plotly.io as pio
7+
import kaleido
8+
59
from convert_b64 import arraysToB64
610

11+
712
args = []
813
if len(sys.argv) == 2 :
914
args = sys.argv[1].split()
@@ -31,11 +36,12 @@
3136
print('output to', dirOut)
3237

3338
mathjax_version = 2
39+
mathjax = None
3440
if '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.defaults.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

@@ -52,8 +58,6 @@
5258

5359
plotlyjs = plotlyjs_with_virtual_webgl
5460

55-
pio.defaults.plotlyjs = plotlyjs
56-
5761
pio.templates.default = 'none'
5862

5963
ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')]
@@ -71,6 +75,7 @@
7175
'map_predefined-styles2', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
7276
'grid_subplot_types', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
7377
'map_fonts-supported-metropolis', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
78+
'map_fonts-supported-metropolis-italic', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
7479
'map_fonts-supported-metropolis-weight', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
7580
'map_fonts-supported-open-sans-weight', # Temporarily blacklist: fails with Kaleido v1.0.0rc14
7681
]
@@ -81,55 +86,74 @@
8186
sys.exit(1)
8287

8388
failed = []
84-
for name in allNames :
85-
outName = name
86-
if mathjax_version == 3 :
87-
outName = 'mathjax3___' + name
88-
89-
print(outName)
90-
91-
created = False
92-
93-
MAX_RETRY = 2 # 1 means retry once
94-
for attempt in range(0, MAX_RETRY + 1) :
95-
with open(os.path.join(dirIn, name + '.json'), 'r') as _in :
96-
fig = json.load(_in)
97-
98-
width = 700
99-
height = 500
100-
if 'layout' in fig :
101-
layout = fig['layout']
102-
if 'autosize' not in layout or layout['autosize'] != True :
103-
if 'width' in layout :
104-
width = layout['width']
105-
if 'height' in layout :
106-
height = layout['height']
107-
108-
if 'b64' in sys.argv or 'b64=' in sys.argv or 'b64-json' in sys.argv :
109-
newFig = dict()
110-
arraysToB64(fig, newFig)
111-
fig = newFig
112-
if 'b64-json' in sys.argv and attempt == 0 : print(json.dumps(fig, indent = 2))
113-
114-
try :
115-
pio.write_image(
116-
fig=fig,
117-
file=os.path.join(dirOut, outName + '.png'),
118-
width=width,
119-
height=height,
120-
validate=False
121-
)
122-
created = True
123-
except Exception as e :
124-
print(e)
125-
if attempt < MAX_RETRY :
126-
print('retry', attempt + 1, '/', MAX_RETRY)
127-
else :
128-
failed.append(outName)
129-
130-
if(created) : break
131-
132-
if len(failed) > 0 :
133-
print('Failed at :')
134-
print(failed)
135-
sys.exit(1)
89+
90+
async def make_baselines_async():
91+
92+
kopts = dict(
93+
plotlyjs=plotlyjs,
94+
)
95+
if mathjax is not None:
96+
kopts['mathjax'] = mathjax
97+
98+
async with kaleido.Kaleido(n=1, **kopts) as k:
99+
for name in allNames:
100+
outName = name
101+
if mathjax_version == 3:
102+
outName = 'mathjax3___' + name
103+
104+
print(outName)
105+
106+
created = False
107+
108+
MAX_RETRY = 2 # 1 means retry once
109+
for attempt in range(0, MAX_RETRY + 1) :
110+
with open(os.path.join(dirIn, name + '.json'), 'r') as _in :
111+
fig = json.load(_in)
112+
113+
width = 700
114+
height = 500
115+
if 'layout' in fig :
116+
layout = fig['layout']
117+
if 'autosize' not in layout or layout['autosize'] != True :
118+
if 'width' in layout :
119+
width = layout['width']
120+
if 'height' in layout :
121+
height = layout['height']
122+
123+
if 'b64' in sys.argv or 'b64=' in sys.argv or 'b64-json' in sys.argv :
124+
newFig = dict()
125+
arraysToB64(fig, newFig)
126+
fig = newFig
127+
if 'b64-json' in sys.argv and attempt == 0 : print(json.dumps(fig, indent = 2))
128+
129+
try:
130+
bytes = await k.calc_fig(
131+
fig,
132+
path=None,
133+
opts=dict(
134+
format="png",
135+
width=width,
136+
height=height,
137+
),
138+
)
139+
filename = os.path.join(dirOut, outName + '.png')
140+
with open(filename, "wb") as f:
141+
f.write(bytes)
142+
created = True
143+
except Exception as e:
144+
print(e)
145+
if attempt < MAX_RETRY :
146+
print('retry', attempt + 1, '/', MAX_RETRY)
147+
else :
148+
failed.append(outName)
149+
150+
if(created): break
151+
152+
if len(failed) > 0 :
153+
print('Failed at :')
154+
print(failed)
155+
sys.exit(1)
156+
157+
158+
if __name__ == "__main__":
159+
asyncio.run(make_baselines_async())

0 commit comments

Comments
 (0)