Taking data from drop-down menu using flask
flask dynamic drop down list
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
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"
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.
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
- Using request.form e.g. searchText = request.form['teamDropdown']
- 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?
- 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
- 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
- Yes I think the problem is that you're trying to run it with apache. As written in the docs link you have to launch the application in a shell with
- 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 that's another problem, I think.