Flask Blueprint Blueprint by Function Modular Architecture Example

Source: Internet
Author: User

Use flask as the development framework, must be modular according to function, otherwise, the larger the project later, the development speed is slower.

1,FlaskModular Structure Planning
[[email protected] yangyun]# tree.├── asset                 #资产功能目录 │   ├── __init__.py│    ├── models.py         #资产数据库结构文件 │   └──  views.py          #资产视图文件 ├── user                  #用户功能目录 │  ├──__init__.py│   ├── models.py           #用户数据库结构文件 │   └── views.py            #用户视图配置文件 ├──  config.py              #公共配置文件 ├──  requirements.txt         #需要的安装包 ├── run.py                 #主运行文件 ├── static                 #静态文件目录, css,js, image et └── templates               #静态页面存放目录    ├── asset             #asset功能模块页面存放目录     │  └──  index.html    ├── index.html          #首页     └── user                                  └── index.html
2,run.pyMaster Run File configuration

[email protected] yangyun]# cat run.py

from flask import flaskfrom asset import  assetfrom user import user  apple=flask (__name__,         template_folder= ' templates ',  #指定模板路径, can be a relative path, or it can be an absolute path.          static_folder= ' static ',   #指定静态文件前缀, default static file path prefix          #static_url_path = '/opt/auras/static ',       #指定静态文件存放路径.          ) apple.register_blueprint (asset,url_prefix= '/asset ')      #注册asset蓝图 and specify a prefix. Apple.register_blueprint (user)        #注册user蓝图, no prefix specified.  if __name__== ' __main__ ':          apple.run (host= ' 0.0.0.0 ', port=8000,debug=true)    #运行flask  http program, host specifies the listener ip,port the specified listening port, debug mode needs to be turned on. 


3,AssetFunction Module Configuration

Similar configuration for other function modules

1) __init__.py file Configuration

[email protected] asset]# cat __init__.py

From flask import Blueprint asset=blueprint (' Asset ', __name__, #template_folder = '/opt/auras/templates/', # Specify template path #static_folder = '/opt/auras/flask_bootstrap/static/', #指定静态文件路径) import views

2) views.py file Configuration

[email protected] asset]# cat views.py

From flask import render_templatefrom Asset import Asset @asset. Route ('/') #指定路由为/, because the prefix specified in run.py, when the browser accesses, the path is Http://IP/asset/def index (): print ' __name__ ', __name__ returnrender_template (' Asse T/index.html ') #返回index. html template, path default under Templates


3) Front-end page configuration

[Email protected] yangyun]# echo ' This isasset index page ... ' >templates/asset/index.html

4,UserFunction Module Configuration

The configuration here is consistent with the above asset configuration

1) __init__.py configuration

[email protected] yangyun]# cat user/__init__.py

From flask import blueprintuser=blueprint (' user ', __name__,) import views

2) views.py configuration

[email protected] yangyun]# cat user/views.py

From flask import render_templatefrom user Import user @user. Route ('/') def index (): print ' __name__ ', __name__ Returnrender_template (' user/index.html ')

3) static page configuration

Echo ' This is User page .... ' >templates/user/index.html

5,Requirements.txtFile Configuration

The primary role is to record the required dependency packages, and the new environment is deployed with the following dependencies installed,pip installation command : pip install-r requirements.txt

[Email protected] yangyun]# catrequirements.txtflask==0.10.1flask-bootstrap==3.3.5.6flask-login== 0.2.11flask-sqlalchemy==2.0flask-wtf==0.12

6, browser access testing

Back-End Run program

[[email protected] yangyun]# python run.py *running on http://0.0.0.0:8000/(press CTRL + C to quit) *restarting with stat

front-end access to asset page

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/4D/wKioL1X6FUTg52DlAABF2vmZqMA453.jpg "title=" 1.png " Style= "Float:none;" alt= "wkiol1x6futg52dlaabf2vmzqma453.jpg"/>


front-end access to user page

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/50/wKiom1X6EwyDfQ7XAABu-gqfM60350.jpg "title=" 2.png " Style= "Float:none;" alt= "wkiom1x6ewydfq7xaabu-gqfm60350.jpg"/>

Why does 404 appear ? Because there is no prefix specified in the run.py, user is not required in the URL.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/4D/wKioL1X6FUSyI3DjAAA7JI0SKS0527.jpg "title=" 3.png " Style= "Float:none;" alt= "wkiol1x6fusyi3djaaa7ji0sks0527.jpg"/>

Above


This article from "Yang Cloud" blog, reproduced please contact the author!

Flask Blueprint Blueprint by Function Modular Architecture Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.