flask蓝图的简单使用
Posted reblue520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask蓝图的简单使用相关的知识,希望对你有一定的参考价值。
1.flask的简单使用 from flask import Flask # from flask import make_response app = Flask(__name__) app.config.from_object(‘config‘) print(app.config[‘DEBUG‘]) # 路由注册方法1 @app.route(‘/hello/‘) def hello(): # 基于类的视图(即插视图) return "Hello, jack" # Response对象 @app.route(‘/hello2/‘) def hello2(): # 返回status code 200,404,301 # 返回content-type http headers # 返回Response对象 headers = { ‘content-type‘: ‘text/plain‘, ‘location‘: ‘http://www.bing.com‘ } # response = make_response(‘<html></html>‘, 301) # response.headers = headers # return "<html></html>" # return response return ‘<html></html>‘, 301, headers # 路由注册方法2 # app.add_url_rule(‘/hello/‘, view_func=hello) # config是dict子类 # 当是入口文件启动的时候才允许app.run # 原因2:当作为生产环境的时候这个app.run是不会运行的,由uwsgi作为服务器启动,如果没有if判断就会冲突 if __name__ == ‘__main__‘: # 生产环境 nginx + uwsgi app.run(host=‘0.0.0.0‘, debug=app.config[‘DEBUG‘], port=81) 2.蓝图的使用 └── fisher ├── app │ ├── __init__.py │ ├── __pycache__ │ └── web │ ├── book.py │ ├── __init__.py │ ├── __pycache__ │ └── user.py ├── config.py ├── fisher.py ├── helper.py ├── httper.py ├── Pipfile ├── Pipfile.lock ├── __pycache__ └── yushu_book.p # 程序入口文件 fisher.py from flask import Flask from app import create_app app = create_app() if __name__ == ‘__main__‘: # 生产环境 nginx + uwsgi app.run(host=‘0.0.0.0‘, debug=app.config[‘DEBUG‘], port=81) # 初始化蓝图 app/__init__.py from flask import Flask def create_app(): app = Flask(__name__) app.config.from_object(‘config‘) register_blueprint(app) return app def register_blueprint(app): from app.web.book import web app.register_blueprint(web) # config.py DEBUG = False # 初始化蓝图文件 app/web/__init__.py from flask import Blueprint web = Blueprint("web", __name__) from app.web import book from app.web import user # 具体的视图文件 app/web/book.py from flask import jsonify from helper import is_key_or_isdn from yushu_book import YuShuBook from . import web # 10.11.0.148:81/book/search/9787501524044/1 @web.route(‘/book/search/<q>/<page>‘) def search(q, page): # 判断搜索关键字是key还是isdn编码 isbn_or_key = is_key_or_isdn(q) if isbn_or_key == ‘isbn‘: result = YuShuBook.search_by_isbn(q) else: result = YuShuBook.search_by_keyword(q) # return json.dump(result), 200, {‘content-type‘: ‘application/json‘} return jsonify(result) # 具体的视图文件 app/web/user.py from flask import Blueprint from . import web @web.route("/user/create") def create_user(): print("create user") return "create user" # 工具类文件 app/helper.py def is_key_or_isdn(word): isbn_or_key = "key" # isdn 13 13位数字 # isdn 10 包括1-10位数字,并且带有一些 - 中划线 short_word = word.replace("-", "") if len(word) == 13 and word.isdigit(): isbn_or_key = "isbn" if "-" in word and len(short_word) == 10 and short_word.isdigit(): isbn_or_key = "isbn" return isbn_or_key # 处理book搜索的文件 app/yushu_book.py from httper import HTTP class YuShuBook(object): isbn_url = ‘http://t.yushu.im/v2/book/isbn/{}‘ keyword_url = ‘http://t.yushu.im/v2/book/search?q={}&count={}&start={}‘ @classmethod def search_by_isbn(cls, isbn): url = cls.isbn_url.format(isbn) result = HTTP.get(url) return result @classmethod def search_by_keyword(cls, keyword, count=15, start=0): url = cls.keyword_url.format(keyword, count, start) result = HTTP.get(url) return result # 处理http请求工具类文件 app/httper.py import requests class HTTP(object): @staticmethod def get(url, return_json=True): r = requests.get(url) # if r.status_code == 200: # if return_json: # return r.json() # else: # return r.text # else: # if return_json: # return {} # else: # return "" if r.status_code != 200: return {} if return_json else ‘‘ return r.json() if return_json else r.text
以上是关于flask蓝图的简单使用的主要内容,如果未能解决你的问题,请参考以下文章