forked from formazione/utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandastable.html
105 lines (85 loc) · 3.23 KB
/
pandastable.html
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
A little <strong>script</strong> to make an html table out of <strong>pandas</strong>. I made this type of stuff before, but I tried a different approach that maybe could bring to something more.
<a href="https://github.com/formazione/utilities">Github repository</a>
<pre class="lang:default decode:true">import pandas as pd
import os
table_class = "rwd-table"
html = """<style>
@import "https://fonts.googleapis.com/css?family=Montserrat:300,400,700";
.rwd-table {
margin: 1em 0;
border: 1px;
min-width: 300px;
}
.rwd-table tr {border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }
.rwd-table th {display: none; }
.rwd-table td {border:1; display: block; }
.rwd-table td:first-child {padding-top: .5em; }
.rwd-table td:last-child {padding-bottom: .5em; }
.rwd-table td:before {content: attr(data-th) ": "; font-weight: bold; width: 6.5em; display: inline-block; }
@media (min-width: 480px) {
.rwd-table td:before {display: none; }
} .rwd-table th, .rwd-table td {text-align: left;
}
@media (min-width: 480px) {
.rwd-table th, .rwd-table td {
display: table-cell; padding: .25em .5em; }
.rwd-table th:first-child,
.rwd-table td:first-child {padding-left: 0; }
.rwd-table th:last-child, .rwd-table td:last-child {padding-right: 0; } }
h1 {font-weight: normal; letter-spacing: -1px; color: #34495E; }
.rwd-table {background: #34495E; color: #fff; border-radius: .4em; overflow: hidden; }
.rwd-table tr {border-color: #46637f; }
.rwd-table th,
.rwd-table td {margin: .5em 1em; } @media (min-width: 480px) {
.rwd-table th, .rwd-table td {padding: 1em !important; } }
.rwd-table th, .rwd-table td:before {color: #dd5; } </style>
<script>
window.console = window.console || function(t) {};
</script>
<script>
if (document.location.search.match(/type=embed/gi)) {
window.parent.postMessage("resize", "*");
}
</script>"""
# df = pd.read_excel(filename, index=1)
df = pd.read_excel("001.xlsx", index=1)
print(type(df))
print()
# df.to_html("data.html")
html += "<table class='rwd-table'>"
html2 = ""
add = ""
def header():
"Create the header of the column"
add = ""
for d in df:
add += f"<th>{d}</th>"
add += "<tr>"
return add
def content_of_table():
"This are the tds"
count = len(df)
add = ""
count = 0
for i in range(0, len(df)):
for d in df:
add += f"<td>{df[d][i]}</td>"
add += "<tr>"
return add
def main(html):
"Builds up the table with the functions made for it"
html += header()
html += content_of_table()
return html
html = main(html)
html += add
html += "</table>"
print(html)
with open("file.html", "w") as file:
file.write(html)
os.startfile("file.html")
</pre>
The data were like this
<a href="https://pythonprogramming.altervista.org/wp-content/uploads/2020/11/001.png"><img class="alignnone size-full wp-image-7745" src="https://pythonprogramming.altervista.org/wp-content/uploads/2020/11/001.png" alt="" width="505" height="328" /></a>
The output is this
<a href="https://pythonprogramming.altervista.org/wp-content/uploads/2020/11/output.png"><img class="alignnone size-full wp-image-7746" src="https://pythonprogramming.altervista.org/wp-content/uploads/2020/11/output.png" alt="" width="316" height="228" /></a>