python flask,file structure ,blueprint简单应用

首先要新建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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值