Categories
Python Answers

How to display image on a HTML page with Python Flask?

Sometimes, we want to display image on a HTML page with Python Flask.

In this article, we’ll look at how to display image on a HTML page with Python Flask.

How to display image on a HTML page with Python Flask?

To display image on a HTML page with Python Flask, we can pass the image path to the template from the view.

For instance, we write

from flask import Flask, render_template
import os

PEOPLE_FOLDER = os.path.join('static', 'people_photo')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER

@app.route('/')
@app.route('/index')
def show_index():
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'image.jpg')
    return render_template("index.html", user_image = full_filename)

to get the image path with

full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'image.jpg')

Then we call render_template with the template file name, and the user_image argument set to the image path.

Next, we render the image in the index.html template by writing

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <img src="{{ user_image }}" alt="User Image">
</body>
</html>

to interpolate the user_image in the template.

Conclusion

To display image on a HTML page with Python Flask, we can pass the image path to the template from the view.

Categories
Python Answers

How to dynamically select template directory to be used in Python Flask?

Sometimes, we want to dynamically select template directory to be used in Python Flask.

In this article, we’ll look at how to dynamically select template directory to be used in Python Flask.

How to dynamically select template directory to be used in Python Flask?

To dynamically select template directory to be used in Python Flask, we set the template_folder argument when we create our Flask instance.

For instance, we write

Flask(__name__, template_folder="templates")

to create the Flask instance with the template_folder argument set to 'templates'.

Then the templates will be read from the templates directory.

Conclusion

To dynamically select template directory to be used in Python Flask, we set the template_folder argument when we create our Flask instance.

Categories
Python Answers

How to decode base64 from POST to use in PIL and Python?

Sometimes, we want to decode base64 from POST to use in PIL and Python.

In this article, we’ll look at how to decode base64 from POST to use in PIL and Python.

How to decode base64 from POST to use in PIL and Python?

To decode base64 from POST to use in PIL and Python, we can call Image.open to open the image.

For instance, we write

from PIL import Image
from io import BytesIO
import base64

data['img'] = '''iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg=''' 

im = Image.open(BytesIO(base64.b64decode(data['img'])))

to call base64.b64decode to decode the base64 string into bytes.

Then we use the bytes to create the BytesIO object.

And then we can open the bytes as an image with Image.open.

Conclusion

To decode base64 from POST to use in PIL and Python, we can call Image.open to open the image.

Categories
Python Answers

How to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy?

Sometimes, we want to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy.

In this article, we’ll look at how to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy.

How to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy?

To fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy, we cakll app.app_context to return the app context.

For instance, we write

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'My connection string'
db.init_app(app)

with app.app_context():
    db.create_all()

to run our SQLAlchemy code in the app context by putting them inside the with block.

We get the app context with app.app_context().

Conclusion

To fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy, we cakll app.app_context to return the app context.

Categories
Python Answers

How to check if row exists in table with Python Flask-SQLAlchemy?

Sometimes, we want to check if row exists in table with Python Flask-SQLAlchemy

In this article, we’ll look at how to check if row exists in table with Python Flask-SQLAlchemy.

How to check if row exists in table with Python Flask-SQLAlchemy?

To check if row exists in table with Python Flask-SQLAlchemy, we can make a query top the table.

For instance, we write

exists = db.session.query(User.id).filter_by(name='davidism').first() is not None

to call query to query the table by User.id.

And we call filter_by to add additional filters by the name field value.

Then we call first to return the first value.

Conclusion

To check if row exists in table with Python Flask-SQLAlchemy, we can make a query top the table.