Skip to content

Commit 87df186

Browse files
committed
use kaleido v1 for image tests
1 parent 7d40224 commit 87df186

File tree

3 files changed

+118
-63
lines changed

3 files changed

+118
-63
lines changed

.circleci/config.yml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,15 @@ jobs:
217217
make-baselines-virtual-webgl:
218218
parallelism: 8
219219
docker:
220-
- image: cimg/python:3.12.11
220+
- image: cimg/python:3.12.11-browsers
221221
working_directory: ~/plotly.js
222222
steps:
223+
- run: sudo apt-get update
224+
- browser-tools/install-browser-tools:
225+
install-firefox: false
226+
install-geckodriver: false
227+
install-chrome: true
228+
chrome-version: "132.0.6834.110"
223229
- attach_workspace:
224230
at: ~/
225231
- run: sudo apt-get update
@@ -236,9 +242,15 @@ jobs:
236242

237243
make-baselines-mathjax3:
238244
docker:
239-
- image: cimg/python:3.12.11
245+
- image: cimg/python:3.12.11-browsers
240246
working_directory: ~/plotly.js
241247
steps:
248+
- run: sudo apt-get update
249+
- browser-tools/install-browser-tools:
250+
install-firefox: false
251+
install-geckodriver: false
252+
install-chrome: true
253+
chrome-version: "132.0.6834.110"
242254
- attach_workspace:
243255
at: ~/
244256
- run: sudo apt-get update
@@ -256,9 +268,15 @@ jobs:
256268
make-baselines:
257269
parallelism: 12
258270
docker:
259-
- image: cimg/python:3.12.11
271+
- image: cimg/python:3.12.11-browsers
260272
working_directory: ~/plotly.js
261273
steps:
274+
- run: sudo apt-get update
275+
- browser-tools/install-browser-tools:
276+
install-firefox: false
277+
install-geckodriver: false
278+
install-chrome: true
279+
chrome-version: "132.0.6834.110"
262280
- attach_workspace:
263281
at: ~/
264282
- run: sudo apt-get update
@@ -276,9 +294,15 @@ jobs:
276294
make-baselines-b64:
277295
parallelism: 12
278296
docker:
279-
- image: cimg/python:3.12.11
297+
- image: cimg/python:3.12.11-browsers
280298
working_directory: ~/plotly.js
281299
steps:
300+
- run: sudo apt-get update
301+
- browser-tools/install-browser-tools:
302+
install-firefox: false
303+
install-geckodriver: false
304+
install-chrome: true
305+
chrome-version: "132.0.6834.110"
282306
- attach_workspace:
283307
at: ~/
284308
- run: sudo apt-get update
@@ -351,9 +375,15 @@ jobs:
351375

352376
make-exports:
353377
docker:
354-
- image: cimg/python:3.12.11
378+
- image: cimg/python:3.12.11-browsers
355379
working_directory: ~/plotly.js
356380
steps:
381+
- run: sudo apt-get update
382+
- browser-tools/install-browser-tools:
383+
install-firefox: false
384+
install-geckodriver: false
385+
install-chrome: true
386+
chrome-version: "132.0.6834.110"
357387
- attach_workspace:
358388
at: ~/
359389
- run: sudo apt-get update

.circleci/env_image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sudo apt install fontconfig
1515
sudo fc-cache -f
1616

1717
# install kaleido & plotly
18-
sudo python3 -m pip install kaleido==0.2.1 plotly==6.2.0 --progress-bar off
18+
sudo python3 -m pip install "plotly[kaleido]==6.2.0" --progress-bar off
1919

2020
# install numpy i.e. to convert arrays to typed arrays
2121
sudo python3 -m pip install numpy==1.24.2

test/image/make_baseline.py

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import asyncio
2+
import json
13
import os
24
import sys
3-
import json
5+
6+
import kaleido
47
import plotly.io as pio
8+
59
from convert_b64 import arraysToB64
610

11+
712
args = []
813
if len(sys.argv) == 2 :
914
args = sys.argv[1].split()
@@ -19,7 +24,7 @@
1924
dirIn = os.path.join(root, 'test', 'image', 'mocks')
2025
dirOut = 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

2429
if '=' in args :
2530
args = args[args.index('=') + 1:]
@@ -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.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

@@ -52,8 +58,8 @@
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')
5763
pio.templates.default = 'none'
5864

5965
ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')]
@@ -79,55 +85,74 @@
7985
sys.exit(1)
8086

8187
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

Comments
 (0)