Python Web Development Libraries 简明教程
Pyramid Framework
Pyramid 是一个通用、开源的 web 应用程序开发框架,用 python 构建。它允许 python 开发人员轻松创建 web 应用程序。
Pyramid is a general, open source, web application development framework built in python. It allows python developer to create web applications with ease.
Installing, starting up and configuring
正如所描述的,“从小开始,大功告成,坚持到底的框架”,Pyramid 非常像 Flask,安装和运行时只需要很少的工作。事实上,一旦你开始构建这个应用程序,你就会发现其中的一些模式类似于 Flask。
As described, “the start small, finish big, stay finished framework”, Pyramid is much like Flask which takes very little effort to install and run. In fact, you’ll recognize that some of the patterns are similar to Flask once you start building this application.
以下是创建 Pyramid 框架环境的步骤:
Following are the steps to create pyramid framework environment −
-
First, create a project directory. Here, we have created a directory named pyramidProject (you can choose any name you want).
-
Next, create a virtual environment where you will install all the project specific dependencies. Here, we created a virtual environment folder named pyramidEnv where Pyramid is installed.
-
Then, go to the directory, pyramidEnv and install the pyramid with pip install pyramid.
完成上述所有操作后,你的目录结构如下所示:
Once everything is done as mentioned above, your directory structure will be as shown below −

系统中安装的 Pyramid 版本如下所示:
And the pyramid version installed in the system is given below −

Core Concepts
Pyramid 框架基于以下核心概念:
The Pyramid framework is based on below core concepts −
-
Zope (extensibility, traversal, declarative security) − Pyramid is loosely based on Zope in terms of extensibility, the concept of traversal and the declarative security.
-
Pylons (URL dispatch, non-opinionated view of persistence, templating, etc.) − Another area from where pyramid draws its concept is the pylons project. Pylons have that concept of routes, that calls the URL dispatch inside the pyramid framework and they also have the non-opinionated view of persistence layer or templating.
-
Django (View, level of documentation) − Pyramid also gets hint from Django. The way we take our view, routed our URL and the level of documentation is very Django way.
以下是 Pyramid 框架的功能:
The following are the features of the Pyramid framework −
-
It is the fastest known Python web framework.
-
It supports small and large projects (why rewrite when you outgrow your small framework).
-
It supports single file webapps like microframeworks.
-
It has built-in sessions.
-
It supports events similar to Plone/Zope.
-
It provides Transaction Management (if already have noticed that we have used Zope before).
Configuration
配置是影响应用程序操作的设置。配置 Pyramid 应用程序有两种方法:命令式配置和声明式配置。
Configuration is the settings that influence the operation of an application. There are two ways to configure a pyramid application: imperative configuration and declarative configuration.
Pyramid 配置支持:
Pyramid configuration supports −
-
Imperative configuration or even the overriding of the decorator-based configs
-
Configuration conflict detection (including more local vs. less local determination)
-
Configuration Extensibility (included from multiple apps)
-
Flexible Authentication and Authorization Policies
-
Programmatic Introspection of Configuration (view current state of routes to generate nav)
URL generation
在金字塔中,我们可以为路径、资源和静态资源生成 URL。使用 URL 生成 API 既容易又灵活。通过金字塔的多个 API 生成 URL,用户可以任意更改配置,而不必担心破坏任何网页的链接。
In pyramid, we can generate URLs for routes, resources and static assets. It is easy and flexible to work with URL generation APIs. By generating URLs through pyramid’s various APIs, users can change the configuration arbitrarily without much worry of breaking a link with any of your web pages.
简而言之,金字塔中的 URL −
So in short, URL in pyramid −
-
supports URL generation to allow changes to app that won’t break links.
-
generates URLs to static resources that live either inside or outside the application.
-
supports Routes and Traversal.
Views
金字塔的主要任务之一是在请求到达应用程序时查找并调用视图可调用对象。视图可调用对象是在对应用程序中的请求做出响应时执行一些有趣操作的代码片段。
One of the primary jobs of pyramid is to find and invoke a view callable when a request reaches your application. View callables are bits of code which do something interesting in response to a request made in your application.
在将视图映射到 URL 调度或 Python 代码时,可以进行任何类型的调用。视图可以是函数声明或实例,可以在金字塔中用作视图。
When you map your views onto your URL dispatch or python code, there can be any kind of call. Views can be a function declaration or an instance, it can be used as a view in the pyramid.
下面给出视图的一些重要要点:
Some important points about Views are given below −
-
Views are generated from any callable.
-
Renderer based views can simply return dictionaries (not required to return a webby style object).
-
Support multiple views per route (GET vs. POST vs. HTTP Header check, etc.).
-
View response adapters (when you want to specify how view returns values should be handled vs. response objects).
Extensibility
金字塔的设计考虑了可扩展性。因此,如果金字塔开发人员在构建应用程序时牢记某些约束,第三方应该能够在无需修改源代码的情况下更改应用程序的行为。遵守某些约束的金字塔应用程序的行为可以在不进行任何修改的情况下进行覆盖或扩展。它设计用于针对多个环境的灵活部署(无单例)。金字塔具有“Tweens”中间件支持(WSGI 中间件,但在金字塔本身的上下文中运行)。
Pyramid is designed with extensibility in mind. So if a pyramid developer is keeping in mind certain constraints while building an application, a third party should be able to change the application’s behaviour without needing to modify its source code. The behaviour of a pyramid application that obeys certain constraints can be overridden or extended without any modification. It is designed for flexible deployments to multiple environments (No Singletons). Pyramid has “Tweens” middleware support (WSGI middle ware, but runs in the context of Pyramid itself).
Running a Hello, Pyramid Program
在安装金字塔框架以检查一切都运行良好之后,我们可以想到的最简单的程序是运行一个简单的“Hello, World”或“Hello, Pyramid”程序。
The simplest program we can think after installing pyramid framework to check if everything is working fine, is to run a simple “Hello, World” or “Hello, Pyramid” program.
下面是我在端口号 8000 上运行的金字塔“Hello, Pyramid”程序:
Below is my pyramid “Hello, Pyramid” program on 8000 port number −

