How to build a static website with a constant url that displays dataframe as a table

Viewed 571

we have one of the output of python script in the form of dataframe : "dfIns"

I converted the above dataframe into html table

dfIns.to_html("insurancedetails.html")

is there a way to generate static website that have URL like (https://insurancedetails.com/) and display the dataframe/html output in the url web page which should be accessible to any one in their browsers anytime.

I tried some of the below approached but unable to generate final output and see the output in the form of URL..

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

dfIns.style.format(make_clickable)


import urllib

Sample table screenshot

enter image description here

Sample dataframe

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

The file "insurancedetails.html" is stored in an aws bucket with the which can be accessed via this link: https://stackoverflow-test-dataframe-to-html.s3.amazonaws.com/insurancedetails.html

In AWS you can create a static website in bucket or you can just upload an html file and provide links. If you want a domain like (https://insurancedetails.com/), then there's a whole other path for that. But the question of creating an html page to show your data via dataframe can be done like I show later. In the end, you can use the data in any application that accepts an html table to create the desired web page design.

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.

This is an example: First, create a Dataframe:

# 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) 

Convert Dataframe to Html table:

result = df.to_html() 
print(result)

Let write the script for convert DataFrame into HTML file:

html = df.to_html() 

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

Let’s display HTML data in the form of a table-striped

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

Read more about this in: https://www.geeksforgeeks.org/how-to-render-pandas-dataframe-as-html-table/

Related