0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

flaskでfoliumを使いHeatマップ表示

Last updated at Posted at 2023-03-24

※既に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

3.ブラウザで確認する

http://xxx.xxx.xxx.xxx:5000/folium_heatmap

heatmap.PNG

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?