-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
dataframe_to_html.py
43 lines (38 loc) · 1.39 KB
/
dataframe_to_html.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
import pandas as pd
import webbrowser
def generate_html(dataframe: pd.DataFrame):
# get the table HTML from the dataframe
table_html = dataframe.to_html(table_id="table")
# construct the complete HTML with jQuery Data tables
# You can disable paging or enable y scrolling on lines 20 and 21 respectively
html = f"""
<html>
<header>
<link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet">
</header>
<body>
{table_html}
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready( function () {{
$('#table').DataTable({{
// paging: false,
// scrollY: 400,
}});
}});
</script>
</body>
</html>
"""
# return the html
return html
if __name__ == "__main__":
# read the dataframe dataset
df = pd.read_csv("Churn_Modelling.csv")
# generate the HTML from the dataframe
html = generate_html(df)
# write the HTML content to an HTML file
open("index.html", "w").write(html)
# open the new HTML file with the default browser
webbrowser.open("index.html")