- 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! |