Sending data from HTML form to a Python script in Flask
The form
tag needs some attributes set:
action
: The URL that the form data is sent to on submit. Generate it withurl_for
. It can be omitted if the same URL handles showing the form and processing the data.method="post"
: Submits the data as form data with the POST method. If not given, or explicitly set toget
, the data is submitted in the query string (request.args
) with the GET method instead.enctype="multipart/form-data"
: When the form contains file inputs, it must have this encoding set, otherwise the files will not be uploaded and Flask won't see them.
The input
tag needs a name
parameter.
Add a view to handle the submitted data, which is in request.form
under the same key as the input's name
. Any file inputs will be in request.files
.
@app.route('/handle_data', methods=['POST'])
def handle_data():
projectpath = request.form['projectFilepath']
# your code
# return a response
Set the form's action
to that view's URL using url_for
:
<form action="{{ url_for('handle_data') }}" method="post">
<input type="text" name="projectFilepath">
<input type="submit">
</form>
How do I send a string from HTML to python using flask?
If you want form data, then you can get it like
from flask import request
var_name = request.form['name_attr']
Passing data from HTML form through Flask to an API
So a good and well documented way to communicate with flask
from html
is via flask wtforms. It helps you essentially to validate your forms in the html and to secure the POST
requests from the frontend usually via a CSRF token.
From the documentation you have a minimal example that could be a good starting point for you:
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired
class MyForm(FlaskForm):
name = StringField('name', validators=[DataRequired()])
HTML
PS: The curling brackets are from jinja2, a templating language for python.
<form method="POST" action="/">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name(size=20) }}
<input type="submit" value="Go">
</form>
Validating
@app.route('/submit', methods=('GET', 'POST'))
def submit():
form = MyForm()
if form.validate_on_submit():
return redirect('/success')
return render_template('submit.html', form=form)
It is fairly straightforward, you create a form, pass it to the frontend together in the routing and then when the user submits it it tries to validate it.
With all that said, if all you want is to simply send a form data and process that in flask, all you really need to do is create / accept POST requests in your endpoint:
@app.route('/', methods=('GET', 'POST'))
@app.route('home')
def home():
if request.method == 'POST':
form = request.form
value1 = form['value1']
# do something with the values from the form.
return flask.render_template('home.html')
I would not recommend due to validation / security concerns, but it could be a starting point if you are new to flask.
Related Topics
How to Terminate Process from Python Using Pid
How to Get Pid by Process Name
Python Ftp Get the Most Recent File by Date
Python Subprocess.Popen "Oserror: [Errno 12] Cannot Allocate Memory"
Python: How to Kill Child Process(Es) When Parent Dies
How to Set Your Pythonpath in an Already-Created Virtualenv
Run Python Script At Startup in Ubuntu
What Do I Need to Read Microsoft Access Databases Using Python
Process List on Linux Via Python
Compare Two Images the Python/Linux Way
How to Activate Virtualenv in Linux
Python-Dev Installation Error: Importerror: No Module Named Apt_Pkg
Packaging a Python Script on Linux into a Windows Executable
Find Size and Free Space of the Filesystem Containing a Given File
Cannot Kill Python Script With Ctrl-C