|
| 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 | from convert_b64 import arraysToB64 |
6 | 7 |
|
7 | 8 | args = [] |
|
20 | 21 | root, "build", "plotly_with_virtual-webgl.js" |
21 | 22 | ) |
22 | 23 |
|
| 24 | +topojson = "file://" + os.path.join(root, "topojson", "dist") |
23 | 25 | dirIn = os.path.join(root, "test", "image", "mocks") |
24 | 26 | dirOut = os.path.join(root, "build", "test_images") |
25 | 27 |
|
26 | | -# N.B. equal is the falg to write to baselines not test_images |
| 28 | +# N.B. equal is the flag to write to baselines not test_images |
27 | 29 |
|
28 | 30 | if "=" in args: |
29 | 31 | args = args[args.index("=") + 1 :] |
|
35 | 37 | print("output to", dirOut) |
36 | 38 |
|
37 | 39 | mathjax_version = 2 |
| 40 | +mathjax = None |
38 | 41 | if "mathjax3" in sys.argv or "mathjax3=" in sys.argv: |
39 | 42 | # until https://github.com/plotly/Kaleido/issues/124 is addressed |
40 | 43 | # we are uanble to use local mathjax v3 installed in node_modules |
41 | 44 | # for now let's download it from the internet: |
42 | | - pio.kaleido.scope.mathjax = ( |
43 | | - "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js" |
44 | | - ) |
| 45 | + mathjax = "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js" |
| 46 | + |
45 | 47 | mathjax_version = 3 |
46 | 48 | print("Kaleido using MathJax v3") |
47 | 49 |
|
|
58 | 60 |
|
59 | 61 | plotlyjs = plotlyjs_with_virtual_webgl |
60 | 62 |
|
61 | | -pio.kaleido.scope.plotlyjs = plotlyjs |
62 | | -pio.kaleido.scope.topojson = "file://" + os.path.join(root, "topojson", "dist") |
63 | | -pio.templates.default = "none" |
64 | | - |
65 | 63 | ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith(".json")] |
66 | 64 | ALL_MOCKS.sort() |
67 | 65 |
|
|
80 | 78 | sys.exit(1) |
81 | 79 |
|
82 | 80 | failed = [] |
83 | | -for name in allNames: |
84 | | - outName = name |
85 | | - if mathjax_version == 3: |
86 | | - outName = "mathjax3___" + name |
87 | | - |
88 | | - print(outName) |
89 | | - |
90 | | - created = False |
91 | | - |
92 | | - MAX_RETRY = 2 # 1 means retry once |
93 | | - for attempt in range(0, MAX_RETRY + 1): |
94 | | - with open(os.path.join(dirIn, name + ".json"), "r") as _in: |
95 | | - fig = json.load(_in) |
96 | | - |
97 | | - width = 700 |
98 | | - height = 500 |
99 | | - if "layout" in fig: |
100 | | - layout = fig["layout"] |
101 | | - if "autosize" not in layout or layout["autosize"] != True: |
102 | | - if "width" in layout: |
103 | | - width = layout["width"] |
104 | | - if "height" in layout: |
105 | | - height = layout["height"] |
106 | | - |
107 | | - if "b64" in sys.argv or "b64=" in sys.argv or "b64-json" in sys.argv: |
108 | | - newFig = dict() |
109 | | - arraysToB64(fig, newFig) |
110 | | - fig = newFig |
111 | | - if "b64-json" in sys.argv and attempt == 0: |
112 | | - 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: |
131 | | - break |
132 | | - |
133 | | -if len(failed) > 0: |
134 | | - print("Failed at :") |
135 | | - print(failed) |
136 | | - sys.exit(1) |
| 81 | + |
| 82 | + |
| 83 | +async def make_baselines_async(): |
| 84 | + |
| 85 | + kopts = dict( |
| 86 | + plotlyjs=plotlyjs, |
| 87 | + ) |
| 88 | + if mathjax is not None: |
| 89 | + kopts["mathjax"] = mathjax |
| 90 | + |
| 91 | + async with kaleido.Kaleido(n=1, **kopts) as k: |
| 92 | + for name in allNames: |
| 93 | + outName = name |
| 94 | + if mathjax_version == 3: |
| 95 | + outName = "mathjax3___" + name |
| 96 | + |
| 97 | + print(outName) |
| 98 | + |
| 99 | + created = False |
| 100 | + |
| 101 | + MAX_RETRY = 2 # 1 means retry once |
| 102 | + for attempt in range(0, MAX_RETRY + 1): |
| 103 | + with open(os.path.join(dirIn, name + ".json"), "r") as _in: |
| 104 | + fig = json.load(_in) |
| 105 | + |
| 106 | + width = 700 |
| 107 | + height = 500 |
| 108 | + if "layout" in fig: |
| 109 | + layout = fig["layout"] |
| 110 | + if "autosize" not in layout or layout["autosize"] != True: |
| 111 | + if "width" in layout: |
| 112 | + width = layout["width"] |
| 113 | + if "height" in layout: |
| 114 | + height = layout["height"] |
| 115 | + |
| 116 | + if ( |
| 117 | + "b64" in sys.argv |
| 118 | + or "b64=" in sys.argv |
| 119 | + or "b64-json" in sys.argv |
| 120 | + ): |
| 121 | + newFig = dict() |
| 122 | + arraysToB64(fig, newFig) |
| 123 | + fig = newFig |
| 124 | + if "b64-json" in sys.argv and attempt == 0: |
| 125 | + print(json.dumps(fig, indent=2)) |
| 126 | + |
| 127 | + try: |
| 128 | + bytes = await k.calc_fig( |
| 129 | + fig, |
| 130 | + opts=dict( |
| 131 | + format="png", |
| 132 | + width=width, |
| 133 | + height=height, |
| 134 | + ), |
| 135 | + topojson=topojson, |
| 136 | + ) |
| 137 | + filename = os.path.join(dirOut, outName + ".png") |
| 138 | + with open(filename, "wb") as f: |
| 139 | + f.write(bytes) |
| 140 | + created = True |
| 141 | + except Exception as e: |
| 142 | + print(e) |
| 143 | + if attempt < MAX_RETRY: |
| 144 | + print("retry", attempt + 1, "/", MAX_RETRY) |
| 145 | + else: |
| 146 | + failed.append(outName) |
| 147 | + |
| 148 | + if created: |
| 149 | + 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