如何使用Flask和HTML在同一网页上打印/显示搜索结果?

时间:2019-11-10 10:16:07

标签: python html flask

我有一个球员数据集和他们进球的进球数,这个数据集存储在一个数据框中。我想在搜索栏中显示或打印搜索到的统计数据和球员详细信息,但是我总是收到错误的请求错误

这是数据集Dataset Screenshot

这是网页的外观Webpage screenshot

MainProject Python文件

    import pandas as pd
    from pandas import DataFrame
    from fuzzywuzzy import process



    scorers=pd.read_csv('tests.csv')

    def main(searchplayer):
        scorers=pd.read_csv('tests.csv')
        player=list(scorers['Player'])
        goals=list(scorers['Goals'])
        team=list(scorers['Team'])


        df_data={'player': player,
                'goals': goals,
                'team': team}

        df_main=pd.DataFrame(df_data, columns=['player', 'goals', 'team'])
        def getSearchedProducts(searchplayer, choices, limit=30):
            res=process.extract(searchplayer, choices, limit=limit)
            return res

        searchplayer=str(searchplayer)
        result=getSearchedProducts(searchplayer, player)

        player_result_list = [res[0] for res in result if res[1]>=70]

        player_result_df = df_main[df_main['Product'].isin(product_result_list)]

        player_result_df.index+=1
        list_player_res=[(tabulate(player_result_df, headers='keys', tablefmt='psql'))]

        return list_player_res

烧瓶文件

    import os
    from flask import Flask, flash, request, render_template, 
            redirect, url_for, send_from_directory
    from werkzeug.utils import secure_filename
    from MainProject import main
    from flask_cors import CORS

    searchplayer=""

    app=Flask(__name__)

    app.config['CACHE_TYPE']='null'
    CORS(app)

    @app.route('/', methods=['GET', 'POST'])
    def homepage():
        searchplayer=request.form['prod']
        list_player_res = main(searchplayer)
        return render_template('home.html', list_player_res=list_player_res)

    if __name__ == "__main__":
        app.run()

我的HTML文件的代码段

    <form class="form-inline" method="POST">
            <input class="form-control mr-sm-2" name="prod" type="search" placeholder="Search for Players" aria-label="Search">
            <button class="btn btn-light my-sm-0" type="Submit">Search</button>  
          </form>           

这是我尝试过的:

    @app.route('/')

    def homepage():
           return render_template('home.html')


    @app.route('/searchpg', methods=['GET', 'POST'])
    def searchpg():
        global searchplayer
        if request.method == 'POST':
            searchplayer = request.form.get['prod']
            list_res, list_prod_res = main(searchplayer)
            print(list_player_res)

            return render_template('home.html', list_player_res=list_player_res)

HTML,这就是我所做的:

          <form class="form-inline" method="POST">
                    <input class="form-control mr-sm-2" name="prod" 
    type="search" placeholder="Search for Players" aria-label="Search">
                    <button class="btn btn-light my-sm-0" 
       type="Submit">Search</button>  
                  </form>  
                </div>
              </nav>

            <br> 
            {% for result in list_player_res %} 
                    {{result}}
                 {% endfor %}   
            </br>

如何在该搜索栏中搜索球员,并且结果显示在同一屏幕上?

0 个答案:

没有答案