bike-ride-day / app.py
muhammadshaheryar's picture
Update app.py
863e983 verified
import requests
import streamlit as st
# Define the API key and base URL
API_KEY = 'your_openweathermap_api_key' # Replace with your OpenWeatherMap API key
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather'
# Function to fetch weather data for Karachi
def get_weather(city='Karachi'):
params = {
'q': city,
'appid': API_KEY,
'units': 'metric' # Get temperature in Celsius
}
response = requests.get(BASE_URL, params=params)
if response.status_code == 200:
return response.json()
else:
return None
# Function to compute a score based on the weather conditions
def get_bike_ride_score(weather_data):
# Example logic to determine the weather score
# You can customize this based on your preferences for biking
temperature = weather_data['main']['temp']
humidity = weather_data['main']['humidity']
weather_condition = weather_data['weather'][0]['main']
score = 0
# Temperature: Ideal range for biking is 15°C to 25°C
if 15 <= temperature <= 25:
score += 50
elif 10 <= temperature < 15 or 25 < temperature <= 30:
score += 30
else:
score += 10
# Humidity: Ideally below 70% for comfort
if humidity <= 60:
score += 20
elif 60 < humidity <= 80:
score += 10
else:
score += 5
# Weather condition: Ideal is clear weather, less ideal are rain or storms
if weather_condition in ['Clear', 'Clouds']:
score += 30
elif weather_condition in ['Rain', 'Thunderstorm']:
score -= 30
else:
score -= 20
# Make sure the score is between 0 and 100
score = max(0, min(100, score))
return score
# Streamlit UI to display the results
def main():
st.title("Bike Ride Day Finder")
st.write("This app shows the best days to ride your bike in Karachi based on the weather conditions.")
# Get weather data for Karachi
weather_data = get_weather('Karachi')
if weather_data:
score = get_bike_ride_score(weather_data)
# Display weather information
st.subheader(f"Weather for Karachi: {weather_data['weather'][0]['description']}")
st.write(f"Temperature: {weather_data['main']['temp']}°C")
st.write(f"Humidity: {weather_data['main']['humidity']}%")
# Display the bike ride score on a scale of 0 to 100
st.subheader("Bike Ride Score")
st.progress(score) # Display the progress bar
if score > 70:
st.success("Great day for biking!")
elif score > 40:
st.warning("Okay for biking, but be cautious.")
else:
st.error("Not ideal for biking today.")
else:
st.error("Unable to fetch weather data.")
if __name__ == '__main__':
main()