Python Pyramid 简明教程

Python Pyramid - Creating A Project

假定 Pyramid 虚拟环境已启动并运行,并且 Cookiecutter 已安装在其中。创建 Cookiecutter 项目最简单的方法是使用预构建的启动模板,如下命令所示:

It is assumed that a Pyramid virtual environment is up and running, and Cookiecutter is installed in it. The easiest way to create a Cookiecutter project is to use a pre-built starter template as per the following command −

cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout 2.0-branch

模板下载后,会询问用户选择何种项目名称 −

The template is downloaded and the user is asked about his choice of name of the project −

project_name [Pyramid Scaffold]: testproj
repo_name [testproj]:

接下来,选择模板语言。

Then choose the template language.

选择 template_language

Select template_language

1 - jinja2
2 - chameleon
3 - mako
Choose from 1, 2, 3 [1]: 1

既然我们熟悉 jinja2,请选择 1。接下来,使用 SQLALchemy 作为后端。

Since we are familiar with jinja2, give 1 as the choice. Next, use SQLALchemy as the backend.

Select backend:
1 - none
2 - sqlalchemy
3 - zodb
Choose from 1, 2, 3 [1]: 2

testproj 文件夹中,创建以下文件结构 −

Inside the testproj folder, following file structure is created −

│ development.ini
│ MANIFEST.in
│ production.ini
│ pytest.ini
│ README.txt
│ setup.py
│ testing.ini
│
├───testproj
│ │ pshell.py
│ │ routes.py
│ │ __init__.py
│ │
│ ├───alembic
│ │ │ env.py
│ │ │ script.py.mako
│ │ │
│ │ └───versions
│ │ README.txt
│ │
│ ├───models
│ │ meta.py
│ │ mymodel.py
│ │ __init__.py
│ │
│ ├───scripts
│ │ initialize_db.py
│ │ __init__.py
│ │
│ ├───static
│ │ pyramid-16x16.png
│ │ pyramid.png
│ │ theme.css
│ │
│ ├───templates
│ │ 404.jinja2
│ │ layout.jinja2
│ │ mytemplate.jinja2
│ │
│ └───views
│ default.py
│ notfound.py
│ __init__.py
│
└───tests
    conftest.py
    test_functional.py
    test_views.py
    __init__.py

外部 testproj 文件夹有一个内部 testproj 包子文件夹和 test 包。内部 testproj 子文件夹是一个具有模型、脚本、子包以及静态和模板文件夹的包。

The outer testproj folder has an inner testproj package subfolder and tests package. The inner testproj subfolder is a package having models and scripts, subpackages, and static as well as templates folders.

接下来,使用 Alembic 初始化和升级数据库。

Next, initialize and upgrade the database using Alembic.

# Generate your first revision.
alembic -c development.ini revision --autogenerate -m "init"
# Upgrade to that revision.
alembic -c development.ini upgrade head

Alembic 是一个轻量级数据库迁移工具,可与 Python 的 SQLAlchemy 数据库工具包一起使用。外部项目文件夹现在将显示 testproj.sqlite 数据库。

Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. The outer project folder will now show a testproj.sqlite database.

development.ini 文件为数据库提供了默认数据。通过以下命令使用它填充数据库。

The development.ini file provides a default data for the database. Populate the database with it by the following command.

initialize_testproj_db development.ini

Cookiecutter 实用程序还生成 test 包中的测试套件。它们基于 PyTest 包。继续并查看测试是否通过。

The Cookiecutter utility also generates the test suite in the tests package. They are based on PyTest package. Go ahead and see if the tests pass.

Pytest
================ test session starts ======================
platform win32 -- Python 3.10.1, pytest-7.1.2, pluggy-1.0.0
rootdir: F:\pyram-env\testproj, configfile: pytest.ini, testpaths: testproj, tests
plugins: cov-3.0.0
collected 5 items

tests\test_functional.py .. [ 40%]
tests\test_views.py ... [100%]
=============== 5 passed, 20 warnings in 6.66s ===============

Cookiecutter 使用 Waitress 服务器。Pyramid 应用程序在 localhost 的端口 6543 上通过以下命令提供服务 −

Cookiecutter uses the Waitress server. The Pyramid application is served on localhost’s port 6543 by following command −

pserve development.ini
Starting server in PID 67700.
2022-06-19 23:43:51,308 INFO [waitress:485][MainThread] Serving on http://[::1]:6543
2022-06-19 23:43:51,308 INFO [waitress:485][MainThread] Serving on http://127.0.0.1:6543

打开浏览器并访问其中的 http://localhost:6543/ 。新创建的项目的主页将显示如下 −

Open the browser and visit http://localhost:6543/ in it. The homepage of the newly created project will be displayed as follows −

cookiecutter

Debug Toolbar

您可以在主页的右上角找到一个小一些的 Pyramid 标志。单击它以打开一个新选项卡和一个调试工具栏,该工具栏提供有关项目的大量有用信息。

You can find a smaller Pyramid logo at the top right of the homepage. Click on it to open a new tab and a debug toolbar that provides lots of useful information about the project.

例如,历史记录标题下的 SQLAlchemy 选项卡显示 SQLAlchemy 查询,显示从 development.ini 中的默认数据创建的模型的结构。

For example, the SQLAlchemy tab under the history heading shows the SQLAlchemy queries showing the structure of the model created from the default data in development.ini.

pyramid  logo

全局标题再次显示诸如 Introspection、Routes 等选项卡,如下所示。单击“路由”选项卡以查看应用程序配置中定义的路由及其匹配模式。

The Global heading again shows tabs such as Introspection, Routes, etc. as shown below. Click the "Routes" tab to see the routes and their matching patterns defined in the application’s configuration.

debug toolbar