如何构建一个静态网站,其中包含一个以表格形式显示数据帧的固定url

2024-10-01 15:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我们有一个python脚本的输出,其形式为dataframe:“dfIns”

我将上面的数据框转换为html表

dfIns.to_html("insurancedetails.html")

是否有一种方法可以生成具有类似于(https://insurancedetails.com/)的URL的静态网站,并在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

Tags: to数据方法脚本formaturl示例dataframe
2条回答

这是一个例子: 首先,创建一个数据帧:

# importing pandas as pd 
import pandas as pd 
from IPython.display import HTML 
  
# creating the dataframe 
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',  
                            'Saurabh', 'Ujjawal'], 
                     
                   "Address": ['Patna', 'Delhi', 'Coimbatore', 
                               'Greater noida', 'Patna'], 
                     
                   "ID": [20123, 20124, 20145, 20146, 20147], 
                     
                   "Sell": [140000, 300000, 600000, 200000, 600000]}) 
  
print("Original DataFrame :") 
display(df) 

将数据框转换为Html表格:

result = df.to_html() 
print(result)

让我们编写将数据帧转换为HTML文件的脚本:

html = df.to_html() 

# write html to file 
text_file = open("index.html", "w") 
text_file.write(html) 
text_file.close() 

让我们以带条表格的形式显示HTML数据

HTML(df.to_html(classes='table table-striped'))

请在以下网站上阅读更多信息: https://www.geeksforgeeks.org/how-to-render-pandas-dataframe-as-html-table/

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

在AWS中,您可以在bucket中创建静态网站,也可以只上载html文件并提供链接。如果您想要一个类似(https://insurancedetails.com/)的域,那么有一个完整的其他路径。但是,创建一个html页面以通过dataframe显示数据的问题可以像我稍后介绍的那样完成。最后,您可以使用任何接受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.

相关问题 更多 >

    热门问题