Flask新建應用

2021-08-21 02:47:16 字數 1350 閱讀 4855

flask應用是乙個flask類的例項。所有關於應用的內容,例如配置與url,都會隨著類一起註冊。

建立flask應用最簡單的辦法,就是在**最頂層建立乙個全域性flask例項。但是隨著**量的增加,這種方法會引發很多棘手的問題。

在函式內部建立flask例項是個比較好的方法,這個函式被稱為應用工廠,所有的配置、註冊以及其他構件過程都在這個函式裡進行,最後返回應用。

應用工廠

建立flaskr目錄並加入乙個__init__.py檔案。這個檔案提供這些功能:包含應用工廠,並且告訴python直譯器,flaskr目錄是乙個應用包。

mkdir flaskr
flaskr/__init__.py

import os

from flask import flask

def(test_config=none):

secret_key='dev',

)if test_config is

none:

# load the instance config, if it exists, when not testing

else:

# load the test config if passed in

# ensure the instance folder exists

try:

except oserror:

pass

# a ****** page that says hello

defhello

():return

'hello, world!'

建立了flask例項

設定了預設配置

執行應用

以開發模式執行:

linux or mac

export flask_env=development

flask run

windows cmd

set flask_env=development

flask run

windows powershell

$env:flask_env = "development"

flask run

配置無誤的話會顯示如下資訊:

* environment: development

* debug mode: on

* running on (press ctrl+c to quit)

* restarting with stat

* debugger is active!

* debugger pin: 855-212-761

flask應用框架

flask是乙個用python編寫的web應用程式微型框架,基於werkzeug wsgi工具包和jinja2模板引擎。werkzeug是乙個wsgi web server gateway inte ce,web伺服器閘道器介面wsgi 工具包,實現了請求 響應物件等,用於構建web框架。jinja...

搭建 flask 應用

1.使用pycharm建立flask應用 from flask import flask 匯入乙個flask的類 例項化flask物件 使用route裝飾器告訴flask什麼樣的url能觸發我們的函式 def index return index page hello defhello return...

ECSTORE 新建APP應用

1 window平台 23 456 linux平台 78 3.接下來我們建立幾個基本必要的資料夾和檔案。4.desktop.xml是後台選單 顯示 檔案。所以我們首要先修改desktop.xml,配置內容如下 1 desktop 2 permissions 3 permission idworkgr...