当前位置:   article > 正文

Python:Flask-Bootstrap和Bootstrap-Flask

flask-bootstrap和bootstrap-flask

体验了一下Flask+Bootstrap,给我的感觉是前后端代码合在一起了,增加开发难度。

一、Flask-Bootstrap

不支持Bootstrap 4

  • github : https://github.com/mbr/flask-bootstrap
  • pypi: https://pypi.org/project/Flask-Bootstrap/

安装

pip install Flask-Bootstrap
  • 1

使用示例

from flask import Flask, render_template
from flask_bootstrap import Bootstrap

app = Flask(__name__)
Bootstrap(app)


@app.route('/')
def index():
    return render_template('index.html')


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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

模板文件 templates/index.html

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

{% block title %}这是标题{% endblock %}

{% block content %}
  <h1>Hello, Bootstrap</h1>
    <button class="btn">按钮</button>

    <button class="btn btn-primary">按钮</button>
{% endblock %}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

渲染结果
在这里插入图片描述

二、Bootstrap-Flask

支持Bootstrap 4

  • doc: https://bootstrap-flask.readthedocs.io/
  • github:https://github.com/greyli/bootstrap-flask

安装

# 需要卸载flask-bootstrap
$ pip uninstall flask-bootstrap bootstrap-flask -y
$ pip install bootstrap-flask
  • 1
  • 2
  • 3

使用示例


from flask import Flask, render_template
from flask_bootstrap import Bootstrap

app = Flask(__name__)
Bootstrap(app)


@app.route('/')
def index():
    return render_template('index.html')


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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

模板文件 templates/index.html

{% block styles %}
    <!-- Bootstrap CSS -->
    {{ bootstrap.load_css() }}
{% endblock %}

{% block content %}
    <h1>Hello, Bootstrap</h1>
    <button class="btn">按钮</button>

    <button class="btn btn-primary">按钮</button>
{% endblock %}


{% block scripts %}
    <!-- Optional JavaScript -->
    {{ bootstrap.load_js() }}
{% endblock %}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

渲染结果
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/856940
推荐阅读
相关标签
  

闽ICP备14008679号