-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
40 lines (34 loc) · 1.33 KB
/
app.py
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
from flask import Flask, render_template, request, jsonify
import requests
import os
from geopy.geocoders import Nominatim
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/weather', methods=['POST'])
def weather():
city = request.form['city']
api_key = os.getenv('OPENWEATHERMAP_API_KEY')
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric'
response = requests.get(url)
weather_data = response.json()
return render_template('index.html', weather=weather_data)
@app.route('/forecast', methods=['POST'])
def forecast():
city = request.form['city']
api_key = os.getenv('OPENWEATHERMAP_API_KEY')
url = f'http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={api_key}&units=metric'
response = requests.get(url)
forecast_data = response.json()
return render_template('index.html', forecast=forecast_data)
@app.route('/geolocation', methods=['POST'])
def geolocation():
geolocator = Nominatim(user_agent="weather_app")
location = geolocator.geocode(request.form['location'])
if location:
return jsonify({'latitude': location.latitude, 'longitude': location.longitude})
else:
return jsonify({'error': 'Location not found'}), 404
if __name__ == '__main__':
app.run(debug=True)