Flask-Bootstrap
Flask-Bootstrap is an extension that packages Twitter Bootstrap (version 3.3.7.1) into a Flask application, providing template macros and resources without requiring boilerplate code. It simplifies the integration of Bootstrap's CSS and JavaScript components into Flask projects. The project's last release was in January 2017, and it is primarily designed for Bootstrap 3 and has Python 2 classifiers.
Warnings
- breaking Flask-Bootstrap is tied to Bootstrap 3.3.7.1, an older version of Bootstrap. For Bootstrap 4, 5, or later, use `Bootstrap-Flask` instead.
- deprecated The project appears to be abandoned, with its last release in 2017 and PyPI classifiers indicating Python 2 support. There is no active development for newer Python or Bootstrap versions.
- gotcha When overriding template blocks (e.g., `styles`, `scripts`) in `bootstrap/base.html`, you must include `{{ super() }}` to retain Bootstrap's default content. Otherwise, your custom content will entirely overwrite Bootstrap's.
- gotcha The correct way to initialize the extension is `bootstrap = Bootstrap(app)`. Simply calling `Bootstrap(app)` might work in some basic scenarios but the explicit assignment is recommended for consistency and proper behavior with certain Flask features.
- gotcha Flask-Bootstrap bundles jQuery 1.x. If your application or other JavaScript libraries require jQuery 2.x or later, you'll need to manually include the desired jQuery version via a CDN or local static files.
Install
-
pip install flask-bootstrap
Imports
- Bootstrap
from flask_bootstrap import Bootstrap
Quickstart
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'a_secret_key_for_dev')
bootstrap = Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html')
# To run this, save as app.py and create templates/index.html:
# # templates/index.html
# {% extends "bootstrap/base.html" %}
# {% block title %}My Flask-Bootstrap App{% endblock %}
#
# {% block content %}
# <div class="container">
# <h1>Hello, Flask-Bootstrap!</h1>
# <p>This is a basic Flask application with Bootstrap 3 via Flask-Bootstrap.</p>
# <button type="button" class="btn btn-primary">Primary button</button>
# </div>
# {% endblock %}
if __name__ == '__main__':
app.run(debug=True)