0

I have python code that shows a data frame. How can I link the python code to show it as a table in an HTML webpage and use CSS to edit this table? I am using Vs code.

Here is my python code for creating a dataframe.

import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "https://www.worldometers.info/coronavirus/country/Austria/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
cases, deaths, recovered = soup.select(".maincounter-number")
active_cases, closed_cases = soup.select(".number-table-main")
active_cases_mild, active_cases_serious, _, _ = soup.select(".number-table")
df = pd.DataFrame(
 {
 "Coronavirus Cases": [cases.get_text(strip=True)],
 "Deaths": [deaths.get_text(strip=True)],
 "Recovered": [recovered.get_text(strip=True)],
 "Currently infected": [active_cases.get_text(strip=True)],
 "Closed cases": [closed_cases.get_text(strip=True)],
 "Active cases (mild)": [active_cases_mild.get_text(strip=True)],
 "Active cases (serious)": [active_cases_serious.get_text(strip=True)],
 }
)
print(df)

You can show the table in simple html code just like that for example:

<!DOCTYPE html>
<html lang="en">
<body>
 "Test"
</body>
</html>
asked Apr 21, 2021 at 20:59
3
  • You are not showing any tables in html Commented Apr 21, 2021 at 21:04
  • 1
    raw and fast way is: pandas.pydata.org/docs/reference/api/… Commented Apr 21, 2021 at 21:04
  • @Matiiss i am just showing the python code.How can i show that code as a table in HTML webpage? Commented Apr 21, 2021 at 21:08

1 Answer 1

1

I guess this is an option but there certainly have to be better options:

import requests
from bs4 import BeautifulSoup
url = "https://www.worldometers.info/coronavirus/country/Austria/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
cases, deaths, recovered = soup.select(".maincounter-number")
active_cases, closed_cases = soup.select(".number-table-main")
active_cases_mild, active_cases_serious, _, _ = soup.select(".number-table")
lst = [cases, deaths, recovered, active_cases, closed_cases, active_cases_mild, active_cases_serious]
table_data = ''
for data in lst:
 data = data.text.replace('\n', '')
 table_data += f'<td>{data}</td>'
html = f"""
<!doctype html>
<html>
<head><title>Test</title></head>
<body>
<table>
<tr>
 <th>Coronavirus Cases</th>
 <th>Deaths</th>
 <th>Recovered</th>
 <th>Currently infected</th>
 <th>Closed cases</th>
 <th>Active cases (mild)</th>
 <th>Active cases (serious)</th>
</tr>
<tr>{table_data}</tr>
</table>
</body>
</html>
"""
print(html)
with open('test.html', 'w') as file:
 file.write(html)
answered Apr 21, 2021 at 21:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.