forked from aitatanit/python-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate_href-ipynb.py
More file actions
86 lines (72 loc) · 2.89 KB
/
translate_href-ipynb.py
File metadata and controls
86 lines (72 loc) · 2.89 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import sys
# -------------------------------------------------------------------------------
#
# Script that converts inter-User Guide href in ipynb (i.e. original) NBs
# to plot.ly domain (using ./inputs/translate.json)
#
# ??? Should I also translate the relative links ???
#
# -------------------------------------------------------------------------------
NAME="translate_href-ipynb" # file name
# Get input arguments
def get_args():
args = sys.argv[1:]
if not args:
print ("usage:\n"
"python {NAME}.py file.html \n"
"python {NAME}.py file1.html file2.html ... fileN.html\n".format(NAME=NAME))
sys.exit(0)
else:
return args
# Get URLs of domains from domains.json
def get_domains():
with open("./scripts/inputs/domains.json") as f:
domains = json.load(f)
return domains
# Get translate.json, to translate URL tails from
# nbviewer to plot.ly domain
# (e.g. s00_homepage/s00_homepage.ipynb to home
def get_translate():
with open('./scripts/inputs/translate.json') as f:
translate = json.load(f)
return translate
# -------------------------------------------------------------------------------
# Get the NB json object
def get_NB(file_ipynb):
with open(file_ipynb, "r") as f:
print "[{}]".format(NAME), 'Opening', file_ipynb
return json.load(f)
# Replace 'nbviewer'-domain hrefs to plot.ly
def replace_href(NB, domains, translate):
for i_cell, cell in enumerate(NB['worksheets'][0]['cells']):
if cell['cell_type']=='markdown':
for i_line, line in enumerate(cell['source']):
if domains['nbviewer'] in line:
_line=line.replace(domains['nbviewer'],domains['plotly-ext'])
print "[{}]".format(NAME), '... link found in cell {} / line {}'.format(i_cell,i_line)
for old, new in translate.items():
if old in _line:
__line=_line.replace(old,new)
NB['worksheets'][0]['cells'][i_cell]['source'][i_line]=__line
print "[{}]".format(NAME), '... line updated to:', __line
#TODO! Print raw string instead
break
return NB
# Replace ipynb file
def replace_file_ipynb(NB_new, file_ipynb):
with open(file_ipynb, "wb") as f:
print "[{}]".format(NAME), '... overwrites', file_ipynb
json.dump(NB_new, f, indent=4)
return
# -------------------------------------------------------------------------------
def main():
files_ipynb = get_args()
domains = get_domains()
translate = get_translate()
for file_ipynb in files_ipynb:
NB = get_NB(file_ipynb)
NB_new = replace_href(NB, domains, translate)
replace_file_ipynb(NB_new, file_ipynb)
if __name__ == "__main__":
main()