0% found this document useful (0 votes)
3 views4 pages

Import Requests

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Import Requests

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import requests

from bs4 import BeautifulSoup


import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from PIL import Image, ImageTk

class DataScraper:
def __init__(self, url):
self.url = url

def fetch_data(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, "html.parser")

countries = []
gold = []
silver = []
bronze = []

table_rows = soup.select("table tbody tr")


for row in table_rows:
cols = row.find_all("td")

country = cols[1].text.strip()
countries.append(country)
gold.append(int(cols[2].text.strip()))
silver.append(int(cols[3].text.strip()))
bronze.append(int(cols[4].text.strip()))

data = pd.DataFrame({
"Country": countries,
"Gold": gold,
"Silver": silver,
"Bronze": bronze
})
data["Total"] = data["Gold"] + data["Silver"] + data["Bronze"]
return data

class DataVisualizer:
@staticmethod
def show_country_chart(data, country):
country_data = data[data["Country"] == country]

if country_data.empty:
print(f"No data available for {country}.")
return

medal_types = ["Gold", "Silver", "Bronze"]


counts = country_data[["Gold", "Silver", "Bronze"]].values[0]

new_window = tk.Toplevel()
new_window.title(f"{country} Medal Count")

fig, ax = plt.subplots(figsize=(5, 4))


ax.bar(medal_types, counts, color=["gold", "silver", "#cd7f32"])
ax.set_title(f"Medals Count for {country}")
ax.set_xlabel("Medal Type")
ax.set_ylabel("Count")

canvas = FigureCanvasTkAgg(fig, master=new_window)


canvas.draw()
canvas.get_tk_widget().pack()

new_window.mainloop()

@staticmethod
def plot_top10_analytics(data):
countries = data["Country"]
gold = data["Gold"]
silver = data["Silver"]
bronze = data["Bronze"]
total = data["Total"]

new_window = tk.Toplevel()
new_window.title("Top 10 Performing Countries Analytics")

fig, axs = plt.subplots(2, 2, figsize=(12, 8))


fig.suptitle("Top 10 Performing Countries Analytics", fontsize=16)

axs[0, 0].pie(gold, labels=countries, autopct='%1.1f%%', startangle=90)


axs[0, 0].set_title("Gold Medals")

axs[0, 1].pie(silver, labels=countries, autopct='%1.1f%%', startangle=90)


axs[0, 1].set_title("Silver Medals")

axs[1, 0].pie(bronze, labels=countries, autopct='%1.1f%%', startangle=90)


axs[1, 0].set_title("Bronze Medals")

axs[1, 1].plot(countries, total, marker='o', color='b')


axs[1, 1].set_title("Total Medals")
axs[1, 1].set_xlabel("Countries")
axs[1, 1].set_ylabel("Number of Medals")
axs[1, 1].grid(True)

canvas = FigureCanvasTkAgg(fig, master=new_window)


canvas.draw()
canvas.get_tk_widget().pack()

new_window.mainloop()

class OlympicsAppGUI:
def __init__(self):
self.scraper = None
self.visualizer = DataVisualizer()
self.data = None

self.root = tk.Tk()
self.root.title("Olympics 2024")
self.root.geometry("500x700")
self.root.configure(bg="white")

title_label = tk.Label(self.root, text="Olympics 2024", font=("Arial", 18,


"bold"), bg="white")
title_label.pack(pady=10)

self.add_image()

self.url_entry = tk.Entry(self.root, width=50, font=("Arial", 12),


justify="center")
self.url_entry.pack(pady=10)

show_list_button = tk.Button(self.root, text="Show List", font=("Arial",


12), command=self.show_list)
show_list_button.pack(pady=10)

self.country_listbox = tk.Listbox(self.root, height=15, width=50,


font=("Arial", 10), selectmode="single")
self.country_listbox.pack(pady=15)

instruction_label = tk.Label(self.root, text="Click on a country to see


detailed medals:", font=("Arial", 12), bg="white")
instruction_label.pack(pady=5)

show_chart_button = tk.Button(self.root, text="Show Chart of selected


country", font=("Arial", 12),
command=self.show_country_chart)
show_chart_button.pack(pady=10)

show_top10_button = tk.Button(self.root, text="Show top 10 performing


countries analytics", font=("Arial", 12),
command=self.show_top10_analytics)
show_top10_button.pack(pady=10)

self.root.mainloop()

def add_image(self):
img = Image.open("indir.png")
img = img.resize((150, 75))
photo = ImageTk.PhotoImage(img)
img_label = tk.Label(self.root, image=photo, bg="white")
img_label.image = photo
img_label.pack(pady=10)

def show_list(self):
url = self.url_entry.get()
if url.strip() == "":
print("URL cannot be empty!")
return

self.scraper = DataScraper(url)
self.data = self.scraper.fetch_data()
self.country_listbox.delete(0, tk.END)
for country in self.data["Country"]:
self.country_listbox.insert(tk.END, country)

def show_country_chart(self):
selected_country = self.country_listbox.get(tk.ACTIVE)
if selected_country and self.data is not None:
self.visualizer.show_country_chart(self.data, selected_country)

def show_top10_analytics(self):
if self.data is not None:
top_10_data = self.data.nlargest(10, "Total")
self.visualizer.plot_top10_analytics(top_10_data)

if __name__ == "__main__":
OlympicsAppGUI()

You might also like