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.

Categories
Python Answers

How to serve static files in Flask?

Sometimes, we want to serve static files in Python Flask.

In this article, we’ll look at how to serve static files in Python Flask.

How to serve static files in Flask?

To serve static files in Python Flask, we can use the send_from_directory function.

For instance, we write

from flask import send_from_directory

@app.route('/file/<path:path>')
def send_file(path):
    return send_from_directory('file', path)

to call send_from_directory with the directory and path of the file to return the file as the response when we go to /file/.

send_from_directory can safely handle paths for files under a known directory.

Conclusion

To serve static files in Python Flask, we can use the send_from_directory function.

Categories
Python Answers

How to get the data received in a Python Flask request?

Sometimes, we want to get the data received in a Flask request.

In this article, we’ll look at how to get the data received in a Flask request.

How to get the data received in a Flask request?

To get the data received in a Flask request, we can use various properties of request.

To get URL query parameters, we use request.args.

For instance, we write

page = request.args.get("page")

in our route function to get the page query parameter.

request.args in a dictionary.

To get form data, we use the request.form dictionary.

For instance, we write

email = request.form.get('email')

to get the form value with key email in our route function.

And to get JSON request data, we use the request.json in our route function.

Conclusion

To get the data received in a Flask request, we can use various properties of request.

To get URL query parameters, we use request.args.