|
5 | 5 | from convert_b64 import arraysToB64 |
6 | 6 |
|
7 | 7 | args = [] |
8 | | -if len(sys.argv) == 2 : |
| 8 | +if len(sys.argv) == 2: |
9 | 9 | args = sys.argv[1].split() |
10 | | -elif len(sys.argv) > 1 : |
| 10 | +elif len(sys.argv) > 1: |
11 | 11 | args = sys.argv |
12 | 12 |
|
13 | 13 | root = os.getcwd() |
14 | 14 |
|
15 | | -virtual_webgl = os.path.join(root, 'node_modules', 'virtual-webgl', 'src', 'virtual-webgl.js') |
16 | | -plotlyjs = os.path.join(root, 'build', 'plotly.js') |
17 | | -plotlyjs_with_virtual_webgl = os.path.join(root, 'build', 'plotly_with_virtual-webgl.js') |
| 15 | +virtual_webgl = os.path.join( |
| 16 | + root, "node_modules", "virtual-webgl", "src", "virtual-webgl.js" |
| 17 | +) |
| 18 | +plotlyjs = os.path.join(root, "build", "plotly.js") |
| 19 | +plotlyjs_with_virtual_webgl = os.path.join( |
| 20 | + root, "build", "plotly_with_virtual-webgl.js" |
| 21 | +) |
18 | 22 |
|
19 | | -dirIn = os.path.join(root, 'test', 'image', 'mocks') |
20 | | -dirOut = os.path.join(root, 'build', 'test_images') |
| 23 | +dirIn = os.path.join(root, "test", "image", "mocks") |
| 24 | +dirOut = os.path.join(root, "build", "test_images") |
21 | 25 |
|
22 | 26 | # N.B. equal is the falg to write to baselines not test_images |
23 | 27 |
|
24 | | -if '=' in args : |
25 | | - args = args[args.index('=') + 1:] |
26 | | - dirOut = os.path.join(root, 'test', 'image', 'baselines') |
| 28 | +if "=" in args: |
| 29 | + args = args[args.index("=") + 1 :] |
| 30 | + dirOut = os.path.join(root, "test", "image", "baselines") |
27 | 31 |
|
28 | | -if 'mathjax3=' in sys.argv : |
29 | | - dirOut = os.path.join(root, 'test', 'image', 'baselines') |
| 32 | +if "mathjax3=" in sys.argv: |
| 33 | + dirOut = os.path.join(root, "test", "image", "baselines") |
30 | 34 |
|
31 | | -print('output to', dirOut) |
| 35 | +print("output to", dirOut) |
32 | 36 |
|
33 | 37 | mathjax_version = 2 |
34 | | -if 'mathjax3' in sys.argv or 'mathjax3=' in sys.argv : |
| 38 | +if "mathjax3" in sys.argv or "mathjax3=" in sys.argv: |
35 | 39 | # until https://github.com/plotly/Kaleido/issues/124 is addressed |
36 | 40 | # we are uanble to use local mathjax v3 installed in node_modules |
37 | 41 | # for now let's download it from the internet: |
38 | | - pio.kaleido.scope.mathjax = 'https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js' |
| 42 | + pio.kaleido.scope.mathjax = ( |
| 43 | + "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js" |
| 44 | + ) |
39 | 45 | mathjax_version = 3 |
40 | | - print('Kaleido using MathJax v3') |
| 46 | + print("Kaleido using MathJax v3") |
41 | 47 |
|
42 | | -virtual_webgl_version = 0 # i.e. virtual-webgl is not used |
43 | | -if 'virtual-webgl' in sys.argv or 'virtual-webgl=' in sys.argv : |
| 48 | +virtual_webgl_version = 0 # i.e. virtual-webgl is not used |
| 49 | +if "virtual-webgl" in sys.argv or "virtual-webgl=" in sys.argv: |
44 | 50 | virtual_webgl_version = 1 |
45 | | - print('using virtual-webgl for WebGL v1') |
| 51 | + print("using virtual-webgl for WebGL v1") |
46 | 52 |
|
47 | | - with open(plotlyjs_with_virtual_webgl, 'w') as fileOut: |
| 53 | + with open(plotlyjs_with_virtual_webgl, "w") as fileOut: |
48 | 54 | for filename in [virtual_webgl, plotlyjs]: |
49 | | - with open(filename, 'r') as fileIn: |
| 55 | + with open(filename, "r") as fileIn: |
50 | 56 | for line in fileIn: |
51 | 57 | fileOut.write(line) |
52 | 58 |
|
53 | 59 | plotlyjs = plotlyjs_with_virtual_webgl |
54 | 60 |
|
55 | 61 | pio.kaleido.scope.plotlyjs = plotlyjs |
56 | | -pio.kaleido.scope.topojson = "file://" + os.path.join(root, 'topojson', 'dist') |
57 | | -pio.templates.default = 'none' |
| 62 | +pio.kaleido.scope.topojson = "file://" + os.path.join(root, "topojson", "dist") |
| 63 | +pio.templates.default = "none" |
58 | 64 |
|
59 | | -ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')] |
| 65 | +ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith(".json")] |
60 | 66 | ALL_MOCKS.sort() |
61 | 67 |
|
62 | | -if len(args) > 0 : |
| 68 | +if len(args) > 0: |
63 | 69 | allNames = [a for a in args if a in ALL_MOCKS] |
64 | | -else : |
| 70 | +else: |
65 | 71 | allNames = ALL_MOCKS |
66 | 72 |
|
67 | | -# unable to generate baselines for the following mocks |
68 | | -blacklist = [ |
69 | | - 'map_stamen-style', |
70 | | - 'map_predefined-styles2', |
71 | | - 'map_scattercluster', |
72 | | - 'map_fonts-supported-open-sans', |
73 | | - 'map_fonts-supported-open-sans-weight', |
74 | | -] |
75 | | -allNames = [a for a in allNames if a not in blacklist] |
76 | | - |
77 | | -if len(allNames) == 0 : |
78 | | - print('error: Nothing to create!') |
| 73 | +with open(os.path.join(root, "test", "image", "disallow_list.json"), "r") as f: |
| 74 | + # unable to generate baselines for the following mocks |
| 75 | + disallowList = set(json.load(f)) |
| 76 | +allNames = [a for a in allNames if a not in disallowList] |
| 77 | + |
| 78 | +if len(allNames) == 0: |
| 79 | + print("error: Nothing to create!") |
79 | 80 | sys.exit(1) |
80 | 81 |
|
81 | 82 | failed = [] |
82 | | -for name in allNames : |
| 83 | +for name in allNames: |
83 | 84 | outName = name |
84 | | - if mathjax_version == 3 : |
85 | | - outName = 'mathjax3___' + name |
| 85 | + if mathjax_version == 3: |
| 86 | + outName = "mathjax3___" + name |
86 | 87 |
|
87 | 88 | print(outName) |
88 | 89 |
|
89 | 90 | created = False |
90 | 91 |
|
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 : |
| 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: |
94 | 95 | fig = json.load(_in) |
95 | 96 |
|
96 | 97 | width = 700 |
97 | 98 | 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 : |
| 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: |
107 | 108 | newFig = dict() |
108 | 109 | arraysToB64(fig, newFig) |
109 | 110 | fig = newFig |
110 | | - if 'b64-json' in sys.argv and attempt == 0 : print(json.dumps(fig, indent = 2)) |
| 111 | + if "b64-json" in sys.argv and attempt == 0: |
| 112 | + print(json.dumps(fig, indent=2)) |
111 | 113 |
|
112 | | - try : |
| 114 | + try: |
113 | 115 | pio.write_image( |
114 | 116 | fig=fig, |
115 | | - file=os.path.join(dirOut, outName + '.png'), |
| 117 | + file=os.path.join(dirOut, outName + ".png"), |
116 | 118 | width=width, |
117 | 119 | height=height, |
118 | | - validate=False |
| 120 | + validate=False, |
119 | 121 | ) |
120 | 122 | created = True |
121 | | - except Exception as e : |
| 123 | + except Exception as e: |
122 | 124 | print(e) |
123 | | - if attempt < MAX_RETRY : |
124 | | - print('retry', attempt + 1, '/', MAX_RETRY) |
125 | | - else : |
| 125 | + if attempt < MAX_RETRY: |
| 126 | + print("retry", attempt + 1, "/", MAX_RETRY) |
| 127 | + else: |
126 | 128 | failed.append(outName) |
127 | 129 |
|
128 | | - if(created) : break |
| 130 | + if created: |
| 131 | + break |
129 | 132 |
|
130 | | -if len(failed) > 0 : |
131 | | - print('Failed at :') |
| 133 | +if len(failed) > 0: |
| 134 | + print("Failed at :") |
132 | 135 | print(failed) |
133 | 136 | sys.exit(1) |
0 commit comments