-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathconvert.py
222 lines (175 loc) · 6.92 KB
/
convert.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from __future__ import unicode_literals
import re
import os
import six
import uuid
import json
import time
import logging
import tempfile
import webbrowser
from io import open
import unicodecsv as csv
from jinja2 import Environment, FileSystemLoader, select_autoescape
logging.basicConfig()
logger = logging.getLogger(__package__)
package_path = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(package_path, "templates")
# Initialize Jinja 2 env
env = Environment(
loader=FileSystemLoader(templates_dir),
autoescape=select_autoescape(["html", "xml", "j2"])
)
template = env.get_template("template.j2")
# Regex to match src property in script tags
js_src_pattern = re.compile(r'<script.*?src=\"(.*?)\".*?<\/script>',
re.IGNORECASE | re.MULTILINE)
# Path to JS files inside templates
js_files_path = os.path.join(package_path, templates_dir)
def convert(input_file_name, **kwargs):
"""Convert CSV file to HTML table"""
delimiter = kwargs["delimiter"] or ","
quotechar = kwargs["quotechar"] or "|"
if six.PY2:
delimiter = delimiter.encode("utf-8")
quotechar = quotechar.encode("utf-8")
# Read CSV and form a header and rows list
with open(input_file_name, "rb") as input_file:
reader = csv.reader(input_file,
encoding="utf-8",
delimiter=delimiter,
quotechar=quotechar)
csv_headers = []
if not kwargs.get("no_header"):
# Read header from first line
csv_headers = next(reader)
csv_rows = [row for row in reader if row]
# Set default column name if header is not present
if not csv_headers and len(csv_rows) > 0:
end = len(csv_rows[0]) + 1
csv_headers = ["Column {}".format(n) for n in range(1, end)]
# Render csv to HTML
html = render_template(csv_headers, csv_rows, **kwargs)
# Freeze all JS files in template
return freeze_js(html)
def save(file_name, content):
"""Save content to a file"""
with open(file_name, "w", encoding="utf-8") as output_file:
output_file.write(content)
return output_file.name
def serve(content):
"""Write content to a temp file and serve it in browser"""
temp_folder = tempfile.gettempdir()
temp_file_name = tempfile.gettempprefix() + str(uuid.uuid4()) + ".html"
# Generate a file path with a random name in temporary dir
temp_file_path = os.path.join(temp_folder, temp_file_name)
# save content to temp file
save(temp_file_path, content)
# Open templfile in a browser
webbrowser.open("file://{}".format(temp_file_path))
# Block the thread while content is served
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
# cleanup the temp file
os.remove(temp_file_path)
def render_template(table_headers, table_items, **options):
"""
Render Jinja2 template
"""
caption = options.get("caption") or "Table"
display_length = options.get("display_length") or -1
height = options.get("height") or "70vh"
default_length_menu = [-1, 10, 25, 50]
pagination = options.get("pagination")
virtual_scroll_limit = options.get("virtual_scroll")
preserve_sort = options.get("preserve_sort")
# Change % to vh
height = height.replace("%", "vh")
# Header columns
columns = []
for header in table_headers:
columns.append({"title": header})
# Data table options
datatable_options = {
"columns": columns,
"data": table_items,
"iDisplayLength": display_length,
"sScrollX": "100%",
"sScrollXInner": "100%"
}
# Enable virtual scroll for rows bigger than 1000 rows
is_paging = pagination
virtual_scroll = False
scroll_y = height
if virtual_scroll_limit:
if virtual_scroll_limit != -1 and len(table_items) > virtual_scroll_limit:
virtual_scroll = True
display_length = -1
fmt = ("\nVirtual scroll is enabled since number of rows exceeds {limit}."
" You can set custom row limit by setting flag -vs, --virtual-scroll."
" Virtual scroll can be disabled by setting the value to -1 and set it to 0 to always enable.")
logger.warn(fmt.format(limit=virtual_scroll_limit))
if not is_paging:
fmt = "\nPagination can not be disabled in virtual scroll mode."
logger.warn(fmt)
is_paging = True
if is_paging and not virtual_scroll:
# Add display length to the default display length menu
length_menu = []
if display_length != -1:
length_menu = sorted(default_length_menu + [display_length])
else:
length_menu = default_length_menu
# Set label as "All" it display length is -1
length_menu_label = [str("All") if i == -1 else i for i in length_menu]
datatable_options["lengthMenu"] = [length_menu, length_menu_label]
datatable_options["iDisplayLength"] = display_length
if is_paging:
datatable_options["paging"] = True
else:
datatable_options["paging"] = False
if scroll_y:
datatable_options["scrollY"] = scroll_y
if virtual_scroll:
datatable_options["scroller"] = True
datatable_options["bPaginate"] = False
datatable_options["deferRender"] = True
datatable_options["bLengthChange"] = False
if preserve_sort:
datatable_options["order"] = []
enable_export = options.get("export")
if enable_export:
if options["export_options"]:
allowed = list(options["export_options"])
else:
allowed = ["copy", "csv", "json", "print"]
datatable_options["dom"] = "Bfrtip"
datatable_options["buttons"] = allowed
datatable_options_json = json.dumps(datatable_options,
separators=(",", ":"))
return template.render(title=caption or "Table",
caption=caption,
datatable_options=datatable_options_json,
virtual_scroll=virtual_scroll,
enable_export=enable_export)
def freeze_js(html):
"""
Freeze all JS assets to the rendered html itself.
"""
matches = js_src_pattern.finditer(html)
if not matches:
return html
# Reverse regex matches to replace match string with respective JS content
for match in reversed(tuple(matches)):
# JS file name
file_name = match.group(1)
file_path = os.path.join(js_files_path, file_name)
with open(file_path, "r", encoding="utf-8") as f:
file_content = f.read()
# Replace matched string with inline JS
fmt = '<script type="text/javascript">{}</script>'
js_content = fmt.format(file_content)
html = html[:match.start()] + js_content + html[match.end():]
return html