我已经使用folium库创建了一个地图Stamen Toner
并且效果很好。但是当我尝试刷新数据以显示其他结果时,它会继续显示以前的结果。
from flask import Flask, request, render_template,make_response,Response
import io
import csv
import pandas as pd
import numpy as np
from flask_googlemaps import GoogleMaps
from flask_googlemaps import Map,icons
import folium
app = Flask(__name__, static_url_path = "/tmp", static_folder = "tmp")
@app.route('/')
def my_form():
return render_template('myform.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['search']
#processed_text = text.upper()
df = pd.read_csv('func_latest.csv',engine='python')
#df1 = df[(df.topic == text)]
df['topic'] = df['topic'].str.lower()
words=text.split(" ")
print(words)
df1=df[df.topic.str.contains(text)==True]
print(df1)
map = folium.Map(location=[0, 0], zoom_start=2, tiles='Stamen Toner')
def color(contry):
col = ''
df = df1
print(df)
score = 0
for row in df.itertuples():
if row.country == contry:
score = score + row.sentiment
if score == 0:
col = 'darkblue'
if score > 0:
col = 'green'
if score < 0:
col = 'darkred'
return col
map.add_child(folium.GeoJson(data=open('world_geojson_from_ogr.json', encoding="utf-8-sig").read(),
name="Population",
style_function=lambda x: {
'fillColor': color(x['properties']['NAME']) if x['properties'][
'NAME'] == 'Pakistan'
else color(x['properties']['NAME']) if x['properties']['NAME'] == 'India'
else color(x['properties']['NAME']) if x['properties']['NAME'] == 'China'
else color(x['properties']['NAME']) if x['properties']['NAME'] == 'United States'
else color(x['properties']['NAME']) if x['properties']['NAME'] == 'United Kingdom'
else 'white'}))
map.add_child(folium.LayerControl())
map.save(outfile='C:/Users/LENOVO_PC/PycharmProjects/untitled1/templates/map.html')
return render_template('map.html')
import os
if __name__ == "__main__":
if os.path.exists("map.html"):
os.remove("map.html")
app.run()
这是在控制台中显示更新的结果,但它没有刷新Map并在Map上显示更新的情绪可视化。 Folium map visualization at first glance
先感谢您。
我遇到过同样的问题。 Flask默认情况下不刷新模板。你可以设置
app.config ['TEMPLATES_AUTO_RELOAD'] =真
在哪里定义flask(app = Flask(name))