pyramid tutorial

Download Pyramid tutorial

If you can't read please download the document

Upload: carlos-de-la-guardia

Post on 16-Apr-2017

5.839 views

Category:

Technology


2 download

TRANSCRIPT

Welcome to the Pyramid Tutorial

Thanks for being here. Today we'll:Quickly go over some administrative stuff.

Briefly introduce the tutorial.

Learn about Pyramid.

Write a simple application.

Improve our application in stages.

Get answers to some Pyramid questions.

Hopefully, have some fun.

Administrative notes

Were you able to install Pyramid?

If not, grab a CD, install and pass it on.

Anyway, all the code is at:

https://github.com/cguardia/Pyramid-Tutorial

Tutorial survey.

Pyramid book winners.

Tutorial structure.

Tutorial Outline

The goal is to write a simple but complete Pyramid application and leave you with a base to start your own projects.

We'll have four stages, where we'll first learn a few Pyramid concepts, then start looking at the code for that stage.

Throughout the tutorial, I will answer any questions that may come up.

Who is the Teacher?

My name is Carlos de la Guardia.

I live in Mexico, but work for Boston based web development consulting firm Jazkarta.

I've been doing web stuff since 1994.

I'm a Pyramid contributor since the days of repoze.bfg (that means 2008 or so).

I love Pyramid so much that I was willing to leave the side of my lovely three children and beautiful wife for a couple of days just to teach you about it.

What you bring to the table

Familiarity with web development. General knowledge of how a web application works, some exposure to HTML, CSS and Javascript.

At least some familiarity with Python. We'll not do anything complex here, but you should be able to look at some code and have some idea what's going on.

Real interest in writing a real web application sometime.

The Pylons Project

The aim of the project is to develop a related set of web technologies

It's an umbrella name, similar to how the Apache name is used today

Pylons 1 is a stable and maintained web framework, but no further enhancements to it will be done.

Pyramid is the current and future embodiment of Pylons-Style web development

There will be other projects under this umbrella, like TurboGears.

What is Pyramid?

A Python web application development framework that features:Simplicity

Minimalism

Documentation

Openness

Speed

Reliability

Pyramid is primarily inspired by Django, Zope and Pylons.

Where is Pyramid and where is it going?

Version 1.1 was released just days ago.

New features include better support for scripting, caching and testing tools.

Supports Python versions 2.5 to 2.7.

Python 3 support is being worked on.

We keep adding new documentation in the form of how-to's and tutorials.

Many people have begun contributing Pyramid add-ons and applications.

Is Pyramid for me?

Like all web frameworks, Pyramid is better suited for some uses cases than others:If you want more flexibility than 'rails'.

If you like to have control of all the parts of your application.

If you need a truly pluggable framework.

if you enjoy 'doing it yourself' when you need something special.

Certainly it's a great option if you want to use Python.

Stage 0 Installing Pyramid

Pyramid runs on Linux, Mac OSX, Windows, PyPy, Google App engine.

The installation procedure is very similar in the three major OS versions.

Pyramid best practice suggests the use of virtualenv for development.

A real development environment is required, which in linux meand C compiler and in Mac OSX means Xcode. On Windows you will require the Python for Windows Extensions

Pyramid starting points

Pyramid includes several 'scaffolds' that help you get started quickly:pyramid_starter

pyramid_zodb

pyramid_routesalchemy

pyramid_alchemy

In addition there are some third party scaffolds available. In particular Akhet might be useful for Pylons legacy users.

More about the scaffolds

They are independent, but share a common structure.

It's a good idea to try to keep that structure when developing using a scaffold.

They include development and production configurations.

They all use paster as a WSGI server.

Typical Pyramid project

Many of us started with Zope years ago and thus use the features that are geared towards that kind of development

Most new users will not start there.

A typical project might use routes and a relational database. We recommend but do not enforce using SQLAlchemy.

There are two templating languages. Most documentation uses ZPT, but you can use Make out of the box as well. There are also third paty bindings.

The routesalchemy scaffold

Uses SQLAlchemy

Uses a transaction manager

Uses routes (URL dispatch)

Uses ZPT

Let's take a look at the code.

What Pyramid does

Pyramid has a main method which is run by the calling WSGI infrastructure. In this case Paster.

This method creates a configuration object to hold Pyramid's routing and view configuration.

Then a WSGI application is created, which is a router that uses the configuration to decide how to handle request.

Pyramid also offers sessions, authorization, authentication and templating.

URL Dispatch

A route consists basically of a name and a pattern, with some possible additional predicates.

When the user visits our application in a browser, the PATH_INFO part of the URL is used to decide which of the application's routes apply.

The pattern syntax allows URL parameters, which are passed on a matchdict variable when a route matches.

Order is important when declaring routes.

Views

A view is a callable, be it a function or class, that accepts a requests and returns a response.

Views are registered at application start up.

A particular view can be associated with a route and is called when the route matches and passed any parameters that it has.

Views can be registered also for exceptions, allowing us to customize responses.

View configuration parameters

Name

Context

route_name

Request type

Request method

Request parameter

Container

Xhr

Accept

Header

Path info

custom

Renderers

Pyramid offers renderers that allow the user to automatically return one type of response without having to construct it.

There are renderers for:String

Json

Jsonp

Templates

To use them just configure one and return a dict with the parameters to pass to it.

It's easy to create custom renderers if needed.

You can still alter the response by using request.response to set headers.

Static views

A static view allows the application to access static resources.

A static resource is any file that's not Python code.

The name of the static view represents a URL path to get the resources.

Static resources can be overridden from other packages.

Let's look at more code

Our application is a twitter clone.

At this point it has a single route.

It has two views which use the same route.

The view predicates are the difference.

Note the use of renderers.

We also have a static view.

Security

Pyramid provides a declarative authorization system that allows us to set permissions to views.

It also provides a simple authentication policy to handle custom logins.

The next stage of our sample application adds such a system to our Birdie app.

Declarative configuration

The configuration for the views up to stage 2 of the sample app is done using a style of configuration that requires all views to be defined one after the other.

Pyramid also offers another configuration mode which allows us to have the view defined right beside the code it uses.

For larger systems, this might be a better strategy.

keeping configuration under control

Get a list of all defined routes:
bin/paster proutes development.ini

Get a list of probable views for a given URL:
bin/paster pviews development.ini /join

Get a shell with the Pyramid configuration:
bin/paster pshell development.ini

Our sample app is done

If we take a look at the code, the mode of configuration is now different.

All the views defined one after the other is known as imperative configuration.

The other system is known as declarative configuration.

Note that the decorators that Pyramid uses for views do not have side effects at import time, so it's necessary to use the scan function.

There's more

But there's no time.

Some important topics we didn't cover:Traversal

Events

Sessions

Scripting

Hooks

Deployment

Traversal

Traversal is a resource finding mechanism that is very useful when you have 'contentish' objects.

It's conceptually similar to navigating a filesystem tree.

It's also very useful when you need row level security (different permissions per content object).

Instead of looking views using routes, we traverse to a content object and then pick one of its views.

Events

Pyramid allows us to subscribe to important events in the application lifecycle.

Most importantly, we can be notified when the request is created and when the response is prepared.

We can have our callbacks execute before these events.

Really ran out of time

Everything is documented, so you can go and check the docs today.

We also have one of the most friendly live support channels on IRC. Please visit.

More information at:

http://www.pylonsproject.org

Thanks a lot

Please fill the survey at:


Http://pyramid.delaguardia.mx

I'm [email protected]

Find us in IRC, channel #pyramid

Click to edit the title text format