Categories
Python Answers

How to include a HTML file in a Jinja2 template with Python Flask?

Sometimes, we want to include a HTML file in a Jinja2 template with Python Flask.

In this article, we’ll look at how to include a HTML file in a Jinja2 template with Python Flask.

How to include a HTML file in a Jinja2 template with Python Flask?

To include a HTML file in a Jinja2 template with Python Flask, w ecan use the include directive.

For instance, we write

{% extends 'template.html' %}
{% block content %}
    {% if task == 'content1' %}
        {% include 'content1.html' %}
    {% endif %}
    {% if task == 'content2' %}
        {% include 'content2.html' %}
    {% endif %}
{% endblock %}

in our Jinja template to include the content1.html and content2.html files with

{% include 'content1.html' %}

and

{% include 'content2.html' %}

Conclusion

To include a HTML file in a Jinja2 template with Python Flask, w ecan use the include directive.

Categories
Python Answers

How to import from config file in Python Flask?

Sometimes, we want to import from config file in Python Flask.

In this article, we’ll look at how to import from config file in Python Flask.

How to import from config file in Python Flask?

To import from config file in Python Flask, we can use the config.from_object method.

For instance, we write

class Config(object):
    DEBUG = True
    DEVELOPMENT = True
    SECRET_KEY = "secret"
    FLASK_HTPASSWD_PATH = "/secret/.htpasswd"
    FLASK_SECRET = SECRET_KEY
    DB_HOST = "database"


class ProductionConfig(Config):
    DEVELOPMENT = False
    DEBUG = False
    DB_HOST = "my.production.database"

to create config classes in the config.py file.

Then we write

app.config.from_object("config.ProductionConfig")

in app.py to load the config from the ProductionConfig class in the config.py file with the app.config.from_object.

app is the Flask app instance.

Conclusion

To import from config file in Python Flask, we can use the config.from_object method.

Categories
Python Answers

How to download a csv file on clicking a button with Python Flask?

Sometimes, we want to download a csv file on clicking a button with Python Flask.

In this article, we’ll look at how to download a csv file on clicking a button with Python Flask.

How to download a csv file on clicking a button with Python Flask?

To download a csv file on clicking a button with Python Flask, we can create a route that returns the csv file.

For instance, we write

from flask import send_file


@app.route("/get-plot-csv")
def plot_csv():
    return send_file(
        "outputs/Adjacency.csv",
        mimetype="text/csv",
        attachment_filename="Adjacency.csv",
        as_attachment=True,
    )

to call send_file with the file path, mimetype, attachment_filename, and as_attachment set to True to return a csv file response.

We set the MIME type to 'text/csv' and the download file’s filename is Adjacency.csv.

Conclusion

To download a csv file on clicking a button with Python Flask, we can create a route that returns the csv file.

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 use the exists method.

For instance, we write

exists = db.session.query(
    db.session.query(User).filter_by(name="John Smith").exists()
).scalar()

to call filter_by to select the entry in the user table with name column value 'John Smith'.

Then we call exists to see if any result is returned.

Conclusion

To check if row exists in table with Python Flask-SQLAlchemy, we can use the exists method.

Categories
Python Answers

How to fix secret key not set in Python Flask session, using the Flask-Session extension?

Sometimes, we want to fix secret key not set in Python Flask session, using the Flask-Session extension.

In this article, we’ll look at how to fix secret key not set in Python Flask session, using the Flask-Session extension.

How to fix secret key not set in Python Flask session, using the Flask-Session extension?

To fix secret key not set in Python Flask session, using the Flask-Session extension, we set it outside of the if __name__ == '__main__' block.

For instance, we write

from flask import Flask, session

app = Flask(__name__)
app.secret_key = "super secret key"

# ...

if __name__ == "__main__":
    app.debug = True
    app.run()

to set the app.secret_key property before we call app.run in the if __name__ == "__main__" block.

This is because flask run skips the if __name__ == "__main__" block.

We can run python app.py to start the app if we want to run the if __name__ == "__main__" block.

Conclusion

To fix secret key not set in Python Flask session, using the Flask-Session extension, we set it outside of the if __name__ == '__main__' block.