您的位置:首页 > 编程语言 > Python开发

python flask,file structure ,blueprint简单应用

2017-07-23 19:41 309 查看
首先要新建flask工程

工程根目录下会自带static目录和templates目录

1.static目录存放网页静态文件,例如js文件,css文件,jpg文件,geojson文件,svg文件,csv文件等。

2.templates目录存放html文件

1.在工程根目录新建工程启动文件,命名为run.py

2.在工程根目录新建python后台包,索性命名为Transfer包(里面自带
__init__.py
文件)

#run.py
from flask import Flask

#引入后台包
from Transfer import transfer

#实例化app
app = Flask(__name__)

#设置http://localhost:5000显示的内容
@app.route('/')
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
#注册http://localhost:5000/transfer链接
app.register_blueprint(transfer,url_prefix='/transfer')
#启动flask小型服务器,默认端口号5000
app.run()


#__init__.py

from flask import Blueprint
from flask import render_template

#实例化transfer
transfer = Blueprint('transfer',__name__)

#注册http://localhost:5000/transfer/ipmap链接所绑定函数
@transfer.route('/ipmap', methods=['POST','GET'])
#绑定函数,不同的链接绑定函数名不能相同
def ipmap():
#执行链接所返回的html文件
return render_template('/transfer/ipmap.html')


3.在static目录下新建mycss.css文件

body{
background: blue;
}


4.在templates下新建transfer目录,进入transfer目录新建ipmap.html文件

<!--ipmap.html-->
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link rel='stylesheet' href='../../static/mycss.css' type='text/css'/>
</head>
<body>
<h1>Success!</h1>
</body>
</html>


运行run.py文件

浏览器访问http://localhost:5000/transfer/ipmap即可看到Success!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: