-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotly-json-to-html
executable file
·52 lines (41 loc) · 1.27 KB
/
plotly-json-to-html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
import sys
from plotly.graph_objects import Figure
def process_file(path: Path, **kwargs):
if not path.exists():
print(f'No such file: {path}')
sys.exit(1)
if path.suffix != '.json':
print(f'Not a json file: {path}')
sys.exit(1)
try:
data = json.load(path.open('r'))
except json.JSONDecodeError:
print(f'Invalid file content: {path}')
sys.exit(1)
save_file_path = path.parent / (path.stem + '.html')
fig = Figure(data['data'], data['layout'])
fig.write_html(
str(save_file_path),
full_html=kwargs['full_html'],
include_plotlyjs=kwargs['include_plotlyjs'],
)
print(f'Output: {save_file_path}')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file', type=str, nargs='+')
parser.add_argument('--full-html', action='store_true')
parser.add_argument('--include-plotlyjs', action='store_true')
args = parser.parse_args()
fpaths = [Path(p) for p in args.file]
for path in fpaths:
process_file(
path,
full_html=args.full_html,
include_plotlyjs=args.include_plotlyjs,
)
if __name__ == '__main__':
main()