Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pyramid tutorial


Published on

Published in: Technology
  • Be the first to comment

Pyramid tutorial

  1. 2. Welcome to the Pyramid Tutorial Thanks for being here. Today we'll: <ul><ul><li>Quickly go over some administrative stuff.
  2. 3. Briefly introduce the tutorial.
  3. 4. Learn about Pyramid.
  4. 5. Write a simple application.
  5. 6. Improve our application in stages.
  6. 7. Get answers to some Pyramid questions.
  7. 8. Hopefully, have some fun. </li></ul></ul>
  8. 9. Administrative notes <ul><li>Were you able to install Pyramid?
  9. 10. If not, grab a CD, install and pass it on.
  10. 11. Anyway, all the code is at:
  11. 12. Tutorial survey.
  12. 13. Pyramid book winners.
  13. 14. Tutorial structure. </li></ul>
  14. 15. Tutorial Outline <ul><li>The goal is to write a simple but complete Pyramid application and leave you with a base to start your own projects.
  15. 16. We'll have four stages, where we'll first learn a few Pyramid concepts, then start looking at the code for that stage.
  16. 17. Throughout the tutorial, I will answer any questions that may come up. </li></ul>
  17. 18. Who is the Teacher? <ul><li>My name is Carlos de la Guardia.
  18. 19. I live in Mexico, but work for Boston based web development consulting firm Jazkarta.
  19. 20. I've been doing web stuff since 1994.
  20. 21. I'm a Pyramid contributor since the days of repoze.bfg (that means 2008 or so).
  21. 22. 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. </li></ul>
  22. 23. What you bring to the table <ul><li>Familiarity with web development. General knowledge of how a web application works, some exposure to HTML, CSS and Javascript.
  23. 24. 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.
  24. 25. Real interest in writing a real web application sometime. </li></ul>
  25. 26. The Pylons Project <ul><li>The aim of the project is to develop a related set of web technologies
  26. 27. It's an umbrella name, similar to how the Apache name is used today
  27. 28. Pylons 1 is a stable and maintained web framework, but no further enhancements to it will be done.
  28. 29. Pyramid is the current and future embodiment of Pylons-Style web development
  29. 30. There will be other projects under this umbrella, like TurboGears. </li></ul>
  30. 31. What is Pyramid? A Python web application development framework that features: <ul><li>Simplicity
  31. 32. Minimalism
  32. 33. Documentation
  33. 34. Openness
  34. 35. Speed
  35. 36. Reliability </li></ul>Pyramid is primarily inspired by Django, Zope and Pylons.
  36. 37. Where is Pyramid and where is it going? <ul><li>Version 1.1 was released just days ago.
  37. 38. New features include better support for scripting, caching and testing tools.
  38. 39. Supports Python versions 2.5 to 2.7.
  39. 40. Python 3 support is being worked on.
  40. 41. We keep adding new documentation in the form of how-to's and tutorials.
  41. 42. Many people have begun contributing Pyramid add-ons and applications. </li></ul>
  42. 43. Is Pyramid for me? <ul><li>Like all web frameworks, Pyramid is better suited for some uses cases than others: </li><ul><li>If you want more flexibility than 'rails'.
  43. 44. If you like to have control of all the parts of your application.
  44. 45. If you need a truly pluggable framework.
  45. 46. if you enjoy 'doing it yourself' when you need something special.
  46. 47. Certainly it's a great option if you want to use Python. </li></ul></ul>
  47. 48. Stage 0 – Installing Pyramid <ul><li>Pyramid runs on Linux, Mac OSX, Windows, PyPy, Google App engine.
  48. 49. The installation procedure is very similar in the three major OS versions.
  49. 50. Pyramid best practice suggests the use of virtualenv for development.
  50. 51. 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 </li></ul>
  51. 52. Pyramid starting points Pyramid includes several 'scaffolds' that help you get started quickly: <ul><li>pyramid_starter
  52. 53. pyramid_zodb
  53. 54. pyramid_routesalchemy
  54. 55. pyramid_alchemy </li></ul>In addition there are some third party scaffolds available. In particular Akhet might be useful for Pylons legacy users.
  55. 56. More about the scaffolds <ul><li>They are independent, but share a common structure.
  56. 57. It's a good idea to try to keep that structure when developing using a scaffold.
  57. 58. They include development and production configurations.
  58. 59. They all use paster as a WSGI server. </li></ul>
  59. 60. Typical Pyramid project <ul><li>Many of us started with Zope years ago and thus use the features that are geared towards that kind of development
  60. 61. Most new users will not start there.
  61. 62. A typical project might use routes and a relational database. We recommend but do not enforce using SQLAlchemy.
  62. 63. 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. </li></ul>
  63. 64. The routesalchemy scaffold <ul><li>Uses SQLAlchemy
  64. 65. Uses a transaction manager
  65. 66. Uses routes (URL dispatch)
  66. 67. Uses ZPT
  67. 68. Let's take a look at the code. </li></ul>
  68. 69. What Pyramid does <ul><li>Pyramid has a main method which is run by the calling WSGI infrastructure. In this case Paster.
  69. 70. This method creates a configuration object to hold Pyramid's routing and view configuration.
  70. 71. Then a WSGI application is created, which is a router that uses the configuration to decide how to handle request.
  71. 72. Pyramid also offers sessions, authorization, authentication and templating. </li></ul>
  72. 73. URL Dispatch <ul><li>A route consists basically of a name and a pattern, with some possible additional predicates.
  73. 74. 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.
  74. 75. The pattern syntax allows URL parameters, which are passed on a matchdict variable when a route matches.
  75. 76. Order is important when declaring routes. </li></ul>
  76. 77. Views <ul><li>A view is a callable, be it a function or class, that accepts a requests and returns a response.
  77. 78. Views are registered at application start up.
  78. 79. A particular view can be associated with a route and is called when the route matches and passed any parameters that it has.
  79. 80. Views can be registered also for exceptions, allowing us to customize responses. </li></ul>
  80. 81. View configuration parameters <ul><li>Name
  81. 82. Context
  82. 83. route_name
  83. 84. Request type
  84. 85. Request method
  85. 86. Request parameter </li></ul><ul><li>Container
  86. 87. Xhr
  87. 88. Accept
  88. 89. Header
  89. 90. Path info
  90. 91. custom </li></ul>
  91. 92. Renderers <ul><li>Pyramid offers renderers that allow the user to automatically return one type of response without having to construct it.
  92. 93. There are renderers for: </li><ul><li>String
  93. 94. Json
  94. 95. Jsonp
  95. 96. Templates </li></ul><li>To use them just configure one and return a dict with the parameters to pass to it.
  96. 97. It's easy to create custom renderers if needed.
  97. 98. You can still alter the response by using request.response to set headers. </li></ul>
  98. 99. Static views <ul><li>A static view allows the application to access static resources.
  99. 100. A static resource is any file that's not Python code.
  100. 101. The name of the static view represents a URL path to get the resources.
  101. 102. Static resources can be overridden from other packages. </li></ul>
  102. 103. Let's look at more code <ul><li>Our application is a twitter clone.
  103. 104. At this point it has a single route.
  104. 105. It has two views which use the same route.
  105. 106. The view predicates are the difference.
  106. 107. Note the use of renderers.
  107. 108. We also have a static view. </li></ul>
  108. 109. Security <ul><li>Pyramid provides a declarative authorization system that allows us to set permissions to views.
  109. 110. It also provides a simple authentication policy to handle custom logins.
  110. 111. The next stage of our sample application adds such a system to our Birdie app. </li></ul>
  111. 112. Declarative configuration <ul><li>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.
  112. 113. Pyramid also offers another configuration mode which allows us to have the view defined right beside the code it uses.
  113. 114. For larger systems, this might be a better strategy. </li></ul>
  114. 115. keeping configuration under control <ul><li>Get a list of all defined routes: bin/paster proutes development.ini
  115. 116. Get a list of probable views for a given URL: bin/paster pviews development.ini /join
  116. 117. Get a shell with the Pyramid configuration: bin/paster pshell development.ini </li></ul>
  117. 118. Our sample app is done <ul><li>If we take a look at the code, the mode of configuration is now different.
  118. 119. All the views defined one after the other is known as imperative configuration.
  119. 120. The other system is known as declarative configuration.
  120. 121. 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. </li></ul>
  121. 122. There's more <ul><li>But there's no time.
  122. 123. Some important topics we didn't cover: </li><ul><li>Traversal
  123. 124. Events
  124. 125. Sessions
  125. 126. Scripting
  126. 127. Hooks
  127. 128. Deployment </li></ul></ul>
  128. 129. Traversal <ul><li>Traversal is a resource finding mechanism that is very useful when you have 'contentish' objects.
  129. 130. It's conceptually similar to navigating a filesystem tree.
  130. 131. It's also very useful when you need row level security (different permissions per content object).
  131. 132. Instead of looking views using routes, we traverse to a content object and then pick one of its views. </li></ul>
  132. 133. Events <ul><li>Pyramid allows us to subscribe to important events in the application lifecycle.
  133. 134. Most importantly, we can be notified when the request is created and when the response is prepared.
  134. 135. We can have our callbacks execute before these events. </li></ul>
  135. 136. Really ran out of time <ul><li>Everything is documented, so you can go and check the docs today.
  136. 137. We also have one of the most friendly live support channels on IRC. Please visit.
  137. 138. More information at: </li></ul>
  138. 139. Thanks a lot <ul><li>Please fill the survey at: </li></ul>Http:// <ul><li>I'm [email_address]
  139. 140. Find us in IRC, channel #pyramid </li></ul>