前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Pyramid框架构建Python

使用Pyramid框架构建Python

作者头像
py3study
发布2020-01-08 19:29:52
1K0
发布2020-01-08 19:29:52
举报
文章被收录于专栏:python3python3

关于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应用:

wKioL1NQ3IagJPY9AAApFLI637Q598.jpg
wKioL1NQ3IagJPY9AAApFLI637Q598.jpg

使用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

                                                                                                                             ——游响云停


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档