Part 1 - Preface and content.
Scenario
Pyramid is excellent at building web applications in several ways. As well as Pylons, before its appearance, Pyramid can build traditional route-oriented (routes-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 Plone tutorial, we chose a presentation script that shows the strengths of Pyramid in content-oriented applications. More specifically, a hierarchical project management system, where you can set tasks for teams.
')
As we study this tutorial, we will create 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 installing additional packages.
Note on the text: Windows users need to adapt UNIX isms to fit your desktop 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 it in the working directory
- $ 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 manual 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 some examples, systematically grow into a whole application.
Sample files are available for those who do not want to enter the code as part of the educational process. (Note: there are no them on
that page )
Creating a simple UX for Pyramid
In this series of steps, we 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 development
- Templates with Chameleon and ZPT
- Use of macros and “layouts” for template productivity
- Static resources
- AJAX via JSON renderer (renderer)
Workflow
Many projects have an invariable workflow that starts with the 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 complete.
This tutorial models this workflow. In this first part, we show how a person, without deep knowledge of the 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. No packages, imports, setup.py, and the like.
Goals
- Get Pyramid pixels on screen as easy as possible.
- Use this as a well-understood base for further complication.
Technical requirements
- Create a module with a view so that it acts as an HTTP server
- Visit the URL in your browser
Prerequisites
Micro-frameworks are a universal hobby of these days. They give a low load when executed. And neither do they load the brain: they do so little that you only have to think about the task.
Pyramid is special because it can act as a separate microframe 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 go further:
$ mkdir creatingux; cd creatingux
$ mkdir step01; cd step01
Copy the text below to the newly created file step01 / application.py:
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:8080
in the browser, we look, we rejoice.
Additional questions
- What happens if you return a string of HTML? The 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 in spirit similar to microform frameworks. View function added to configuration. When called, the view returns an answer.
Explanations
The prerequisites for mega-frameworks, microframes and Pyramid are based on this.
Part 3: