|
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 | 73 | # unable to generate baselines for the following mocks |
68 | 74 | 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 | | - 'map_layers', |
| 75 | + "map_stamen-style", |
| 76 | + "map_predefined-styles2", |
| 77 | + "map_scattercluster", |
| 78 | + "map_fonts-supported-open-sans", |
| 79 | + "map_fonts-supported-open-sans-weight", |
| 80 | + "map_layers", |
75 | 81 | ] |
76 | 82 | allNames = [a for a in allNames if a not in blacklist] |
77 | 83 |
|
78 | | -if len(allNames) == 0 : |
79 | | - print('error: Nothing to create!') |
| 84 | +if len(allNames) == 0: |
| 85 | + print("error: Nothing to create!") |
80 | 86 | sys.exit(1) |
81 | 87 |
|
82 | 88 | failed = [] |
83 | | -for name in allNames : |
| 89 | +for name in allNames: |
84 | 90 | outName = name |
85 | | - if mathjax_version == 3 : |
86 | | - outName = 'mathjax3___' + name |
| 91 | + if mathjax_version == 3: |
| 92 | + outName = "mathjax3___" + name |
87 | 93 |
|
88 | 94 | print(outName) |
89 | 95 |
|
90 | 96 | created = False |
91 | 97 |
|
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 : |
| 98 | + MAX_RETRY = 2 # 1 means retry once |
| 99 | + for attempt in range(0, MAX_RETRY + 1): |
| 100 | + with open(os.path.join(dirIn, name + ".json"), "r") as _in: |
95 | 101 | fig = json.load(_in) |
96 | 102 |
|
97 | 103 | width = 700 |
98 | 104 | 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 : |
| 105 | + if "layout" in fig: |
| 106 | + layout = fig["layout"] |
| 107 | + if "autosize" not in layout or layout["autosize"] != True: |
| 108 | + if "width" in layout: |
| 109 | + width = layout["width"] |
| 110 | + if "height" in layout: |
| 111 | + height = layout["height"] |
| 112 | + |
| 113 | + if "b64" in sys.argv or "b64=" in sys.argv or "b64-json" in sys.argv: |
108 | 114 | newFig = dict() |
109 | 115 | arraysToB64(fig, newFig) |
110 | 116 | fig = newFig |
111 | | - if 'b64-json' in sys.argv and attempt == 0 : print(json.dumps(fig, indent = 2)) |
| 117 | + if "b64-json" in sys.argv and attempt == 0: |
| 118 | + print(json.dumps(fig, indent=2)) |
112 | 119 |
|
113 | | - try : |
| 120 | + try: |
114 | 121 | pio.write_image( |
115 | 122 | fig=fig, |
116 | | - file=os.path.join(dirOut, outName + '.png'), |
| 123 | + file=os.path.join(dirOut, outName + ".png"), |
117 | 124 | width=width, |
118 | 125 | height=height, |
119 | | - validate=False |
| 126 | + validate=False, |
120 | 127 | ) |
121 | 128 | created = True |
122 | | - except Exception as e : |
| 129 | + except Exception as e: |
123 | 130 | print(e) |
124 | | - if attempt < MAX_RETRY : |
125 | | - print('retry', attempt + 1, '/', MAX_RETRY) |
126 | | - else : |
| 131 | + if attempt < MAX_RETRY: |
| 132 | + print("retry", attempt + 1, "/", MAX_RETRY) |
| 133 | + else: |
127 | 134 | failed.append(outName) |
128 | 135 |
|
129 | | - if(created) : break |
| 136 | + if created: |
| 137 | + break |
130 | 138 |
|
131 | | -if len(failed) > 0 : |
132 | | - print('Failed at :') |
| 139 | +if len(failed) > 0: |
| 140 | + print("Failed at :") |
133 | 141 | print(failed) |
134 | 142 | sys.exit(1) |
0 commit comments