gpt4 book ai didi

python - 如何为python flask 应用程序提供结构

转载 作者:行者123 更新时间:2023-12-04 16:08:37 25 4
gpt4 key购买 nike

我是 python flask 的新手

在单个文件中使用 MongoDB 试验一些端点,如下所示

from flask import Flask, request
from flask.ext.mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['MONGOALCHEMY_DATABASE'] = 'library'
db = MongoAlchemy(app)


class Author(db.Document):
name = db.StringField()


class Book(db.Document):
title = db.StringField()
author = db.DocumentField(Author)
year = db.IntField();


@app.route('/author/new')
def new_author():
"""Creates a new author by a giving name (via GET parameter)
e.g.: GET /author/new?name=Francisco creates a author named Francisco
"""
author = Author(name=request.args.get('name', ''))
author.save()
return 'Saved :)'






@app.route('/authors/')
def list_authors():
"""List all authors.

e.g.: GET /authors"""
authors = Author.query.all()
content = '<p>Authors:</p>'
for author in authors:
content += '<p>%s</p>' % author.name
return content

if __name__ == '__main__':
app.run()

上面的代码包含两个端点来发布和获取工作正常的数据

知道寻找一种方法将代码分成不同的文件,例如

数据库连接相关代码应该在不同的文件中

from flask import Flask, request
from flask.ext.mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['MONGOALCHEMY_DATABASE'] = 'library'
db = MongoAlchemy(app)

我应该能够在定义架构的不同文件中获取数据库引用并使用它

class Author(db.Document):
name = db.StringField()


class Book(db.Document):
title = db.StringField()
author = db.DocumentField(Author)
year = db.IntField();

和路线将是不同的文件

@app.route('/author/new')
def new_author():
"""Creates a new author by a giving name (via GET parameter)
e.g.: GET /author/new?name=Francisco creates a author named Francisco
"""
author = Author(name=request.args.get('name', ''))
author.save()
return 'Saved :)'






@app.route('/authors/')
def list_authors():
"""List all authors.

e.g.: GET /authors"""
authors = Author.query.all()
content = '<p>Authors:</p>'
for author in authors:
content += '<p>%s</p>' % author.name
return content

在端点文件中我应该得到数据库模式的引用,请帮助我获得这个结构

给我指一些可以帮助我做的可以理解的示例或视频,我是 python 和 flask 的新手,请指点一些示例并帮助了解更多谢谢

最佳答案

基本结构可能如下所示:

/yourapp  
/run.py
/config.py
/yourapp
/__init__.py
/views.py
/models.py
/static/
/main.css
/templates/
/base.html
/requirements.txt
/venv

应用于您的示例,它看起来像这样。

run.py:启动您的应用程序。

from yourapp import create_app

app = create_app()
if __name__ == '__main__':
app.run()

config.py:包含配置,您可以添加子类来区分开发配置、测试配置和生产配置

class Config:
DEBUG = True
MONGOALCHEMY_DATABASE = 'library'

yourapp/_init_.py:初始化您的应用程序,创建一个 Flask 实例。 (也使您的应用程序成为一个包)。

from flask import Flask
from flask.ext.mongoalchemy import MongoAlchemy
from config import Config

db = MongoAlchemy()

def create_app():
app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)

from views import author_bp
app.register_blueprint(author_bp)

return app

yourapp/models.py:包含您的不同模型。

from . import db

class Author(db.Document):
name = db.StringField()


class Book(db.Document):
title = db.StringField()
author = db.DocumentField(Author)
year = db.IntField();

yourapp/views.py:有时也称为 routes.py。包含您的 url 端点和相关的行为。

from flask import Blueprint
from .models import Author

author_bp = Blueprint('author', __name__)

@author_bp.route('/author/new')
def new_author():
"""Creates a new author by a giving name (via GET parameter)
e.g.: GET /author/new?name=Francisco creates a author named Francisco
"""
author = Author(name=request.args.get('name', ''))
author.save()
return 'Saved :)'

@author_bp.route('/authors/')
def list_authors():
"""List all authors.
e.g.: GET /authors"""
authors = Author.query.all()
content = '<p>Authors:</p>'
for author in authors:
content += '<p>%s</p>' % author.name
return content

yourapp/static/... 包含您的静态文件。

yourapp/templates/.. 包含您的模板。

requirements.txt 包含您的软件包依赖项的快照。

venv (Virtualenv) 文件夹,您的 python 库将能够在包含的环境中工作。

引用资料:

Have a look at this related question.

Good example of a widely used project structure.

关于python - 如何为python flask 应用程序提供结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41219635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com