Categories
Python Answers

How to send data from HTML form to a Python script in Flask?

Sometimes, we want to send data from HTML form to a Python script in Flask.

In this article, we’ll look at how to send data from HTML form to a Python script in Flask.

How to send data from HTML form to a Python script in Flask?

To send data from HTML form to a Python script in Flask, we can use the request.form dictionary to get the data.

For instance, w ewrite

from flask import request

@app.route('/add_region', methods=['POST'])
def add_region():
    #...
    return (request.form['file_path'])

to get the form data with the request.form dict.

And then we create our form with

<form action="{{ url_for('add_region') }}" method="post">
    Project file path: <input type="text" name="file_path"><br>
    <input type="submit" value="Submit">
</form>

in our template.

request.form['file_path'] has the value of the input with name attribute set to file_path in our form.

action is {{ url_for('add_region') }} which is the URL to the add_region endpoint.

Categories
Python Answers

How to get POSTed JSON in Python Flask?

Sometimes, we want to get POSTed JSON in Python Flask.

In this article, we’ll look at how to get POSTed JSON in Python Flask.

How to get POSTed JSON in Python Flask?

To get POSTed JSON in Python Flask, we can use the request.json property.

For instance, we write

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
def add_message(uuid):
    content = request.json
    return jsonify({"uuid":uuid})

if __name__ == '__main__':
    app.run(host= '0.0.0.0',debug=True)

to create the add_message route that gets the JSON payload from request.json.

request.json returns a dictionary.

We make add_message accept POST requests with

@app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])

Conclusion

To get POSTed JSON in Python Flask, we can use the request.json property.

Categories
Python Answers

How to configure Python Flask dev server to be visible across the network?

Sometimes, we want to configure Python Flask dev server to be visible across the network.

In this article, we’ll look at how to configure Python Flask dev server to be visible across the network.

How to configure Python Flask dev server to be visible across the network?

To configure Python Flask dev server to be visible across the network, we set the --host option to 0.0.0.0.

For instance, we write

flask run --host=0.0.0.0

to run our app with flask run and set the --host option to 0.0.0.0 to make our Flask app visible anywhere in our local network.

Conclusion

To configure Python Flask dev server to be visible across the network, we set the --host option to 0.0.0.0.

Categories
Python Answers

How to return a JSON response from a Python Flask view?

Sometimes, we want to return a JSON response from a Python Flask view.

In this article, we’ll look at how to return a JSON response from a Python Flask view.

How to return a JSON response from a Python Flask view?

To return a JSON response from a Python Flask view, we an return a dictionary directly as the response.

For instance, we write

@app.route("/summary")
def summary():
    d = make_summary()
    return d

to get the dict d from the make_summary function and then return its contents as the JSON response in the /summary view.

Conclusion

To return a JSON response from a Python Flask view, we an return a dictionary directly as the response.

Categories
Python Answers

How to share data between requests with Python Flask?

Sometimes, we want to share data between requests with Python Flask.

In this article, we’ll look at how to share data between requests with Python Flask.

How to share data between requests with Python Flask?

To share data between requests with Python Flask, we can store data in a session.

For instance, we write

from flask import Flask, session
from flask_session import Session

app = Flask(__name__)

SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
Session(app)

@app.route('/')
def reset():
    session["counter"]=0

    return "counter was reset"

@app.route('/inc')
def routeA():
    if not "counter" in session:
        session["counter"]=0

    session["counter"]+=1

    return "counter is {}".format(session["counter"])

to create a session with Session(app).

Then we can store session data in the session dictionary.

We set session["counter"] to 0 in the reset route function.

And then we try to get the counter value in the if statement and set it if it doesn’t exist in routeA.

Conclusion

To share data between requests with Python Flask, we can store data in a session.