Skip to content

Commit fe6aa6d

Browse files
committed
Update make exports script
1 parent fea595e commit fe6aa6d

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

test/image/make_exports.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,82 @@
1+
import asyncio
2+
import json
3+
import kaleido
14
import os
25
import sys
3-
import json
4-
import plotly.io as pio
56

67
root = os.getcwd()
78
dirIn = os.path.join(root, "test", "image", "mocks")
89
dirOut = os.path.join(root, "build", "test_images")
910
os.makedirs(dirOut, exist_ok=True)
1011

11-
pio.templates.default = "none"
12-
pio.kaleido.scope.plotlyjs = os.path.join(root, "build", "plotly.js")
12+
plotlyjs = os.path.join(root, "build", "plotly.js")
13+
topojson = "file://" + os.path.join(root, "topojson", "dist")
1314

1415
allFormats = ["svg", "jpg", "jpeg", "webp", "pdf"]
1516
# 'png' is tested by image-test
1617

1718
allNames = [
18-
"plot_types",
1919
"annotations",
20-
"shapes",
21-
"range_slider",
2220
"contour_legend-colorscale",
23-
"layout_image",
24-
"image_astronaut_source",
21+
"fonts",
2522
"gl2d_no-clustering2",
2623
"gl3d_surface-heatmap-treemap_transparent-colorscale",
24+
"image_astronaut_source",
25+
"layout_image",
2726
"map_density-multiple_legend",
27+
"mathjax",
28+
"plot_types",
29+
"range_slider",
30+
"shapes",
2831
"smith_modes",
29-
"zsmooth_methods",
30-
"fonts",
3132
"worldcup",
32-
"mathjax",
33+
"zsmooth_methods",
3334
]
3435

3536
failed = 0
36-
for name in allNames:
37-
for fmt in allFormats:
38-
print(name + " --> " + fmt)
3937

40-
with open(os.path.join(dirIn, name + ".json"), "r") as _in:
41-
fig = json.load(_in)
4238

43-
width = 700
44-
height = 500
45-
if "layout" in fig:
46-
layout = fig["layout"]
47-
if "autosize" not in layout or layout["autosize"] != True:
48-
if "width" in layout:
49-
width = layout["width"]
50-
if "height" in layout:
51-
height = layout["height"]
39+
async def make_exports_async():
40+
global failed
41+
42+
async with kaleido.Kaleido(n=1, plotlyjs=plotlyjs) as k:
43+
for name in allNames:
44+
with open(os.path.join(dirIn, name + ".json"), "r") as _in:
45+
fig = json.load(_in)
46+
47+
width = 700
48+
height = 500
49+
if "layout" in fig:
50+
layout = fig["layout"]
51+
if "autosize" not in layout or layout["autosize"] != True:
52+
if "width" in layout:
53+
width = layout["width"]
54+
if "height" in layout:
55+
height = layout["height"]
56+
57+
for fmt in allFormats:
58+
print(name + " --> " + fmt)
59+
60+
try:
61+
data = await k.calc_fig(
62+
fig,
63+
opts=dict(
64+
format=fmt,
65+
width=width,
66+
height=height,
67+
),
68+
topojson=topojson,
69+
)
70+
filename = os.path.join(dirOut, name + "." + fmt)
71+
with open(filename, "wb") as f:
72+
f.write(data)
5273

53-
try:
54-
pio.write_image(
55-
fig=fig,
56-
file=os.path.join(dirOut, name + "." + fmt),
57-
width=width,
58-
height=height,
59-
validate=False,
60-
)
74+
except Exception as e:
75+
print(e)
76+
failed += 1
6177

62-
except Exception as e:
63-
print(e)
64-
failed += 1
6578

66-
if failed > 0:
67-
sys.exit(1)
79+
if __name__ == "__main__":
80+
asyncio.run(make_exports_async())
81+
if failed > 0:
82+
sys.exit(1)

0 commit comments

Comments
 (0)