-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_html.py
More file actions
123 lines (112 loc) · 3.66 KB
/
csv_to_html.py
File metadata and controls
123 lines (112 loc) · 3.66 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
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
import pandas as pd
from pathlib import Path
import sys
def csv_to_html(csv_file, output_html):
df = pd.read_csv(csv_file)
# Make URLs clickable
for col in df.columns:
if df[col].astype(str).str.startswith('http').any():
df[col] = df[col].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>' if str(x).startswith('http') else x)
# Add a delete button column
df["Actions"] = '<button onclick="deleteRow(this)">🗑️ Delete</button>'
# Convert DataFrame to HTML table
html_table = df.to_html(escape=False, index=False, classes='display', table_id="csvTable")
# Full HTML with DataTables and styles
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSV Report</title>
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
<style>
body {{
font-family: 'Segoe UI', sans-serif;
background-color: #f9f9f9;
color: #333;
padding: 2rem;
transition: background-color 0.3s, color 0.3s;
}}
.dark-mode {{
background-color: #1e1e1e;
color: #e0e0e0;
}}
.dark-mode table {{
color: #e0e0e0;
}}
.dark-mode a {{
color: #80bfff;
}}
.dark-mode .dataTables_wrapper .dataTables_filter input,
.dark-mode .dataTables_wrapper .dataTables_length select {{
background-color: #2b2b2b;
color: #e0e0e0;
border: 1px solid #555;
}}
.dark-mode .dataTables_wrapper .dataTables_info,
.dark-mode .dataTables_wrapper .dataTables_paginate {{
color: #aaa;
}}
.dark-mode .dataTables_wrapper .dataTables_paginate .paginate_button {{
background-color: #333;
color: #eee !important;
border: none;
}}
.dark-mode th,
.dark-mode td {{
border: 1px solid #444;
}}
.dark-mode button {{
color: #e0e0e0; /* Matches the color of usernames in dark mode */
}}
button {{
background: none;
border: none;
cursor: pointer;
font-size: 1em;
}}
#toggleDark {{
margin-bottom: 1rem;
padding: 0.5em 1em;
border-radius: 8px;
background-color: #007bff;
color: white;
border: none;
}}
#toggleDark:hover {{
background-color: #0056b3;
}}
</style>
</head>
<body>
<button id="toggleDark" onclick="toggleDarkMode()">🌗 Toggle Dark Mode</button>
{html_table}
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {{
$('#csvTable').DataTable({{
responsive: true
}});
}});
function deleteRow(button) {{
const row = button.closest('tr');
row.parentNode.removeChild(row);
}}
function toggleDarkMode() {{
document.body.classList.toggle('dark-mode');
}}
</script>
</body>
</html>
"""
Path(output_html).write_text(html_content, encoding='utf-8')
print(f"✅ HTML report saved to: {output_html}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python csv_to_html.py <input.csv> [output.html]")
sys.exit(1)
input_csv = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else "report.html"
csv_to_html(input_csv, output_file)