Легкое приложение прогноза погоды
Сейчас ни чем не удивить приложениями как todo лист,прогнозом погоды.Но это идеальный пример навыков программиста.
Библиотеки:
Requests-библиотека отвечающая за парсинг.Документация
pip install requests
Flask-фреймворк отвечает за Backend.Документация
pip install Flask
Код
Сначала я сделал запросы на api:Дальше идем использовать данные уже в Flask.
import requests
city = input('Input city:')
url = 'https://api.openweathermap.org/data/2.5/weather?q=' +city+ units=metric lang=ru appid=79d1ca96933b0328e1c7e3e7a26cb347'
weather_data = requests.get(url).json()
temperature = round(weather_data['main']['temp'])
temperature_feels = round(weather_data['main']['feels_like'])
Но я хотел сделать все автоматизировано как у других сайтов.Находить город через IP адрес.
def get_ip():
response = requests.get('https://api64.ipify.org?format=json').json()
return response["ip"]
def get_location():
ip_address = get_ip()
response = requests.get(f'https://ipapi.co/{ip_address}/json/').json()
city=response.get('city')
return city
Теперь можем перевести это в один код:
from flask import *
import requests
app = Flask(__name__)
def get_ip():
response = requests.get('https://api64.ipify.org?format=json').json()
return response["ip"]
def get_location():
ip_address = get_ip()
response = requests.get(f'https://ipapi.co/{ip_address}/json/').json()
city=response.get('city')
return city
@app.route("/")
def home():
data = get_location()
city = str(data)
url = 'https://api.openweathermap.org/data/2.5/weather?q=' + city + '&units=metric&lang=ru&appid=79d1ca96933b0328e1c7e3e7a26cb347'
weather_data = requests.get(url).json()
temperature = round(weather_data['main']['temp'])
temperature_feels = round(weather_data['main']['feels_like'])
return render_template("weather.html", data=data, date=temperature, date_f=temperature_feels)
if __name__ == "__main__":
app.run(debug=True)
Теперь html css снизу будет фото как можно подключить css:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/weather.css') }}">
<title>Title</title>
</head>
<body>
<h1>Weather Forcast:{{data}}</h1>
<div class="app">
<h1>{{ date }}°C</h1>
<h1>{{ date_f }}°C</h1>
</div>
</body>
</html>
*{
font-family: 'Poppins', sans-serif;
color: antiquewhite;
}
.app{
text-align:center;
background:skyblue;
border: 2px solid ;
border-radius: 12px;
padding: 5px;
}
body{
background:darkslateblue;
}
Теперь результат всей работы:
Начать дискуссию