I was wondering if there was a way to take something from a text box in the HTML, feed it into flask, then parse that data with Python. I was thinking this might involve some JS but I could be wrong. Any ideas?
In the <form> tag, you set the method attribute to post so the form data gets sent to the server as a POST request. In the form, you have a text input field named title ; this is the name you'll use on the application to access the title form data. You give the <input> tag a value of {{ request.
Unless you want to do something more complicated, feeding data from a HTML form into Flask is pretty easy.
my_form_post
).request.form
.templates/my-form.html
:
<form method="POST"> <input name="text"> <input type="submit"> </form>
from flask import Flask, request, render_template app = Flask(__name__) @app.route('/') def my_form(): return render_template('my-form.html') @app.route('/', methods=['POST']) def my_form_post(): text = request.form['text'] processed_text = text.upper() return processed_text
This is the Flask documentation about accessing request data.
If you need more complicated forms that need validation then you can take a look at WTForms and how to integrate them with Flask.
Note: unless you have any other restrictions, you don't really need JavaScript at all to send your data (although you can use it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With