Python Flask Bootstrap














































Python Flask Bootstrap



Flask  Bootstrap


Bootstrap is an open source framework from Twitter that provides user interface components to create clean and attractive web pages that are compatible with all modern web browsers Bootstrap is a client-side framework, so the server is not directly involved with it. All the server needs to do is provide HTML responses that reference Bootstraps cascading style sheets (CSS) and JavaScript files and instantiate the desired components through HTML, CSS, and JavaScript code. The ideal place to do all this is in templates.  


Flask-Bootstrap can be installed with pip

pip install flask-bootstrap

Flask extensions are usually initialized at the same time the application instance is created.


from flask import request,redirect,abort
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
app = Flask(__name__)
bootstrap = Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html')

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

Flask-Bootstrap is imported from the flask.ext namespace and initialized by passing the application instance in the constructor.

Template

{% extends "bootstrap/base.html" %}
{% block title %}This is an example page{% endblock %}

{% block navbar %}
<div class="navbar navbar-fixed-top">
<!-- ... -->
</div>
{% endblock %}

{% block content %}
<h1>Hello, Bootstrap</h1>
{% endblock %}

Output

Once Flask-Bootstrap is initialized, a base template that includes all the Bootstrap files is available to the application. This template takes advantage of Jinja2s template inheritance; the application extends a base template that has the general structure of the page including the elements that import Bootstrap.

Everything you do in child templates is based on blocks. Some blocks (like title, navbar or content) are convenience blocks. Strictly speaking they would not be necessary, but are added to save typing effort.


A very powerful feature is Jinjas super() function. This gives you the option of amending blocks instead of replacing them.




Comments