Taking data from drop-down menu using flask

how to get a value using flask, from a selected option in a drop down list?
flask dynamic drop down list
creating a dynamic select field with flask-wtf and javascript
wtforms selectfield jinja
how to get selected value from dropdown list in python
jinja drop down list
select data in flask
flask dynamic form

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?


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"

Creating a Dynamic Select Field With Flask-WTF and JavaScript , After watching this video, you should be able to create your down drop downs that depend on Duration: 19:35 Posted: May 23, 2018 I'm using Flask and Sqlite and am try to let the user select an item from an HTML drop down menu. The item selected from the drop down menu will be used in a SQL query to retrieve information from a database and then display those results on a new page.


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.

[AF] Getting selected value from dropdown list : flask, Flask is easy to get started with and a great way to build web sites and web … trouble getting the selected value from a dropdown list to pass on to another page. How to write a Python Flask REST API and use Sqlite Database as a data  I started reading into some flask application programming and I have been trying to get drop down menu to work, but so far I have had no luck. What I want to do is, when the user selects a Type of Food from the first dropdown list, it should get the corresponding list from the database and fill the second set of drop down list.


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>

Flask series part 8: Improving user experience by using input chips , Last time, on a previous installment in our series, we focused on vastly improving the way we handled input, by creating a dropdown menu with  Would love your feedback on this 20min Youtube video - "How to write a Python Flask REST API and use Sqlite Database as a data store in less than 100 lines". The Books API will implement CRUD : Create, Retrieve, Update and Delete. The example shown in this video is a fully functional REST API.


How to get a value using flask, from a selected option in a drop , How to get a value using flask, from a selected option in a drop down list? cs50x html flask. I have the following code for my form in HTML: <div  I just graduated college and I had some free time -- so i decided to delve deep into a project using Flask (i have a bit of Flask experience from an internship, but am by no means fluent). I wanted the site to have data-intensive elements (like time-series graphs), as well as some user signups/db action.


How To Create a Hoverable Dropdown Menu, How do you create a drop down menu in HTML? How to make a dropdown menu using flask-bootstrap? #71. (i.e. body when you should be using content). Flask-bootstrap loads the javascript source at the end of


Remembering selected dropdown values Python/Flask, Python Flask view #app.route('/form/') def form(): # list of tuples representing in (e.g. GET method), default value is 1 selected = request.args.get('choice', '1') simple question , how to set the selected item in dropdown list !! in plain Html is easy gvo.get_optimal_strategies(vega) str1 = None for i in range(0, len(data)): if i  CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up. Sign up to join this community