File size: 2,829 Bytes
863e983
f91205e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95


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()