Guide: Pyramid for People - Part 2

    Part 1 - preface and content.

    Scenario


    Pyramid is excellent in building web applications in several ways. Like Pylons, before its introduction, Pyramid can build traditional route-oriented RDBMS applications using SQLAlchemy. Unlike other RDBMS-based web frameworks, Pyramid is also very well equipped to easily build content-oriented applications familiar from the world of Plone and Zope.

    Since this is a tutorial for the Plone conference, we chose a presentation scenario that shows the strengths of Pyramid in content-oriented applications. More precisely, a hierarchical project management system where you can set tasks for teams.

    As we study this tutorial, we will be creating an application called Projector.

    How to install


    This tutorial assumes that you already have Python 2.6 and above installed, you have an Internet connection, and a text editor of your choice. This is enough to get you started with the first step. Other steps may require the installation of additional packages.

    Text note: Windows users need to adapt UNIX isms to suit your work environment.

    Our workspace will look like this:

    someworkingdirectory /
      tutorial_workspace /
        venv ### Target for our virtualenv
        creatingux /
            step01
            step02
            etc.
        resources
          step01
          step02
        etc.
      etc.

    Steps

    Open the shell and go into the working directory in it

    • $ mkdir tutorial_workspace; cd tutorial_workspace
    • $ virtualenv --no-site-packages venv
    • $ export PATH = / path / to / tutorial_workspace / venv / bin: $ PATH
    • $ which easy_install
    should output something like:
    /home/ks/projects/tutorial_workspace/venv/bin/easy_install

    • $ easy_install pyramid WebTest nose
    • $ export PYRAMID_RELOAD_TEMPLATES = 1
    Code examples

    Each step in this guide requires the reader to enter code examples and thus create a working application. The directories in these steps are not Python packages, but be that as it may, the simple directory is full of Python modules. In addition, they are fully working, in separate examples, systematically grow into an entire application.

    Sample files are available for those who do not want to enter the code as part of the learning process. (Note: there are none on that page )

    Creating Simple UX for Pyramid


    In this series of steps, we will build the essence of user experience (UX), with substitutes for logic and data.

    Goals
    • The simplest possible demonstration of Pyramid
    • Using unit tests and WebTest to speed up development
    • Patterning with Chameleon and ZPT
    • The use of macros and “schemes” (layouts), for the productivity of templates
    • Static Resources
    • AJAX via JSON renderer
    Work flow

    Many projects have a consistent workflow that starts with a client who usually wants to see something in order to understand and approve. Usually the “UI person” in the team works at this stage, getting a working prototype in place, and then the logic is considered full.

    This tutorial simulates such a workflow. In this first part, we will show how a person, without a deep knowledge of Pyramid architecture, can create working prototypes of various kinds in Projector.

    Step 01: Hello World in Pyramid

    What is the easiest way to start in Pyramid? Separate-file module. Without packages, imports, setup.py and other such.

    Goals
    • Getting Pyramid pixels on screen is as simple as possible.
    • Use this as a well-understood base to further complicate
    Technical requirements
    • Create a module with a view so that it acts as an HTTP server
    • Visit URL in your browser
    Background

    Microframes are the universal fascination of these days. They give a low load at runtime. And also they do not load the brain: they do so little that you have to think only about the task.

    Pyramid is special because it can act as a separate microframework file. You have a simple Python file that can be executed directly through Python. But Pyramid also scales to huge applications.

    Steps

    Make sure you do everything as stated here .
    And let's move on: Copy the text below into the newly created file step01 / application.py:
    $ mkdir creatingux; cd creatingux
    $ mkdir step01; cd step01


    from  wsgiref.simple_server  import  make_server

    from  pyramid.config  import  Configurator
    from  pyramid.response  import  Response

    # This acts as the view function
    def  hello_world (request):
        return  Response ('hello!')

    def  main ():
        # Grab the config, add a view, and make a WSGI app
        config = Configurator ()
        config.add_view (hello_world)
        app = config.make_wsgi_app ()
        return  app

    if  __name__ == '__main__':
        # When run from command line, launch a WSGI server and app
        app = main ()
        server = make_server ('0.0.0.0', 8080, app)
        server.serve_forever ()
    $ python application.py
    We open 127.0.0.1:8080in the browser, look, rejoice.

    Additional questions
    • What happens if you return the string of HTML? Sequence of integers?
    • Put something wrong, such as print xyz, in the view function. Kill your python application and restart, then refresh your browser. What is the exception in the console?
    • Does Pyramid support automatic updating of Python code?
    Analysis

    This simple module does very little for a few lines of code, so creating a web application is similar in spirit to microframes. A view function has been added to the configuration. When called, the view returns a response.

    Explanations

    Prerequisites for megaframes, microframes, and Pyramid are based on this.

    Part 3:

    Also popular now: