Categories
Python Answers

How to send image generated by PIL to browser with Python Flask?

Sometimes, we want to send image generated by PIL to browser with Python Flask.

In this article, we’ll look at how to send image generated by PIL to browser with Python Flask.

How to send image generated by PIL to browser with Python Flask?

To send image generated by PIL to browser with Python Flask, we can use the StringIO class.

For instance, we write

def serve_pil_image(pil_img):
    img_io = StringIO()
    pil_img.save(img_io, 'JPEG', quality=70)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')

@app.route('some/route/')
def serve_img():
    img = Image.new('RGB', ...)
    return serve_pil_image(img)

to create the serve_pil_image that calls send_file with the img_io StringIO object.

We populate it with the image data by writing

pil_img.save(img_io, 'JPEG', quality=70)

Then we use img_io.seek(0) to back to the beginning of the image.

And then we call send_file with img_io and the mimetype to return the image.

Then we call serve_pil_image with the PIL image img to return that as the response in our view.

Conclusion

To send image generated by PIL to browser with Python Flask, we can use the StringIO class.

Categories
Python Answers

How to use g.user global in Python Flask?

Sometimes, we want to use g.user global in Python Flask.

In this article, we’ll look at how to use g.user global in Python Flask.

How to use g.user global in Python Flask?

To use g.user global in Python Flask, we can set g.user to the current user’s data before a request is made in an authenticated route.

For instance, we write

@app.before_request
def load_user():
    if session["user_id"]:
        user = User.query.filter_by(username=session["user_id"]).first()
    else:
        user = {"name": "Guest"}
    g.user = user

to add a load_user function that’s called before each request.

We use the @app.before_request to make load_user run before each request.

In it, we get the user’s data from the user_id key in the session object.

And then we assign that to g.user.

Now g.user should have the current user data in our views.

Conclusion

To use g.user global in Python Flask, we can set g.user to the current user’s data before a request is made in an authenticated route.

Categories
Python Answers

How to delete a record by id in Python Flask-SQLAlchemy?

Sometimes, we want to delete a record by id in Python Flask-SQLAlchemy.

In this article, we’ll look at how to delete a record by id in Python Flask-SQLAlchemy.

How to delete a record by id in Python Flask-SQLAlchemy?

To delete a record by id in Python Flask-SQLAlchemy, we can call the session delete method.

For instance, we write

session.delete(obj1)
session.delete(obj2)

session.commit()

to call session.delete to mark obj1 and obj2 to be deleted.

And then we call session.commit to commit the delete operations.

Conclusion

To delete a record by id in Python Flask-SQLAlchemy, we can call the session delete method.

Categories
Python Answers

How to serve robot.txt, sitemap.xml static files in Python Flask?

Sometimes, we want to serve robot.txt, sitemap.xml static files in Python Flask.

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

How to serve robot.txt, sitemap.xml static files in Python Flask?

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

For instance, we write

from flask import Flask

app = Flask(__name__, static_folder='static', static_url_path='')

to create a Flask instance with the static_folder argument set to the 'static' folder in our Flask app project’s folder.

Then we can put robots.txt and sitemap.xml inside it.

Conclusion

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

Categories
Python Answers

How to set content type with Python Flask?

Sometimes, we want to set content type with Python Flask.

In this article, we’ll look at how to set content type with Python Flask.

How to set content type with Python Flask?

To set content type with Python Flask, we can create a Response instance with the mimetype argument.

For instance, we write

from flask import Response
@app.route('/ajax')
def ajax():
    xml = 'foo'
    return Response(xml, mimetype='text/xml')

to create the ajax view that returns a response by creating a Response instance with the mimetyp set to 'text/xml' to return an XML response with the xml string as the body.

Conclusion

To set content type with Python Flask, we can create a Response instance with the mimetype argument.