序言
首先,我們使用 virtualenv 隔離我的專案。例如,我們要開發一個輪詢應用程式(a poll app)。
mkdir poll_app cd poll_app virtualenv . source bin/activate
常用 Python 函式庫
我正在開發需要資料庫的應用程式。因此,我總是使用 flask_script 和 flask_migrate 函式庫。我不喜歡 Flask 的 CLI 工具。
Flask-Script: https://flask-script.readthedocs.io/en/latest/
Flask-Migrate: https://flask-migrate.readthedocs.io/en/latest/
#和Django 類似地,我在根資料夾中建立了一個名為manage. py 的Python 文件,例如:
from MYAPP.data.models import db from MYAPP import app from flask_script import Manager from flask_migrate import Migrate, MigrateCommand db.init_app(app) migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) if __name__ == "__main__": manager.run()
然後,我們可以對資料可進行以下操作:
python manage.py db init # --> init migrations python manage.py db migrate # --> migrate models python manage.py db upgrade # --> apply changes python manage.py db --help # --> :)
主要應用程式檔案
##建立新項目時,我在根資料夾中建立了一個檔案app.py,然後它會像這樣更改。from MYAPP import app # To do: This place will change later config = { "development": "config.Development" } if __name__ == "__main__": app.config.from_object(config["development"]) app.run()
設定檔
我還在根資料夾中建立了一個名為 config.py 的設定檔。class BaseConfig(object): """ Base config class. This fields will use by production and development server """ ORIGINS = ["*"] # for api calls SECRET_KEY = 'YOUR SECRET KEY' class Development(BaseConfig): """ Development config. We use Debug mode """ PORT = 5000 DEBUG = True TESTING = False ENV = 'dev' # Currently we only have development config. # If you have production, you will need to pass it to here. config = { 'development': 'config.Development' } def configure_app(app): """ App configuration will be here. Parameters ---------- app : Flask app instance """ app.config.from_object(config['development'])
資料夾結構
我在根目錄中建立一個資料夾,並將其命名為 om_core ,然後在奇中建立兩個新的資料夾api 和data.。 api 檔案儲應用程式邏輯和路由。例如,我在 api 中建立了一個名為 user 的資料夾。 在 user 資料夾中產生兩個名為 init.py 和 controllers.py 的文件,我們其他的 Api 層也會這樣。 controllers.py (控制器檔案)應該像這樣:from flask import Blueprint, jsonify, request from MYAPP.data.models import db, User user = Blueprint('user', __name__) @user.route('/', methods=['GET']) def get_users(): return jsonify({ "message": "Hi user :)"}) @user.route('/<int:id>', methods=['GET']) def users(id): return jsonify({ "id": id })
from flask_sqlalchemy import SQLAlchemy from MYAPP import app # We didn't pass app instance here. db = SQLAlchemy() class User(db.Model): """ Model for user management """ id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(100), unique=True) password = db.Column(db.String(100)) name = db.Column(db.String(100)) surname = db.Column(db.String(100)) active = db.Column(db.Boolean(), default=True) created_at = db.Column(db.DateTime, default=db.func.now()) updated_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, email, password, name, surname, active, created_at, updated_at): self.email = email self.password = password self.name = name self.surname = surname self.active = active self.created_at = created_at self.updated_at = updated_at
from flask import Flask from flask_cors import CORS from config import BaseConfig from config import configure_app app = Flask(__name__) from MYAPP.api.user.controllers import user """ Corst settings will be here. We maybe use this endpoint later. """ cors = CORS(app, resources={ r'/api/*': { 'origins': BaseConfig.ORIGINS } }) configure_app(app) app.url_map.strict_slashes = False app.register_blueprint(user, url_prefix='/api/users')
Python教學》
以上是了解 Flask 專案結構的詳細內容。更多資訊請關注PHP中文網其他相關文章!