2016-12-19 70 views
0

我是新來的Python燒瓶如何給結構蟒蛇燒瓶應用

嘗試用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() 

上面的代碼包含兩個端點後如下圖所示並獲得其工作正常數據

知道尋找一種方式將代碼分成不同的文件中像

數據庫連接RELAT編輯代碼應該是在不同的文件

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) 

我應該能夠得到在不同的文件DB查詢在架構定義和使用它

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 

在端點文件中,我應該得到數據庫模式的參考,請幫助我獲取此結構

點我的一些理解樣品或視頻,可以幫我做的,我是新來的蟒蛇以及燒瓶請點一些樣本,並有助於瞭解更多的感謝

回答

1

基本結構看起來是這樣的:

/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有時。包含您的網址端點和相關行爲。

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 /靜態/ ...包含您的靜態文件。

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

requirements.txt具有包依賴關係的快照。

venv(Virtualenv)文件夾,您的python庫可以在一個包含的環境中工作。

參考文獻:

Have a look at this related question.

Good example of a widely used project structure.

+0

@Beat \t 我已經更新了我的答案,包括更廣泛的結構可以是什麼樣子的概述。請再次查看。 –

+0

我與上面相同的目錄,但是當我運行run.py我得到的錯誤有回溯(最近呼叫最後): 文件「run.py」,第1行,在 from flaskApp導入create_app 文件「d:\角蟒\ pythonAppStructure \ flaskApp \ __ init__.py」,3號線,在 從配置導入配置 導入錯誤:無法導入名稱配置 請什麼我是個做錯了 –

+0

@dhana拉克希米試着改變它太「從配置導入配置」(第二個配置大寫字母) –