|
| 1 | +import asyncio |
1 | 2 | import os |
2 | 3 | import sys |
3 | 4 | import json |
| 5 | + |
4 | 6 | import plotly.io as pio |
| 7 | +import kaleido |
| 8 | + |
5 | 9 | from convert_b64 import arraysToB64 |
6 | 10 |
|
| 11 | + |
7 | 12 | args = [] |
8 | 13 | if len(sys.argv) == 2 : |
9 | 14 | args = sys.argv[1].split() |
|
31 | 36 | print('output to', dirOut) |
32 | 37 |
|
33 | 38 | mathjax_version = 2 |
| 39 | +mathjax = None |
34 | 40 | if 'mathjax3' in sys.argv or 'mathjax3=' in sys.argv : |
35 | 41 | # until https://github.com/plotly/Kaleido/issues/124 is addressed |
36 | 42 | # we are uanble to use local mathjax v3 installed in node_modules |
37 | 43 | # for now let's download it from the internet: |
38 | | - pio.defaults.mathjax = 'https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js' |
| 44 | + mathjax = 'https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js' |
39 | 45 | mathjax_version = 3 |
40 | 46 | print('Kaleido using MathJax v3') |
41 | 47 |
|
|
52 | 58 |
|
53 | 59 | plotlyjs = plotlyjs_with_virtual_webgl |
54 | 60 |
|
55 | | -pio.defaults.plotlyjs = plotlyjs |
56 | | - |
57 | 61 | pio.templates.default = 'none' |
58 | 62 |
|
59 | 63 | ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')] |
|
71 | 75 | 'map_predefined-styles2', # Temporarily blacklist: fails with Kaleido v1.0.0rc14 |
72 | 76 | 'grid_subplot_types', # Temporarily blacklist: fails with Kaleido v1.0.0rc14 |
73 | 77 | '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 |
74 | 79 | 'map_fonts-supported-metropolis-weight', # Temporarily blacklist: fails with Kaleido v1.0.0rc14 |
75 | 80 | 'map_fonts-supported-open-sans-weight', # Temporarily blacklist: fails with Kaleido v1.0.0rc14 |
76 | 81 | ] |
|
81 | 86 | sys.exit(1) |
82 | 87 |
|
83 | 88 | 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