SuperGeocoder
在web上完成CSV格式文件的上传;
为含有Address属性的CSV格式文件添加经纬度属性;
在web上显示该dataframe:data.to_html()
script.py:
建立一个文件夹用于存储更改后的csv文件;
文件命名需引入datetime,加入当前时间方式名称重复;
from flask import Flask,render_template,request,send_file #得到email by request
#from werkzeug import secure_filename
from werkzeug.utils import secure_filename
import pandas #存放csv
from geopy.geocoders import ArcGIS #添加经纬度
import datetime #防止同时传入相同name的文件
app=Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/submit', methods=['POST'])
def submit():
global filename
if request.method=='POST':
try:
file=request.files["file"] #input里的file name
#file.save(secure_filename("upload"+file.filename)) #防止含攻击代码的文件名
#with open("upload"+file.filename) as f:
# data=pandas.read_csv(f)
data=pandas.read_csv(file)
try:
nom=ArcGIS()
data["Coordinates"]=data["Address"].apply(nom.geocode)
data["Latitude"]=data["Coordinates"].apply(lambda x:x.latitude if x != None else None)
data["Longitude"]=data["Coordinates"].apply(lambda x:x.longitude if x != None else None)
data=data.drop(['Coordinates'],axis=1)
filename=datetime.datetime.now().strftime("uploads/%Y-%m-%d-%H-%M-%S-%f"+".csv")
#data.to_csv("upload"+file.filename)
data.to_csv(filename,index=None)
return render_template('home.html',btn="download.html",warning="success! You can download the table!",text=data.to_html())
except:
return render_template('home.html',warning="Seems like we have some problems ><<br>Please check your Address columns and try again!")
except:
return render_template('home.html',warning="No file uploaded!")
@app.route('/download')
def download():
#return render_template('home.html',text="hello!")
return send_file(filename, attachment_filename="Yourfile.csv",as_attachment=True) #file需要设为全局变量
if __name__ =='__main__':
app.debug=True
app.run(port=5001)
注意点
上传文件至heroku时,新建文件夹内需要有文件,不然会被忽略;
home.html
只接受后缀为.csv的文件:<input type="file" name='file' accept=".csv">
<!DOCTYPE html>
<html>
<title>Super GeoCoder</title>
<head>
<link href='../static/main.css' rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Super GeoCoder</h1>
<h3>Upload your <span>CSV file</span> to get the longitude and latitude of your Address!</h3>
<p>Make sure that there has an <span>Address column</span> in your CSV file</p>
<form action={{url_for('submit')}} method='POST' enctype="multipart/form-data">
<input type="file" name='file' accept=".csv"> <br>
<p class='message'>
{{warning | safe}}
</p>
<button type="submit">Submit</button>
</form>
<div class="output">
{{text|safe}}
{% include [btn] ignore missing %}
</div>
</div>
</body>
</html>
download.html
<!DOCTYPE html>
<html lang="en">
<div class="output">
<a href={{url_for('download')}} target="blank"><button class='btn'>download</button> </a>
</div>
</html>
main.css
html,body{
height: 100%;
margin: 0 auto;
}
.container{
font-family: Arial, sans-serif;
margin: 0 auto;
width:100%;
height: 100%;
background-color: black;
color:white;
text-align: center;
overflow-y: scroll;
}
.container h1{
margin-top: 80px;
font-size:50px;
}
.container h3{
margin:20px;
}
.container form{
margin:20px;
}
.message{
margin-top: 10px;
font-size: 18px;
color:red;
}
.container button{
width: 110px;
height:50px;
margin-top:30px;
background-color:white;
font-size:20px;
}
.container input{
font-size:20px;
transition: all 0.2s ease-in-out;
}
.container p{
margin: 10px;
font-size: 18px;
}
span{
color: hotpink;
font-size: 25px;
}
.output{
display: inline-block;
margin: 20px;
}
接着传入heroku即可;