※既にanaconda環境でFlask,folimu環境構築済みであることが前提です。
1.Flask環境に下記pythonファイル、htmlを組み込む
1-1.ファイル
(flask-web) [xxx Flask]$ tree
.
├── flask-test
│ ├── folium_heat_map.py
│ └── templates
│ └── folium_heat_map.html
1-2.folium_heat_map.py
##############################################
# foliumを使ってheatmap表示する
##############################################
import folium
from folium.plugins import HeatMap
from flask import Flask,render_template,request
#インスタンス生成
app = Flask(__name__)
@app.route('/folium_heatmap')
def foliummap():
#地図の中心の緯度、経度
start_cords=(35.681236,139.767125)
folium_map = folium.Map(location=start_cords, zoom_start=15)
#ヒートマップ用プロット作成 ***************
hiatdata = list()
location_1 = (35.681236,139.767125)
location_2 = (35.6762009,139.7666254)
for i in range(5):
hiatdata.append(location_1)
for i in range(100):
hiatdata.append(location_2)
#HeatMap作成
HeatMap(hiatdata, radius=30).add_to(folium_map)
#保存
folium_map.save('templates/heat_map.html')
return render_template('folium_heat_map.html')
if __name__ == "__main__":
#Webサーバ立ち上げ
app.debug=True
app.run(host='xxx.xxx.xxx.xxx')
1-3.folium_heat_map.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>folium Map Test</title>
</head>
<body>
<div style="height:768px; width:1024px;">{% include "heat_map.html"%}</div>
</body>
</html>
2.実行する
(flask-test) [xxx flask-test]$ python folium_heat_map.py
* Serving Flask app "folium_heat_map" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://xxx.xxx.xxx.xxx:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 217-574-955