Sending Data from HTML Form to a Python Script in Flask

Sending data from HTML form to a Python script in Flask

The form tag needs some attributes set:

  1. action: The URL that the form data is sent to on submit. Generate it with url_for. It can be omitted if the same URL handles showing the form and processing the data.
  2. method="post": Submits the data as form data with the POST method. If not given, or explicitly set to get, the data is submitted in the query string (request.args) with the GET method instead.
  3. 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">

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()])


PS: The curling brackets are from jinja2, a templating language for python.

<form method="POST" action="/">
{{ form.hidden_tag() }}
{{ }} {{ }}
<input type="submit" value="Go">


@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'))
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

Leave a reply