Taking data from drop-down menu using flask

2024/9/30 23:24:59

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?

Answer

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"
https://en.xdnf.cn/q/71031.html

Related Q&A

How to get list_blobs to behave like gsutil

I would like to only get the first level of a fake folder structure on GCS.If I run e.g.:gsutil ls gs://gcp-public-data-sentinel-2/tiles/I get a list like this:gs://gcp-public-data-sentinel-2/tiles/01/…

How to avoid gcc warning in Python C extension when using Py_BEGIN_ALLOW_THREADS

The simplest way to manipulate the GIL in Python C extensions is to use the macros provided:my_awesome_C_function() {blah;Py_BEGIN_ALLOW_THREADS// do stuff that doesnt need the GILif (should_i_call_ba…

Pairwise operations (distance) on two lists in numpy

I have two lists of coordinates:l1 = [[x,y,z],[x,y,z],[x,y,z],[x,y,z],[x,y,z]] l2 = [[x,y,z],[x,y,z],[x,y,z]]I want to find the shortest pairwise distance between l1 and l2. Distance between two coordi…

Bad results when undistorting points using OpenCV in Python

Im having trouble undistorting points on an image taken with a calibrated camera using the Python bindings for OpenCV. The undistorted points have entirely different coordinates than the original point…

Pandas - Groupby and create new DataFrame?

This is my situation - In[1]: data Out[1]: Item Type 0 Orange Edible, Fruit 1 Banana Edible, Fruit 2 Tomato Edible, Vegetable 3 Laptop Non Edible, Elec…

Can pytest fixtures and confest.py modules be shared across packages?

Suppose I have packageA which provides a class usefulClass, pytest fixtures in a test_stuff.py module, and test configurations in a conftest.py module. Moreover, suppose that I have packageBand package…

Sort a list by presence of items in another list

Suppose I have two lists:a = [30, 10, 90, 1111, 17] b = [60, 1201, 30, 17, 900]How would you sort this most efficiently, such that:list b is sorted with respect to a. Unique elements in b should be pla…

Sample from a multivariate t distribution python

I am wondering if there is a function for sampling from a multivariate student t-distribution in Python. I have the mean vector with 14 elements, the 14x14 covariance matrix and the degrees of freedom …

Why is this Jinja nl2br filter escaping brs but not ps?

I am attempting to implement this Jinja nl2br filter. It is working correctly except that the <br>s it adds are being escaped. This is weird to me because the <p>s are not being escaped and…

select certain monitor for going fullscreen with gtk

I intend to change the monitor where I show a fullscreen window. This is especially interesting when having a projector hooked up.Ive tried to use fullscreen_on_monitor but that doesnt produce any visi…