首页 > 解决方案 > 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

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)

标签: pythonpandasflask

解决方案


推荐阅读