构建flask应用程序的最佳方法
Flask-Unchained的Python项目详细描述
烧瓶未染色
构建烧瓶应用程序的最佳方法
这是什么?
无约束烧瓶是一个(进行中的工作)完全集成的可选电池,包括在烧瓶顶部构建的网络框架及其扩展生态系统。Flask unchained的目标是成为一个全新的、现代的、用Flask构建Web应用程序和API的平台,同时尽可能真实地展现Flask的精神和API。
为什么?
- 旨在让您的应用程序易于启动,甚至更易于发展
- 干净、可预测的应用程序结构,鼓励良好的设计模式(无循环导入!)
- 记录真实世界的使用情况(有些部分仍在进行中)
- 支持的库之间没有集成问题;一切正常
- 绝对要尽量少用样板文件(你的注意力应该放在你的应用上,而不是管道上!)
- 对
pytest的测试的开箱即用支持
- 用于自定义、扩展和/或覆盖几乎所有内容的简单且一致的模式(例如配置、视图/控制器/资源、路由、模板、服务、扩展等)
- 您的定制很容易作为一个独立的包(python包)分发,它本身支持相同的定制模式,无限期。
- 用于自定义、扩展和/或覆盖几乎所有内容的简单且一致的模式(例如配置、视图/控制器/资源、路由、模板、服务、扩展等)
怎么做?
flask unchained实现了应用程序工厂模式,使用标准化(但可配置)的方式组织"bundle"代码,使它们在多个独立的项目中易于分发、重用和自定义。bundle中的所有代码会自动发现并在应用程序中注册。您可以将bundle看作是烧瓶蓝图和扩展的增强替代品。bundle与django的"apps"有些相似,但我想您会发现bundle更加强大和灵活。
这个架构的灵感来自于symfony,它是企业级的,除了它不是python;)如果你听说过laravel,这个框架是建立在//symfony.com/components" rel="nofollow">symfony组件-在烧瓶未受约束的情况下,现有烧瓶/蟒蛇生态系统构成我们的"组件"。[*]
[*]不喜欢默认选项?烧瓶未经约束几乎是完全可定制的。核心体系结构为您提供了使用完全不同的库堆栈的潜力,或仅交换某些组件的潜力-选择的权力是您的。
有用链接
目录
功能
- 巨蟒3.6+
- 使用控制器,资源和改进了基于类的视图ref="https://flask unchained.readthedocs.io/en/latest/api/api/api\u bundle.html\modelresource" rel="nofollow">模型资源基类
- 声明性路由
- 服务和扩展的依赖注入
- 包括开箱即用(大部分是可选的)集成:
- sqlalchemy和烧瓶迁移(数据库orm和迁移,可选)。sqlalchemy unchained在sqlalchemy的基础上构建一些"糖",以使现有的最佳orm更快、更容易使用:
- 除非您自己定义主键列,否则模型将自动获取主键列
- 模型在创建和更新时(可选)自动加上时间戳
- 超简单多态模型类继承
- 一个用于创建、查询、更新和删除模型的统一api,使用modelmanager的
- 烧瓶登录(用户身份验证和会话管理)和烧瓶主体(具有权限和角色的用户授权)
- 当前支持会话和令牌身份验证
- 包括可选的注册支持(在激活帐户前提供可选的必需电子邮件确认)
- 可选的更改密码和忘记密码功能
- 烧瓶棉花糖(sqlalchemy模型序列化,可选)
- api bundle提供了一个(工作正在进行中的)restful api框架,它集成了marshmallow序列化器(aka schemas)和sqlalchemy模型,使用了模型资源
- 对openapi(又名swagger)文档的工作进行中支持,将redoc用作前端
- flask-graphql(graphql支持,将graphne与sqlalchemy集成,可选)
- 烧瓶wtf(表单和CSRF保护,始终启用)
- 烧瓶会话(服务器端会话,可选)
- 芹菜(分布式任务队列,选项铝)
- 烧瓶邮件(电子邮件发送支持,可选)
- 烧瓶管理(管理界面,可选)
- 烧瓶-巴别莱克斯(翻译,始终启用,但可选)
- pytest和工厂男孩(测试框架)
- sqlalchemy和烧瓶迁移(数据库orm和迁移,可选)。sqlalchemy unchained在sqlalchemy的基础上构建一些"糖",以使现有的最佳orm更快、更容易使用:
快速启动
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run
注意:如果启用了sqlalchemy包,则需要安装alembic1.0.9版或更高版本。
在启动开发服务器之前,您可能还需要运行迁移:
flask db init
flask db migrate -m 'create initial tables'
flask db upgrade
它看起来像什么?
应用结构和项目布局
与Stock Flask不同,Flask未受限制的应用程序不能在单个文件中编写。相反,flask unchained的bundles定义了一个(可配置的)文件夹约定,为了使flaskunchained能够正确地发现您的所有代码,必须遵循这个约定。大型应用程序结构可能如下所示:
/home/user/dev/project-root
├── app # your app bundle package
│ ├── admins # model admins
│ ├── commands # Click CLI groups/commands
│ ├── extensions # Flask extensions
│ ├── models # SQLAlchemy models
│ ├── fixtures # SQLAlchemy model fixtures (for seeding the dev db)
│ ├── serializers # Marshmallow serializers (aka schemas)
│ ├── services # dependency-injectable services
│ ├── tasks # Celery tasks
│ ├── templates # Jinja2 templates
│ ├── views # Controllers, Resources and views
│ └── __init__.py
│ └── config.py # app config
│ └── routes.py # declarative routes
├── assets # static assets to be handled by Webpack
│ ├── images
│ ├── scripts
│ └── styles
├── bundles # custom bundles and/or bundle extensions/overrides
│ └── security # a customized/extended Security Bundle
│ ├── models
│ ├── serializers
│ ├── services
│ ├── templates
│ └── __init__.py
├── db
│ └── migrations # Alembic (SQLAlchemy) migrations (generated by Flask-Migrate)
├── static # static assets (Webpack compiles to here, and Flask
│ # serves this folder at /static (by default))
├── templates # the top-level templates folder
├── tests # your pytest tests
├── webpack # Webpack configs
└── unchained_config.py # the Flask Unchained config
要了解如何构建这样一个更大的示例应用程序,请查看官方教程
最小的应用程序结构如下:
/home/user/dev/hello-flask-unchained
├── app
│ ├── templates
│ │ └── site
│ │ └── index.html
│ ├── __init__.py
│ ├── config.py
│ ├── forms.py
│ ├── models.py
│ ├── routes.py
│ ├── services.py
│ └── views.py
└── unchained_config.py
我们来建造它吧!
# (create a virtualenv and activate it) pip install flask-unchained[dev,sqlalchemy]\&& mkdir -p hello-flask-unchained/app &&cd hello-flask-unchained \&& touch unchained_config.py &&cd app \&& mkdir -p templates/site && touch templates/site/index.html \&& touch __init__.py config.py forms.py models.py routes.py services.py views.py
束
第一步是在项目根目录中创建一个app bundle模块-我们在这里调用我们的app
-其中有一个appbundle
子类:
# hello-flask-unchained/app/__init__.pyfromflask_unchainedimportAppBundle,FlaskUnchainedclassApp(AppBundle):# you only need to subclass AppBundle; everything below is optional. for example, these# attributes can be set to customize which bundle module to load from (defaults shown)config_module_name='config'models_module_name='models'routes_module_name='routes'services_module_name='services'# these two methods are optional callbacks which the app factory will calldefbefore_init_app(self,app:FlaskUnchained):passdefafter_init_app(self,app:FlaskUnchained):pass
配置
烧瓶本身的配置是非常小的,例如:
# hello-flask-unchained/unchained_config.pyimportosPROJECT_ROOT=os.path.abspath(os.path.dirname(__file__))deffolder_or_none(folder_name):folder=os.path.join(PROJECT_ROOT,folder_name)returnfolderifos.path.exists(folder)elseNone# these get passed to the :class:`FlaskUnchained` constructorTEMPLATE_FOLDER=folder_or_none('templates')STATIC_FOLDER=folder_or_none('static')STATIC_URL_PATH='/static'ifSTATIC_FOLDERelseNone# declare which bundles Flask Unchained should loadBUNDLES=['flask_unchained.bundles.sqlalchemy','app',# your app bundle *must* be last]
并将所需的应用程序配置添加到应用程序捆绑包:
# hello-flask-unchained/app/config.pyimportosfromflask_unchainedimportAppBundleConfigclassConfig(AppBundleConfig):SECRET_KEY=os.getenv('FLASK_SECRET_KEY','change-me-to-a-secret-key')# the following env-specific config classes are all optionalclassDevConfig(Config):passclassProdConfig(Config):passclassStagingConfig(ProdConfig):pass
型号
# hello-flask-unchained/app/models.pyfromflask_unchained.bundles.sqlalchemyimportdbclassNameSubmission(db.Model):classMeta:repr=('id','name')name=db.Column(db.String(128))# the following primary key and timestamp columns are automatically added to models# (if necessary/not customized)# id = db.Column(db.Integer, primary_key=True)# created_at = db.Column(db.DateTime, server_default=sqlalchemy.func.now())# updated_at = db.Column(db.DateTime, server_default=sqlalchemy.func.now(),# onupdate=sqlalchemy.func.now())
服务
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run0
表格
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run1
视图
您好,世界观:
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run2
模板
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run3
# (create a virtual environment) pip install flask-unchained[dev] flask new project <your-project-folder-name> --prompt # (answer the questions and `cd` into the new directory) pip install -r requirements-dev.txt flask run4
路线
现在我们可以用 运行它: 现在您应该可以浏览到http://localhost:5000 rel="nofollow">http://localhost:5000来查看您的新站点! 我们非常欢迎您的贡献!这是一个大项目,需要做很多不同的事情。在项目根目录中有一个todo文件,或者如果您有想法,请打开一个问题或公关,让我们聊天。 麻省理工学院< /P>路由注册控制器
# (create a virtual environment)
pip install flask-unchained[dev]
flask new project <your-project-folder-name> --prompt
# (answer the questions and `cd` into the new directory)
pip install -r requirements-dev.txt
flask run
5
命令
# (create a virtual environment)
pip install flask-unchained[dev]
flask new project <your-project-folder-name> --prompt
# (answer the questions and `cd` into the new directory)
pip install -r requirements-dev.txt
flask run
6
贡献
许可证
推荐PyPI第三方库