Import Requests
Import Requests
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 = []
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
new_window = tk.Toplevel()
new_window.title(f"{country} Medal Count")
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")
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")
self.add_image()
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()