使用Pyramid框架構建Python
- 2020 年 1 月 8 日
- 筆記
關於Pyramid框架
在本博另一篇文章「使用Buildout進行開發」中,有講到使用Buildout來將Pyramid框架集成到Python環境中,並構建一個「helloworld」級別的Python Web應用。
Web框架旨在提供一個快速、簡單的方式來跳躍式的開始一個Web應用。幾乎每一個框架都遵從MVC軟件模式,MVC代表模型(model)、視圖(view)和控制器(controller)。這是一種以分辨和分隔應用中的不同功能來簡化應用的設計和允許每一個部件的修改完全獨立於其他的模式。
Pyramid是一個輕量級的Python應用的web框架,它允許非常快速的擁有你的基本的web應用並運行之。事實上,它能夠將所有的框架都放置在單一文件中,只要你喜歡。
使用Pyramid構建web應用
1. 依照本博的另外一篇文章,可以創建一個Python的虛擬環境:
#virtualenv –no-site-packages env
#source env/bin/activate
2. 創建項目主目錄,並在虛擬環境中安裝Pyramid:
#mkdir pyramid_sites
#cd pyramid_sites
#easy_install pyramid
上述命令會將框架需要的所有文件都安裝到虛擬環境中。
3. 創建一個helloworld示例項目:
#mkidr hello_world
#cd hello_world
創建應用腳本文件,並添加以下內容:
#vim application.py
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('<h1>Hello world!</h1>')
if __name__ == '__main__':
config = Configurator()
config.add_view(hello_world)
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
註解:
1. import語句部分:make_server函數能夠創建一個web服務器,當它運行了一個應用時;Configurator和Response是Pyramid中的函數,這些函數分別被使用來為應用配置細節和設置參數以及對請求作出反應。
2. hello_world函數部分代表了我們應用的一個視圖,通過Response函數將請求反應值傳送到客戶端。
3. 主函數是程序真正執行的地方,通過運行主函數的實例來配置和構建服務器。
保存上述文件,並執行:
#python application.py
通過Web訪問helloworld應用:

使用Scaffolding構建一個Pyramid應用
在上述示例中,所有的工作都是在一個文件中(application.py)完成,雖然這是一種非常好的方式來展示如何使用Pyramid來壓縮和簡化構建MVC應用的過程,但這不總是最簡單的方法。
與大多數流行框架類似,Pyramid能夠使用scaffolding來快速創建一個複雜的項目目錄結構。通過pcreate這個工具來使用scaffolding。Wikipedia上關於scaffolding的介紹為:
Scaffolding is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used. The compiler uses this specification to generate code that the application can use to create, read, update and delete database entries, effectively treating the template as a "scaffold" on which to build a more powerful application.
進入Pyramid主目錄下,查看可用的scaffolding:
#cd pyramid_sites
#pcreate -l
Available scaffolds: alchemy: Pyramid SQLAlchemy project using url dispatch starter: Pyramid starter project zodb: Pyramid ZODB project using traversal
註解:
1. alchemy能夠使用SQL融合來創建一個項目
2. starter能夠創建一個在應用實體之間非持續性的基本項目
3. zodb能夠創建一個依靠ZODB來運行的項目
使用starter模板來創建第一個項目:
#pcreate -s starter first_project
#cd first_project
#ls
CHANGES.txt first_project MANIFEST.in README.txt development.ini first_project.egg-info production.ini setup.py
在該目錄下的文件大多是用於配置的,程序本身主要包含在以項目名字命名的一個子目錄中
運行setup腳本來配置應用的開發環境:
#python setup.py develop
running develop running egg_info writing requirements to first_project.egg-info/requires.tx writing first_project.egg-info/PKG-INFO writing top-level names to first_project.egg-info/top_level.txt writing dependency_links to first_project.egg-info/dependency_links.txt writing entry points to first_project.egg-info/entry_points.txt writing requirements to first_project.egg-info/requires.txt writing first_project.egg-info/PKG-INFO writing top-level names to first_project.egg-info/top_level.txt writing dependency_links to first_project.egg-info/dependency_links.txt writing entry points to first_project.egg-info/entry_points.txt reading manifest file 'first_project.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.cfg'
……
該命令將使用定義在development.ini中的可用參數來配置你的應用
最後運行你的項目:
#pserve development.ini
Starting server in PID 21251.
增加調試面板:Pyramid Debug Toolbar,
#vim development.ini
#在[app:main]選項中增加以下語句來使得所有連接到的主機都能夠看到調試面板
debugtoolbar.hosts = 0.0.0.0/0
保存並退出,重啟服務器可以看到調試面板在右手邊
#pserve development.ini
——游響雲停