-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (69 loc) · 2.91 KB
/
Copy pathapp.py
File metadata and controls
82 lines (69 loc) · 2.91 KB
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
from flask import Flask, render_template, request
import requests
from datetime import datetime
app = Flask(__name__)
API_KEY = '28c4f5d4fa37ba285317f003dd06f60a'
def get_weather(city):
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
weather = {
'city': data['name'],
'temperature': data['main']['temp'],
'humidity': data['main']['humidity'],
'description': data['weather'][0]['description'].title(),
'icon': data['weather'][0]['icon']
}
return weather
else:
return None
def get_forecast(city):
url = f'https://api.openweathermap.org/data/2.5/forecast/daily?q={city}&cnt=7&appid={API_KEY}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
forecast = []
for day in data['list']:
forecast.append({
'date': datetime.fromtimestamp(day['dt']).strftime('%A, %d %B'),
'temperature_day': day['temp']['day'],
'temperature_night': day['temp']['night'],
'humidity': day['humidity'],
'description': day['weather'][0]['description'].title(),
'icon': day['weather'][0]['icon']
})
return forecast
else:
return None
def get_crop_advice(crop, temperature, humidity, soil):
# Placeholder logic for crop advisory
advice = f"For {crop}, ensure the soil is {soil}. Optimal temperature is around {temperature}°C with humidity at {humidity}%."
return advice
def get_fertilizer_recommendation(crop, soil, temperature, humidity):
# Placeholder logic for fertilizer recommendation
recommendation = f"Based on the {soil} soil and current conditions, it's recommended to use a balanced NPK fertilizer for {crop}."
return recommendation
@app.route('/', methods=['GET', 'POST'])
def index():
weather = None
forecast = None
advisory = ""
fertilizer = ""
# Get current server time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if request.method == 'POST':
city = request.form['city']
crop = request.form['crop']
soil = request.form['soil']
weather = get_weather(city)
forecast = get_forecast(city)
if weather:
advisory = get_crop_advice(crop, weather['temperature'], weather['humidity'], soil)
fertilizer = get_fertilizer_recommendation(crop, soil, weather['temperature'], weather['humidity'])
else:
advisory = "Weather data unavailable."
fertilizer = ""
return render_template('index.html', weather=weather, forecast=forecast, advisory=advisory, fertilizer=fertilizer, current_time=current_time)
if __name__ == '__main__':
app.run(debug=True)