标签:分离 self libs txt tab 拆分 ora ack 安装
pip install --user pipenv安装pipenv在用户目录下 py -m site --user-site通过此命令找到用户基础目录,结果为C:\Users\u14e\AppData\Roaming\Python\Python35\site-packages 将用户基础目录结尾的site-packages换成Scripts,即C:\Users\u14e\AppData\Roaming\Python\Python35\Scripts,然后将这一路径添加到系统变量中 重新打开命令行工具,如cmd,pipenv --version检查是否安装成功 pipenv install创建一个虚拟环境 pipenv shell激活虚拟环境,exit推出虚拟环境 pipenv install requests安装python包,pipenv install django==1.11.7安装制定版本的包,pipenv uninstall requests卸载包 pipenv graph查看安装的包,以及依赖的其他包
?
# 1 安装 pip install virtualenv virtualenv --version ? # 非Windows # pip install virtualenvwrapper ? # Windows pip install virtualenvwrapper-win ? # 查看当前所有的虚拟环境 workon ? # 创建一个虚拟环境 mkvirtualenv new_env ? # 删除一个虚拟环境 rmvirtualenv new_env
?
?
# 在当前环境中将路径切到项目包打包txt文件目录下 - 项目依赖 pip3 install pipreqs - 生成依赖文件:pipreqs ./ --encoding==‘utf-8‘ - 安装依赖文件:pip3 install -r requirements.txt ?
# app.py from flask import Flask def create_app(): app = Flask(__name__) # 指定位置信息 app.config.from_object(‘app.config.setting‘) app.config.from_object(‘app.config.secure‘) register_blueprint(app) return app ? # genger.py---启动文件 ? from app.app import create_app ? app = create_app() ? ? @app.route(‘/v1/user/get‘) def get_user(): return ‘i am andy‘ ? ? @app.route(‘/v1/book/get‘) def get_book(): return ‘this is get book ‘ ? ? if __name__ == ‘__main__‘: app.run(debug=True)
# 分别设置蓝图 # book.py from flask import Blueprint ? book = Blueprint(‘book‘, __name__) ? ? @book.route(‘/v1/book/get‘) def get_book(): return ‘this is get book ‘ ? # user.py from flask import Blueprint ? user = Blueprint(‘user‘, __name__) ? ? @user.route(‘/v1/user/get‘) def get_user(): return ‘i am andy‘ ? # app.py(生成app并注册蓝图) from flask import Flask ? ? def register_blueprint(app): from app.api.v1.user import user from app.api.v1.book import book app.register_blueprint(user) app.register_blueprint(book) ? ? def create_app(): app = Flask(__name__) # 指定位置信息 app.config.from_object(‘app.config.setting‘) app.config.from_object(‘app.config.secure‘) register_blueprint(app) return app ? # 启动文件 from app.app import create_app app = create_app() if __name__ == ‘__main__‘: app.run(debug=True) ?
1 Blurpring蓝图不是用来拆分视图函数的,应该是用来模块级别的拆分
?
2 写路由的时候太繁琐(重复劳动,比如我写v1/book/get,又要写一个接口是v1/book/delete)
?
新建一个包libs,里面放置的是红图的方法
?
#创建红图 class Redprint: def __init__(slef,name): self.name = name self.mound = [] def route(self,rule,**options): def decorator(f): # f就是所装饰的方法试图,rule就是路由,options就是关键字参数,比如,methods=[‘get‘,‘post‘] self.mound.append((f,rule,options)) return f return decorator def register(self, bp, url_prefix=None): if url_prefix is None: url_prefix = ‘/‘ + self.name for f, rule, options in self.mound: endpoint = options.pop("endpoint", f.__name__) bp.add_url_rule(url_prefix + rule, endpoint, f, **options) ? ? ? # book.py(使用红图) from app.libs.redprint import Redprint ? api = Redprint(‘book‘) ? ? @api.route(‘/get‘) def get_book(): return ‘this is get book ‘ ? ? ? # v1/__init__.py(创建蓝图对象,并在蓝图对象中注册红图) from flask import Blueprint from app.api.v1 import book, user ? ? def create_blueprint_v1(): bp_v1 = Blueprint(‘v1‘, __name__) user.api.register(bp_v1) book.api.register(bp_v1) ? return bp_v1 ? # app.py(将蓝图注册到app对象中) from flask import Flask ? ? def register_blueprint(app): from app.api.v1 import create_blueprint_v1 app.register_blueprint(create_blueprint_v1(), url_prefix=‘/v1‘) # 蓝图补充路由的前缀 ? ? def create_app(): app = Flask(__name__) # 指定位置信息 app.config.from_object(‘app.config.setting‘) app.config.from_object(‘app.config.secure‘) register_blueprint(app) return app ?
标签:分离 self libs txt tab 拆分 ora ack 安装
原文地址:https://www.cnblogs.com/ouyang99-/p/12249480.html