-
Notifications
You must be signed in to change notification settings - Fork 0
/
funs.py
158 lines (148 loc) · 4.94 KB
/
funs.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import datetime
import pandas as pd
span_style = '''
span.core {color: #93c47d; font-weight: bold}
span.tier1 {color: #a4c2f4; font-weight: bold}
span.tier2 {color: #d35f98; font-weight: bold}
'''
html_style = '''
<style>
body { padding-bottom: 600px; }
tr:hover {background-color:#f5f5f5;}
th, td {text-align: center; padding: 3px;}
table {border-collapse: collapse;}''' + span_style + '''
a {color: DodgerBlue}
a:link { text-decoration: none; }
a:visited { text-decoration: none; }
a:hover { text-decoration: underline; }
a:active { text-decoration: underline;}
ul.twocol { columns: 2; -webkit-columns: 2; -moz-columns: 2; }
</style>
'''
html_legend = '''
<p style="font-size: smaller;"> Colour legend:
<span class="planned">planned</span>
<span class="running">running</span>
<span class="completed">completed</span>
<span class="published">published</span>
</p>
'''
table_props = [('width', '100px')]
priority_spans = {
'I4C-CORE': 'core',
'I4C-TIER1': 'tier1',
'I4C-TIER2': 'tier2'
}
def html_header(title = 'CORDEX-CMIP6 downscaling plans'):
return(f'''<!DOCTYPE html>
<html><head>
{html_style}
</head><body>
<h1 id="top">{title}</h1>
<div style="display:table;width:100%;">
<div style="display:table-row;">
<div style="display:table-cell;width:50%;">
<a href="https://wcrp-cordex.github.io/simulation-status">Back to main</a> or see tables by
<a href="./CORDEX_CMIP6_status.html">domain</a>,
<a href="./CORDEX_CMIP6_status_by_scenario.html">scenario</a> or
<a href="./CORDEX_CMIP6_status_by_experiment.html">experiment</a>
</div>
<div style="display:table-cell;text-align:right;width:50%;">
(Version: {datetime.datetime.now().strftime("%Y-%m-%d %H:%M")})
</div>
</div>
</div>
<p style="text-align: justify;">
Simulation status according to CORDEX-CMIP6 downscaling plans reported by the groups and collected in <a href="https://github.com/WCRP-CORDEX/simulation-status/blob/main/CMIP6_downscaling_plans.csv">CMIP6_downscaling_plans.csv</a>.
To contribute/update simulations open and issue or pull request at <a href="https://github.com/WCRP-CORDEX/simulation-status">https://github.com/WCRP-CORDEX/simulation-status</a>.
'''
)
def html_footer():
return('</body></html>')
def addtag(word, field):
rval = word
if (field == 'comment') and word.startswith('#'):
rval = f'<span class="tag">{word[1:]}</span>'
elif (field == 'comment') and word.startswith('http'):
rval = f'<a href="{word}">{word}</a>'
elif (field == 'priority') and word in priority_spans.keys():
rval = f'<span class="{priority_spans[word]}">{word}</span>'
return(rval)
def taggify(text, field):
rval = text
if field in ['priority', 'comment']:
rval = ' '.join([addtag(x, field) for x in text.split(' ')])
return(rval)
def csv2datatable(csvfile, htmlout, title='', intro='', rename_fields = {}):
plans = pd.read_csv(csvfile, na_filter=False)
field_names = dict(zip(plans.columns,plans.columns))
field_names.update(rename_fields)
fp = open(htmlout,'w')
fp.write('''<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author" content="J. Fernandez" />
<meta name="keywords" content="HTML, CSS, JavaScript" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/plug-ins/2.0.5/features/deepLink/dataTables.deepLink.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#table_id').DataTable( $.fn.dataTable.ext.deepLink( [
'search.search'
]));
} );
</script>
''')
if title:
fp.write(f'<title>{title}</title>')
fp.write(f'''
<style>
span.tag {{
background-color: #c5def5;
padding: 0 10px;
font-size: 12px;
font-weight: 500;
line-height: 22px !important;
border: 1px solid transparent;
border-radius: 2em;
}}
a {{color: DodgerBlue}}
a:link {{ text-decoration: none; }}
a:visited {{ text-decoration: none; }}
a:hover {{ text-decoration: underline; }}
a:active {{ text-decoration: underline;}}
{span_style}
</style>
</head>
<body>
''')
if title:
fp.write(f'<h1>{title}</h1>')
if intro:
fp.write(f'{intro}')
fp.write('''
<table id="table_id" class="display">
<thead>
<tr>
''')
[fp.write(f' <th>{field_names[x]}</th>\n') for x in plans]
fp.write(f'''
</tr>
</thead>
<tbody>
''')
for idx, plan in plans.iterrows():
fp.write(f' <tr>\n')
for field,item in zip(plans.columns, plan):
fp.write(f' <td>{taggify(item, field)}</td>\n')
fp.write(f' </tr>\n')
fp.write('''
</tbody>
</table>
</body>
</html>''')
fp.close()