Spaces:
Configuration error
Configuration error
Upload 5 files
Browse files- Procfile.txt +1 -0
- README.md +1 -13
- requirements.txt +4 -0
- setup.sh.txt +9 -0
- weather_app.py +140 -0
Procfile.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: sh setup.sh && streamlit weather_app.py
|
README.md
CHANGED
|
@@ -1,13 +1 @@
|
|
| 1 |
-
|
| 2 |
-
title: Streamlit Weather
|
| 3 |
-
emoji: 📊
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: streamlit
|
| 7 |
-
sdk_version: 1.19.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: apache-2.0
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
"# Streamlit_weather"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==0.85.1
|
| 2 |
+
requests==2.26.0
|
| 3 |
+
pandas==1.3.3
|
| 4 |
+
matplotlib==3.4.3
|
setup.sh.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mkdir -p ~/.streamlit/
|
| 2 |
+
echo "[general]
|
| 3 |
+
email = \"rahulmishak2323@gmail.com\"
|
| 4 |
+
" > ~/.streamlit/credentials.toml
|
| 5 |
+
echo "[server]
|
| 6 |
+
headless = true
|
| 7 |
+
port = $PORT
|
| 8 |
+
enableCORS = false
|
| 9 |
+
" > ~/.streamlit/config.toml
|
weather_app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|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pip install streamlit
|
| 11 |
+
|
| 12 |
+
import streamlit as st
|
| 13 |
+
import requests
|
| 14 |
+
import pandas as pd
|
| 15 |
+
import random
|
| 16 |
+
import seaborn as sns
|
| 17 |
+
import matplotlib.pyplot as plt
|
| 18 |
+
import time
|
| 19 |
+
|
| 20 |
+
# Geocoding API credentials
|
| 21 |
+
geocoding_api_key = 'eac461277beb2ad0bf0b0b629a05e04d'
|
| 22 |
+
|
| 23 |
+
# Function to get latitude and longitude coordinates for a given location
|
| 24 |
+
def geocode_location(location):
|
| 25 |
+
# Geocoding API request
|
| 26 |
+
url = f'http://api.positionstack.com/v1/forward?access_key={geocoding_api_key}&query={location}'
|
| 27 |
+
response = requests.get(url)
|
| 28 |
+
data = response.json()
|
| 29 |
+
|
| 30 |
+
if 'data' in data and len(data['data']) > 0:
|
| 31 |
+
result = data['data'][0]
|
| 32 |
+
latitude = result['latitude']
|
| 33 |
+
longitude = result['longitude']
|
| 34 |
+
return latitude, longitude
|
| 35 |
+
else:
|
| 36 |
+
return None, None
|
| 37 |
+
|
| 38 |
+
# Set page configuration
|
| 39 |
+
st.set_page_config(page_title='Weather Forecast', page_icon=':partly_sunny:')
|
| 40 |
+
|
| 41 |
+
# Set application layout
|
| 42 |
+
st.title('Weather Forecast')
|
| 43 |
+
col1, col2 = st.columns([2, 3])
|
| 44 |
+
|
| 45 |
+
# Get user input for location
|
| 46 |
+
location = col1.text_input('Enter a location')
|
| 47 |
+
latitude, longitude = geocode_location(location)
|
| 48 |
+
|
| 49 |
+
if latitude and longitude:
|
| 50 |
+
# Make a request to the Open-Meteo API
|
| 51 |
+
url = 'https://api.open-meteo.com/v1/forecast'
|
| 52 |
+
params = {
|
| 53 |
+
'latitude': latitude,
|
| 54 |
+
'longitude': longitude,
|
| 55 |
+
'current_weather': 'true',
|
| 56 |
+
'hourly': 'temperature_2m,relativehumidity_2m,windspeed_10m'
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
response = requests.get(url, params=params)
|
| 60 |
+
data = response.json()
|
| 61 |
+
|
| 62 |
+
# Extract current weather information
|
| 63 |
+
current_weather = data['current_weather']
|
| 64 |
+
temperature = current_weather['temperature']
|
| 65 |
+
weather_code = current_weather['weathercode']
|
| 66 |
+
windspeed = current_weather['windspeed']
|
| 67 |
+
wind_direction = current_weather['winddirection']
|
| 68 |
+
|
| 69 |
+
# Extract hourly forecast information
|
| 70 |
+
hourly_forecast = data['hourly']
|
| 71 |
+
forecast_times = hourly_forecast['time']
|
| 72 |
+
temperatures = hourly_forecast['temperature_2m']
|
| 73 |
+
relative_humidity = hourly_forecast['relativehumidity_2m']
|
| 74 |
+
windspeeds = hourly_forecast['windspeed_10m']
|
| 75 |
+
|
| 76 |
+
# Convert forecast data to a DataFrame
|
| 77 |
+
forecast_data = pd.DataFrame({
|
| 78 |
+
'Time': forecast_times,
|
| 79 |
+
'Temperature (°C)': temperatures,
|
| 80 |
+
'Relative Humidity (%)': relative_humidity,
|
| 81 |
+
'Windspeed (m/s)': windspeeds
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
# Display current weather information
|
| 85 |
+
col2.subheader('Current Weather')
|
| 86 |
+
col2.write(f'Location: {location}')
|
| 87 |
+
col2.write(f'Temperature: {temperature}°C')
|
| 88 |
+
col2.write(f'Weather Code: {weather_code}')
|
| 89 |
+
col2.write(f'Windspeed: {windspeed} m/s')
|
| 90 |
+
col2.write(f'Wind Direction: {wind_direction}°')
|
| 91 |
+
|
| 92 |
+
# Display interactive DataFrame using Streamlit's data table
|
| 93 |
+
col2.subheader('Hourly Forecast')
|
| 94 |
+
col2.dataframe(forecast_data)
|
| 95 |
+
|
| 96 |
+
# Add line charts to visualize temperature and humidity
|
| 97 |
+
col2.subheader('Temperature and Humidity')
|
| 98 |
+
col2.line_chart(forecast_data[['Temperature (°C)', 'Relative Humidity (%)']])
|
| 99 |
+
|
| 100 |
+
# Add a bar chart to visualize windspeed
|
| 101 |
+
col2.subheader('Windspeed')
|
| 102 |
+
col2.bar_chart(forecast_data['Windspeed (m/s)'])
|
| 103 |
+
|
| 104 |
+
# Add correlation chart
|
| 105 |
+
col2.subheader('Correlation Chart')
|
| 106 |
+
correlation = forecast_data[['Temperature (°C)', 'Relative Humidity (%)', 'Windspeed (m/s)']].corr()
|
| 107 |
+
fig, ax = plt.subplots()
|
| 108 |
+
ax = sns.heatmap(correlation, annot=True, cmap='coolwarm', square=True)
|
| 109 |
+
col2.pyplot(fig)
|
| 110 |
+
|
| 111 |
+
# Add live weather updates
|
| 112 |
+
col2.subheader('Live Weather Updates')
|
| 113 |
+
col2.write('Updating every 10 seconds...')
|
| 114 |
+
live_update = col2.empty()
|
| 115 |
+
|
| 116 |
+
while True:
|
| 117 |
+
# Make a new request for live weather updates
|
| 118 |
+
response = requests.get(url, params=params)
|
| 119 |
+
data = response.json()
|
| 120 |
+
current_weather = data['current_weather']
|
| 121 |
+
temperature = current_weather['temperature']
|
| 122 |
+
windspeed = current_weather['windspeed']
|
| 123 |
+
|
| 124 |
+
# Update the live weather updates section
|
| 125 |
+
live_update.write(f'Temperature: {temperature}°C')
|
| 126 |
+
live_update.write(f'Windspeed: {windspeed} m/s')
|
| 127 |
+
live_update.write('---')
|
| 128 |
+
time.sleep(120) # Wait for 120 seconds before the next update
|
| 129 |
+
|
| 130 |
+
# Add export to CSV and JSON buttons
|
| 131 |
+
col2.subheader('Export Data')
|
| 132 |
+
if col2.button('Export to CSV'):
|
| 133 |
+
forecast_data.to_csv('forecast_data.csv', index=False)
|
| 134 |
+
col2.write('Data exported to forecast_data.csv')
|
| 135 |
+
if col2.button('Export to JSON'):
|
| 136 |
+
forecast_data.to_json('forecast_data.json', orient='records')
|
| 137 |
+
col2.write('Data exported to forecast_data.json')
|
| 138 |
+
else:
|
| 139 |
+
col2.warning('Invalid location. Please enter a valid location.')
|
| 140 |
+
|