上面的示例很容易运行。将此保存为 app.py(在此,我们给出了名称 pyramid_helloW.py)。
Above simple example is easy to run. Save this as app.py (In this, we have given the name pyramid_helloW.py).
运行最简单的程序:-
Running the simplest program: −

接下来,在浏览器中打开 http://localhost:8000/ ,您将看到 Hello, Pyramid! 消息如下所示 −
Next, open http://localhost:8000/ in a browser, and you will see the Hello, Pyramid! Message as follows −

以下是上述代码的说明 −
The following is the explanation for above code −
Line no. 1-3
在文件开头,我们有 import 语句。第一行导入 make_server 函数,该函数可以创建一个简单的 Web 服务器,当它传递给应用程序时。第二行和第三行从 pyramid 导入配置和响应函数。这些函数分别用于配置详细信息、设置应用程序的参数以及响应请求。
At the head of the file, we have import statements. The first line imports make_server function, which can create a simple web server when it is passed to an application. The second and third line import the configuration and Response function from pyramid. These functions are used to configure details and set parameters for the application and respond to requests, respectively.
Line no. 5-6
现在,我们有一个名为 hello_world 的函数定义。实现生成响应的视图代码。满足视图要求的函数负责呈现将传递回请求实体的文本。在上述情况下,该函数在调用时使用我们之前导入的响应函数。这会回传一个应该提供给客户端的值。
Now we have a function definition called hello_world. Implement view code that generates the response. A function that fulfils the requirement of a view is responsible for rendering the text that will be passed back to the requesting entity. In the above case, the function, when called, uses the Response function we imported earlier. This passes back a value that should be given to the client.
Line no. 8
if name == ‘ main ’:Python 在从命令行运行时会说“从这里开始”,而不是在导入此模块时。
if name == ‘main’: Python is saying, “Start here when running from the command line”, rather than when this module is imported.
Line no. 9-11
在第 9 行中,我们创建一个名为 config 的变量,该变量来自我们在程序顶部导入的配置器函数创建的对象。第 10 行和第 11 行调用此对象的 add_route 和 add_view 方法。此方法用于定义应用程序可以使用的视图。正如我们所看到的,我们传递了我们之前定义的 hello_world 函数。这是该函数实际上作为视图被合并的地方。
In line no. 9, we create a variable called config out of the object created by the configurator function that we imported at the top of the program. Line 10 and 11 call the add_route and add_view method of this object. This method is used to define a view that can be used by the application. As we can see, we pass the hello_world function we defined earlier. This is where that function is actually incorporated as a view.
Line no. 12-14
在此,我们实际上通过调用配置对象的 make_wsgi_app 方法来创建 WSGI 应用程序。这使用对象的属性(例如,我们添加的视图)来创建一个应用程序。然后将此应用程序传递给我们在导入时导入的 make_server 函数,以创建一个可以启动 Web 服务器来为我们的应用程序提供服务的对象。最后一行启动此服务器。
In this, we actually create the WSGI application by calling the make_wsgi_app method of the config object. This uses the object’s attributes, such as the view we added, to create an application. This application is then passed to the make_server function we imported in order to create an object that can launch a web server to serve our application. The last line launches this server.
我们的 hello world application 是最简单、最容易使用的金字塔应用程序之一,配置为“命令式”。它是必要的,因为我们在执行配置任务时可以使用 Python 的全部功能。
Our hello world application is one of the simplest and easiest possible pyramid applications, configured “imperatively”. It is imperative because the full power of Python is available to us as we perform configuration tasks.
总之,Pyramid 是一个拥有庞大且活跃社区的开源 Python Web 框架。这个大型社区为使 Python Web 框架流行且相关做出了贡献。Pyramid Web 框架通过提供一组健壮的功能和工具简化并加速了 Web 应用程序的开发。
To summarize, Pyramid is an open source python web framework with a large and active community. This large community contributes towards making the python web framework popular and relevant. Pyramid web framework simplify and accelerate web application development by providing a set of robust features and tools.