0

I have a dataset of Players and the number of goals they have scored, this dataset is stored in a dataframe. I want to display or print the stats and player details that I have searched for on the search bar, but I am always getting a bad request error

This is the dataset Dataset Screenshot

this is how the Webpage looks like Webpage screenshot

MainProject Python file

    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

Flask File

    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()

A code snippet of my Html file

    <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>           

This is what I tried:

    @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 This is what I did:

          <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>

How do I search for players in that searchbar and the results get displayed on the same screen?

Glen Veigas
  • 85
  • 1
  • 12
  • Your route doesn't differentiate between GET and POST requests. You need to shield the part that expects form input within an `if request.method == 'POST':` guard, otherwise you get an error when you initially try load the page – roganjosh Nov 10 '19 at 10:22
  • Does this answer your question? [Handling GET and POST in same Flask view](https://stackoverflow.com/questions/42018603/handling-get-and-post-in-same-flask-view) – roganjosh Nov 10 '19 at 10:23
  • No, it doesn't. It doesnt print/display the output though – Glen Veigas Nov 10 '19 at 13:03
  • Well, come on now, you need to give me something to work on here. It almost certainly _is_ your issue, but I can't see how you've attempted to implement it or what the issue is – roganjosh Nov 10 '19 at 14:24
  • I can't read that. Please [edit] the question – roganjosh Nov 10 '19 at 14:45
  • Sorry...Yeah! whatever I have done its added in the question. – Glen Veigas Nov 10 '19 at 14:56
  • No idea how to add the code in the comments – Glen Veigas Nov 10 '19 at 14:57

0 Answers0