Сurrent weather and forecast - OpenWeatherMap
Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on global and local weather models, satellites, radars and vast network of weather stations. how to obtain APIs (subscriptions with di
openweathermap.org
api.openweathermap.org/data/2.5/weather?q=daegu&appid={API key}
-터미널 설치
pip install requests
- app.py
추가
@app.route('/')
def home():
city = 'daegu'
appid = 'e5d4ba22d1c0aae4130753ea87c69eec'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={appid}'
res = requests.get(url)
# weather_data = json.loads(res.text)
weather_data = res.json()
description = weather_data["weather"][0]["description"]
icon = weather_data["weather"][0]["icon"]
temp = weather_data["main"]["temp"] - 273
temp = round(temp, 1)
return render_template('index.html', description=description, icon=icon, temp=temp)
@app.route('/weather', methods=['GET'])
def weather():
city = 'daegu'
appid = 'e5d4ba22d1c0aae4130753ea87c69eec'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={appid}'
res = requests.get(url)
weather_data = res.json()
print("weather_data: ", weather_data)
print("날씨: ", weather_data["weather"][0]["description"])
description = weather_data["weather"][0]["description"]
icon = weather_data["weather"][1]["icon"]
print("온도: ", weather_data["main"]["temp"]-273)
temp = weather_data["main"]["temp"] - 273
return render_template('weather.html', description=description, icon=icon, temp=temp)
- weather.html
{% extends "layout.html" %}
{% block content %}
<div class="weather box_shadow" style="width:250px;">
<div style="display: block">
{{ description }}
</div>
<div style="display: inline-block; ">
<img src="http://openweathermap.org/img/wn/{{ icon }}@2x.png">
</div>
<div class="weather_body" style="display: block;">
<div class="temp" style="display: block;">
{{temp}}°
</div>
</div>
</div>
{% endblock %}
-index.html
{% extends "layout.html" %}
{% block title %}
홈
{% endblock %}
{% block weather %}
<img src="http://openweathermap.org/img/wn/{{ icon }}@2x.png">
{{ description }}
{{ temp }}°
{% endblock %}
{% block content %}
<div class="jumbotron">
<h1 class="display-4">MongoDB 프로젝트</h1>
<p class="lead">
This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
<hr class="my-4">
<ul>
<li>
flask 기반의 MongoDB 관리 시스템 입니다.
</li>
<li>
flask 기반의 MongoDB 관리 시스템 입니다.
</li>
</ul>
</div>
{% endblock %}
'mongodb' 카테고리의 다른 글
날짜, 위경도, 구글글꼴 (0) | 2021.01.25 |
---|---|
몽고디비- 로그인 수정중 (0) | 2021.01.22 |
몽고디비 7일차 - book_store 시간, 검색기능 추가 (0) | 2021.01.22 |
몽고디비 7일차 -atlas (0) | 2021.01.22 |
몽고디비- 홈페이지 ip:포트번호 로 접속하기 (0) | 2021.01.21 |