7

I'm completely new to flask, and really am completely lost with how to approach this. I've looked into other SO questions but I can't seem to get this working regardless.

I have a form as such:

<form class="teamSelection" method="POST" action="/submitted">  
   <select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
        <option disabled selected>Select a game</option>
        <option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
   </select>
   <input class="btn" type="submit" value="submit">
</form>

and my flask as so:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted")
def hello():
    return "hello world"

The goal is to take the content of the selected/submitted dropdown item, pass this to the flask file where I then use the team names to scrape information about the match. However at the moment I can't even seem to get the POST of the form to work and am at a complete loss. I appreciate this is a pretty vague and open-ended question, but I seriously don't know how else to figure this out.

Should I instead use jquery to detect when the dropdown has changed and use AJAX to send a POST to somehow call the script and pass the values into it?

Any help would be greatly appreciated.


EDIT
I thought I put this in the original post, but must have forgot.
I am currently running an apache localhost server, and am working with flask via pycharm. All I've done at the moment is install the flask package in pycharm, and haven't set any of it up like I've seen in some tutorials do when running from the command line. I assumed this step wasn't necessary, as I already have a server up and running with apache?
When it comes to backend stuff like this I really have no idea, so apologies if that's a stupid assumption.

I've changed the flask to:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
    with open("newTest.csv", mode="w+") as file:
        fileWriter = csv.writer(file)
        fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
    file.close()

The reason being as I can see if this script is actually being called, if it is it will make a new csv file called newTest. After running the webpage and submitting no new csv file appears, so this script isn't being run, meaning it's likely due to me not configuring flask correctly?/The assumption that apache was enough was incorrect?

Danny
  • 435
  • 1
  • 5
  • 17

3 Answers3

6

You have just to tell the flask method to accept POST request and to read parameters from the request

Example:

from flask import Flask, request
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
   myvariable = request.form.get("teamDropdown")
   ... your code ...
   return "hello world"
FedOpp
  • 313
  • 2
  • 8
  • Appreciate the response! I added the 'with open ('newTest.csv)...' code seen in the edit of the OP to your answer, replacing the '... your code ...' however after submitting the form the file still doesn't appear. I'm positive your solution as others posted here work, so perhaps the issue is that I haven't actually got flask setup correctly? – Danny Sep 21 '18 at 12:55
  • I tried the function and it actually works.. notice that post arguments are to be read from "request" variable! and remember to import it from flask first – FedOpp Sep 21 '18 at 13:19
  • Fair enough, must be something to do with me not setting up flask correctly. All I've done is install the flask package in pycharm and copied your code in. The webpage is on a local apache server running. I thought apache would handle the post stuff without me needing to setup flask, but clearly I'm missing something obvious with this. Regardless I appreciate the effort on your behalf, though I think i'm a lost cause as I simply have no idea what i'm doing wrong/not doing at all – Danny Sep 21 '18 at 13:26
  • Yes I think the problem is that you're trying to run it with apache. As written in the docs [link](http://flask.pocoo.org/docs/1.0/quickstart/) you have to launch the application in a shell with `flask run` (or `python `) – FedOpp Sep 21 '18 at 13:38
1

So, your question is not about flask, but about fopen - you have to add a full file path including directory path script_dir = path.dirname(path.abspath(__file__)).

Flask script (modified for launching in my local copy of project):

from flask import Flask, render_template, request
import csv
from os import path
app = Flask(__name__)

script_dir = path.dirname(path.abspath(__file__))

@app.route ("/")
def index():
    return render_template("index.html") 

@app.route("/submitted", methods=["GET", "POST"])
def hello():
    if request.method == "GET":
        return render_template("index.html") 
    filefullpath = script_dir + '//newTest.csv'
    with open(filefullpath, mode="w+") as file:
        fileWriter = csv.writer(file)
        fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
    file.close()
    return "hello world"

index.html (in folder "/templates")

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    Test
        <br>
    <form class="teamSelection" method="POST" action="/submitted">  
       <select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
            <option disabled selected>Select a game</option>
            <option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
       </select>
       <input class="btn" type="submit" value="submit">
    </form>
</body>
</html>
Viktor Ilienko
  • 817
  • 8
  • 15
0

Modify your code as:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
    return request.form['teamDropdown']

Please let me know if that helps.

fabrik
  • 14,094
  • 8
  • 55
  • 71
  • Okay I've modified it but still nothing appears to happen on the webpage. I changed the return to "hello world" again and search the webpage for it after submitting but it wasn't found. I've updated some info which might be useful – Danny Sep 21 '18 at 12:39
  • @Danny that's another problem, I think. – fabrik Sep 21 '18 at 12:56