- CodeCraft by Dr. Christine Lee
- Posts
- Elevate Your Olympics Stats with New Filters...
Elevate Your Olympics Stats with New Filters...
Supercharge Your Olympics Leaderboard with Filtering! 🥇🔍
In this post, we're taking your Olympics leaderboard to the next level by adding advanced features like filtering by country or sport.
These features will make your app more dynamic and user-friendly, allowing users to dive into specific data with ease.
Let’s get started!
Step 1: Update Your Data Structure 🗂️
To make filtering easy, we need to organize our data effectively. We'll start by ensuring that our CSV file has columns for the country, sport, and the number of gold medals.
Here's a quick example of how your CSV file might look:
Country,Sport,Gold Medals
USA,Swimming,30
China,Gymnastics,15
Japan,Judo,10
Germany,Cycling,8
Australia,Swimming,7
Load this data into a dictionary just like before, but this time we’ll include the country and sport information.
The name of the CSV file is medals.csv
import csv
leaderboard = {}
# Load data from the CSV file
with open(medals.csv', mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
key = (row['Country'], row['Sport'])
leaderboard[key] = int(row['Gold Medals'])
This structure will allow us to easily filter the data later on.
Step 2: Create the Filtering Interface 🎛️
Now that our data is organized, let's build the GUI components that will allow users to filter the leaderboard.
import PySimpleGUI as sg
layout = [
[sg.Text('Choose a country or sport to filter')],
[sg.Text('Country:'), sg.Combo(values=list(set([key[0] for key in leaderboard.keys()])), key='-COUNTRY-', enable_events=True)],
[sg.Text('Sport:'), sg.Combo(values=list(set([key[1] for key in leaderboard.keys()])), key='-SPORT-', enable_events=True)],
[sg.Button('Show Results')],
[sg.Text('', key='-RESULT-', size=(40, 10))]
]
window = sg.Window('Olympics Leaderboard', layout)
This simple GUI lets the user select a country or sport from the dropdown menus.
GUI: Text, Dropdown List and Button
Step 3: Implement the Filtering Logic 🎯
Now, let’s add the code to filter and display the results based on the user’s selection.
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Show Results':
country = values['-COUNTRY-']
sport = values['-SPORT-']
results = ""
for (c, s), medals in leaderboard.items():
if (country and c country) or (sport and s sport):
results += f'{c} - {s}: {medals} Gold Medals\n'
if not results:
results = 'No results found'
window['-RESULT-'].update(results)
window.close()
In this code:
Country and Sport Selection: Users can select a country, a sport, or both.
Filtering Logic: The app checks if the selected country or sport matches any records in the leaderboard and displays the corresponding results.
Displaying Results: The results are shown in the window, making it easy for users to see the filtered data.
Step 4: Save and Run Your App! 🏃♂️
Once you’ve implemented these steps, save your script and run it.
Your app should now allow users to filter the leaderboard by country or sport, showing only the data they’re interested in.
Wrapping Up 🎉
Congratulations!
You’ve just enhanced your Olympics leaderboard with filtering features.
Not only does this make your app more interactive, but it also makes it incredibly useful for users who want to explore specific data points.
Keep experimenting with different features, and stay tuned for more exciting enhancements in future posts!
Coding with a Smile 🤣 😂
Module Mix-Up:
Importing the wrong module is like calling the wrong number. You expect to talk to pandas, but instead, you get NumPy, and you have to politely hang up and try again.
Let’s Inspire Future AI Coders Together! ☕
I’m excited to continue sharing my passion for Python programming and AI with you all. If you’ve enjoyed the content and found it helpful, do consider supporting my work with a small gift. Just click the link below to make a difference – it’s quick, easy, and every bit helps and motivates me to keep creating awesome contents for you.
Thank you for being amazing!
Ready for More Python Fun? 📬
Subscribe to our newsletter now and get a free Python cheat sheet! 📑 Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep learning, keep coding 👩💻👨💻, and keep discovering new possibilities! 💻✨
Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!
Stay tuned for our next exciting project in the following edition!
Happy coding!🚀📊✨
🎉 We want to hear from you! 🎉 How do you feel about our latest newsletter? Your feedback will help us make it even more awesome! |