Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Flask-Bootstrap works?

I'm learning Flask web development, and the tutorial I'm following introduces an extension called Flask-Bootstrap. To use this extension, you must initialize it first, like this:

from flask_bootstrap import Bootstrap
# ...
bootstrap = Bootstrap(app)

Weirdly enough to me, the variable bootstrap is not used in the rest of my module. However, if I comment out this line, a jinja2.exceptions.TemplateNotFound exception will be raised. Also, the templates used start with this line:

{% extends "bootstrap/base.html" %}

But I don't have a directory named /bootstrap under /templates!

I want to know what's going on:

  1. What does the bootstrap = Bootstrap(app) line do?
  2. Where does bootstrap/base.html reside?
like image 313
nalzok Avatar asked Sep 10 '16 14:09

nalzok


People also ask

How does a Flask app work?

Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

What version of Bootstrap does Flask use?

Flask-Bootstrap 3.3. 7.1 An extension that includes Bootstrap in your project, without any boilerplate code.

Is Flask full stack web framework?

This course is aimed at Python developers who want to get into full-stack web development with Flask, a micro-framework for web development using Python.


1 Answers

  • Other answers have told you

the bootstrap = Bootstrap(app) line "init the extension on the app".

The bootstrap/base.html resides in the Flask-Bootstrap package.

To understand this, you must know something about "Flask's template searchpath"

  1. application's template folder
  2. Blueprint's template folder

So the Flask-Bootstrap actually register a blueprint to your app

class Bootstrap(object):
    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        blueprint = Blueprint(
            'bootstrap',
            __name__,
            template_folder='templates',
            static_folder='static',
            static_url_path=app.static_url_path + '/bootstrap',
            subdomain=app.config['BOOTSTRAP_LOCAL_SUBDOMAIN'])

        app.register_blueprint(blueprint)

You can see it clearly by set EXPLAIN_TEMPLATE_LOADING:

app = Flask(__name__)
app.config['EXPLAIN_TEMPLATE_LOADING'] = True

then

export FLASK_ENV=development
flask run

when you access the page:

[2018-07-12 15:28:58,659] INFO in debughelpers: Locating template "user.html":
1: trying loader of application "hello"
   class: jinja2.loaders.FileSystemLoader
   encoding: 'utf-8'
   followlinks: False
   searchpath:
     - /root/learn/python-lab/Flask/flasky/templates
   -> found ('/root/learn/python-lab/Flask/flasky/templates/user.html')
2: trying loader of blueprint "bootstrap" (flask_bootstrap)
   class: jinja2.loaders.FileSystemLoader
   encoding: 'utf-8'
   followlinks: False
   searchpath:
     - /root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates
   -> no match
################################################################# Note here #######
[2018-07-12 15:28:58,677] INFO in debughelpers: Locating template "bootstrap/base.html":
1: trying loader of application "hello"
   class: jinja2.loaders.FileSystemLoader
   encoding: 'utf-8'
   followlinks: False
   searchpath:
     - /root/learn/python-lab/Flask/flasky/templates
   -> no match  ### in app path not found
2: trying loader of blueprint "bootstrap" (flask_bootstrap)
   class: jinja2.loaders.FileSystemLoader
   encoding: 'utf-8'
   followlinks: False
   searchpath:
     - /root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates
   ## in blueprint path found the bootstrap/base.html
   -> found ('/root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html')
127.0.0.1 - - [12/Jul/2018 15:28:58] "GET /user/Yao HTTP/1.1" 200 -
  • Addition,The Blueprint template folder is added to the search path of templates but with a lower priority than the actual application’s template folder, see http://flask.pocoo.org/docs/1.0/blueprints/#templates
like image 88
tinyhare Avatar answered Sep 28 '22 20:09

tinyhare