python - Creating Search function on CSV file through flask
问题描述
I am a beginner at creating a web API using Flask. I am trying to use flask rest API to search and get data from the CSV file to the page through some keywords. More specifically, the goal is to input string as a keyword on /search/<user_input> which will be searched throughout the CSV file and pull all the similar strings and give the entre rows of data.
I have tried to upload the CSV file using python flask, but I have no idea about searching. Anyone can suggest me solutions?
The CSV file is this : enter image description here
- List item
My code to upload the CSV file is here:
from flask import Flask, render_template, request, make_response
import pandas as pd
import csv
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
# Set The upload HTML template '\templates\index.html'
return render_template('index.html')
@app.route('/data', methods=['GET', 'POST'])
def data():
if request.method == 'POST':
f = request.form['csvfile']
data = []
with open(f) as file:
csvfile = csv.reader(file)
for row in csvfile:
data.append(row)
data = pd.DataFrame(data)
return render_template('data.html', data=data.to_html(header=False, index=False))
if (__name__ == "__main__"):
app.run(port=5000, threaded=True, debug=True)
解决方案
推荐阅读
- google-chrome-extension - 在 Chrome 扩展程序中以 PDF 格式下载当前网站选项卡
- reactjs - 为什么我的页面在 React 中滚动到底部时会重新渲染(刷新)?
- python - 如果列值存在于指定列表中,如何使用 Pandas 将行组合在一起?
- git - 无法在 IntelliJ IDEA 中运行 Shell 脚本
- c++ - 如何输入不同的数字作为数组的不同元素?
- javascript - 集群时从 Leaflet.markercluster 中删除标记
- javascript - 与服务人员进行指纹识别
- c++ - 如何使用 DelegateExecute 打开默认应用程序?
- android - 迁移 Native Apps 到 Flutter 的产品 ID 冲突
- jenkins - 使用 groovy 脚本获取内部版本号