前奏曲:快速项目启动与 Cookiecutters

为了简化项目的启动过程,塔架项目提供了一个 cookiecutter 产生样本的 Pyramid 来自项目模板的项目。将安装Cookiecutter Pyramid 以及它的依赖性。我们将继续讨论使用 Pyramid 但是很高兴知道这个设施。这首序曲将演示如何进行工作 Pyramid 通过运行Web应用程序 cookiecutter .

目标

  • 使用CookieCutter创建新项目。

  • 启动一个 Pyramid 应用程序并在Web浏览器中访问它。

步骤

  1. 将cookiecutter安装到虚拟环境中。

    $VENV/bin/pip install cookiecutter
    
  2. 让我们用cookiecutter pyramid-cookiecutter-starter 创建一个启动器 Pyramid 在当前目录中,按如下所示的提示为以下命令输入值。

    $VENV/bin/cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout main
    

    如果提示输入第一项,则默认返回“yes”

    You've cloned ~/.cookiecutters/pyramid-cookiecutter-starter before.
    Is it okay to delete and re-clone it? [yes]: yes
    project_name [Pyramid Scaffold]: cc_starter
    repo_name [cc_starter]: cc_starter
    Select template_language:
    1 - jinja2
    2 - chameleon
    3 - mako
    Choose from 1, 2, 3 [1]: 1
    Select backend:
    1 - none
    2 - sqlalchemy
    3 - zodb
    Choose from 1, 2, 3 [1]: 1
    
  3. 然后我们运行以下命令。

    # Change directory into your newly created project.
    cd cc_starter
    # Create a new virtual environment...
    python3 -m venv env
    # ...where we upgrade packaging tools...
    env/bin/pip install --upgrade pip setuptools
    # ...and into which we install our project.
    env/bin/pip install -e .
    
  4. 通过指向启动应用程序 Pyramidpserve 项目(生成)配置文件中的命令:

    env/bin/pserve development.ini --reload
    

    启动时, pserve 记录一些输出:

    Starting monitor for PID 60461.
    Starting server in PID 60461.
    Serving on http://localhost:6543
    Serving on http://localhost:6543
    
  5. 在浏览器中打开http://localhost:6543/。

分析

而不是从头开始,一个cookiecutter可以使包含工作的python项目变得容易 Pyramid 应用。

pservePyramid 的应用程序运行程序,将操作细节与代码分开。安装时 Pyramid 一个叫做 pserve 是写给你的 bin 目录。这个程序是一个可执行的python模块。它被传递一个配置文件(在本例中, development.ini