|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import kaleido |
1 | 4 | import os |
2 | 5 | import sys |
3 | | -import json |
4 | | -import plotly.io as pio |
5 | 6 |
|
6 | 7 | root = os.getcwd() |
7 | 8 | dirIn = os.path.join(root, "test", "image", "mocks") |
8 | 9 | dirOut = os.path.join(root, "build", "test_images") |
9 | 10 | os.makedirs(dirOut, exist_ok=True) |
10 | 11 |
|
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") |
13 | 14 |
|
14 | 15 | allFormats = ["svg", "jpg", "jpeg", "webp", "pdf"] |
15 | 16 | # 'png' is tested by image-test |
16 | 17 |
|
17 | 18 | allNames = [ |
18 | | - "plot_types", |
19 | 19 | "annotations", |
20 | | - "shapes", |
21 | | - "range_slider", |
22 | 20 | "contour_legend-colorscale", |
23 | | - "layout_image", |
24 | | - "image_astronaut_source", |
| 21 | + "fonts", |
25 | 22 | "gl2d_no-clustering2", |
26 | 23 | "gl3d_surface-heatmap-treemap_transparent-colorscale", |
| 24 | + "image_astronaut_source", |
| 25 | + "layout_image", |
27 | 26 | "map_density-multiple_legend", |
| 27 | + "mathjax", |
| 28 | + "plot_types", |
| 29 | + "range_slider", |
| 30 | + "shapes", |
28 | 31 | "smith_modes", |
29 | | - "zsmooth_methods", |
30 | | - "fonts", |
31 | 32 | "worldcup", |
32 | | - "mathjax", |
| 33 | + "zsmooth_methods", |
33 | 34 | ] |
34 | 35 |
|
35 | 36 | failed = 0 |
36 | | -for name in allNames: |
37 | | - for fmt in allFormats: |
38 | | - print(name + " --> " + fmt) |
39 | 37 |
|
40 | | - with open(os.path.join(dirIn, name + ".json"), "r") as _in: |
41 | | - fig = json.load(_in) |
42 | 38 |
|
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) |
52 | 73 |
|
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 |
61 | 77 |
|
62 | | - except Exception as e: |
63 | | - print(e) |
64 | | - failed += 1 |
65 | 78 |
|
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