python - 如何使用将数据框显示为表格的常量 url 构建静态网站

标签 python html pandas dataframe web-hosting

我们有数据帧形式的 python 脚本输出之一:“dfIns”

我把上面的dataframe转换成了html表格

dfIns.to_html("insurancedetails.html")

有没有一种方法可以生成具有URL(https://insurancedetails.com/)的静态网站,并在url网页中显示dataframe/html输出,任何人都可以随时在浏览器中访问.

我尝试了以下一些方法但无法生成最终输出并以 URL 的形式查看输出..

def make_clickable(val):
    return '<a href="{}">{}</a>'.format(val,val)

dfIns.style.format(make_clickable)


import urllib

示例表格截图

enter image description here

示例数据框

S.No    Name    Amount
101     aaa     12256
102     bbb     8256
103     ccc     24525

最佳答案

文件“insurancedetails.html”存储在 aws 存储桶中,可以通过以下链接访问:https://stackoverflow-test-dataframe-to-html.s3.amazonaws.com/insurancedetails.html

在 AWS 中,您可以在存储桶中创建一个静态网站,或者您可以只上传一个 html 文件并提供链接。如果您想要一个像 ( https://insurancedetails.com/) 这样的域,那么还有一条完整的其他路径。但是创建一个 html 页面以通过数据框显示您的数据的问题可以像我稍后展示的那样完成。最后,您可以在任何接受 html 表格的应用程序中使用这些数据来创建所需的网页设计。

example-with-your-data.com

dfIns = pd.DataFrame(columns=['S.No', 'Name', 'Amount'])
s = [101,102,103]
n = ['aaa', 'bbb', 'ccc']
a = [12256, 8256, 24525]

dfIns['S.No'] = s
dfIns['Name'] = n
dfIns['Amount'] = a

#create table from dataframe; minimal formatting
table1_html = dfIns.to_html(index=False, border=2)
table1_html = table1_html.replace('<tr>', '<tr align="center">')

# beginning of html page
html_start = """<!DOCTYPE html>
                <html lang="en" dir="ltr">
                  <head>
                    <meta charset="utf-8">
                    <title></title>
                    <style>
                          table, th, td {
                              border: 1px solid black;
                              height: 22px;
                              padding: 3px;
                          }

                          table {
                              border-collapse: collapse;
                              height: 20px;
                          }
                          </style>
                    </head>
                  <body>
                 <br></br>"""

# closing html tags
html_end = """</body></html>"""

# put it all together
html_conc = html_start + """<caption><b>%s</b></caption>
                             """ % (table1_html) + html_end

#create the file
fn = "insurancedetails.html"
with open(fn, 'w+') as file:
    file.write(html_conc)
    file.close()

  # Note, I uploaded the file to s3, from local directory, but you can just click in your working directory and see the file that way too.

关于python - 如何使用将数据框显示为表格的常量 url 构建静态网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64625347/

相关文章:

html - 为什么 HTML5 header 元素需要 h 标签?

Python函数传递参数

python - 如何使用在每一行和每一列调用的函数有效地填充 numpy ndarray?

python - 在 Python 中将日期作为一行数据透视

python - 避免在 scikit learn StandardScaler 中缩放二进制列

python - 如何为 Pandas 时间序列制作动画?

python - pandas loc 的逻辑更复杂?

python - 为什么 flask 信号不能与 lambda 一起使用?

java - 使用 ajax 将模型和字符串传递给 Spring MVC

javascript - Stripe 集成 - 具有结帐重定向功能的付款意图