diff options
author | Connor Frank <conjfrnk@gmail.com> | 2024-04-18 15:13:09 -0400 |
---|---|---|
committer | Connor Frank <conjfrnk@gmail.com> | 2024-04-18 15:13:09 -0400 |
commit | f60055ae4ba007d933d78bab15138f37edd58337 (patch) | |
tree | 69e55ee9f0bac789bafce95d30f3fc10401e5c63 /euler/update_table.py | |
parent | 81b75f3e9c04036161a9abfe2422e733bf760311 (diff) |
Project Euler
Diffstat (limited to 'euler/update_table.py')
-rw-r--r-- | euler/update_table.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/euler/update_table.py b/euler/update_table.py new file mode 100644 index 0000000..8fe74b2 --- /dev/null +++ b/euler/update_table.py @@ -0,0 +1,72 @@ +import csv +from bs4 import BeautifulSoup + + +def read_csv(file_path): + with open(file_path, mode="r", newline="") as file: + reader = csv.DictReader(file) + problems = list(reader) + return problems + + +def create_table_content(problems): + content = [] + current_table = [] + row = [] + count = 0 + for problem in problems: + if len(row) == 25: + current_table.append(row.copy()) + row = [] + if count == 100: # Start a new table after every 100 problems + content.append(current_table.copy()) + current_table = [] + count = 0 + status_class = "completed" if problem["Solve Status"] == "1" else "open" + desc = problem["Description"] + id = problem["ID"] + row.append( + f'<td class="{status_class}"><a href="https://projecteuler.net/problem={id}" title="{desc}">{id}</a></td>' + ) + count += 1 + if row: + current_table.append(row) + if current_table: + content.append(current_table) + return content + + +def update_html(file_path, tables_content): + with open(file_path, "r", encoding="utf-8") as file: + soup = BeautifulSoup(file, "html.parser") + + # Remove old progress tables + container = soup.find("div", class_="container") + if container: + for table in container.find_all("table", class_="progress"): + table.decompose() + else: + container = soup.new_tag("div", **{"class": "container"}) + soup.body.append(container) # Append container to body if it does not exist + + # Append new tables to the container + for table_content in tables_content: + new_table = soup.new_tag("table", **{"class": "progress"}) + for row in table_content: + tr = soup.new_tag("tr") + for td in row: + tr.append( + BeautifulSoup(td + "\n", "html.parser") + ) # Adding newline for better formatting + new_table.append(tr) + container.append(new_table) + + # Write the updated HTML back to the file + with open(file_path, "w", encoding="utf-8") as file: + file.write(str(soup)) + + +# Usage +problems = read_csv("pe_minimal_problems.csv") +tables_content = create_table_content(problems) +update_html("stats.html", tables_content) |