Python 3 Web Development Beginner's Guide
Use Python to create, theme, and deploy unique web applications
Michel Anders
BIRMINGHAM - MUMBAI
Python 3 Web Development Beginner's Guide
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2011
Production Reference: 1060511
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849513-74-6 www.packtpub.com
Cover Image by Rakesh Shejwal (
[email protected])
Credits Author Michel Anders Reviewers Michael Driscoll Róman Joost Tomi Juhola Andrew Nicholson
Project Coordinators Poorvi Nair Michelle Quadros Proofreader Mario Cecere Indexer Tejal Daruwale
Herjend Teny Graphics Acquisition Editor
Nilesh Mohite
Sarah Cullington Production Coordinator Development Editor
Kruthika Bangera
Neha Mallik Cover Work Technical Editors Sakina Kaydawala Gauri Iyer Copy Editor Leonard D'Silva
Kruthika Bangera
About the Author Michel Anders, after his chemistry and physics studies where he spent more time on computer simulations than on real world experiments, the author found his real interests lay with IT and Internet technology, and worked as an IT manager for several different companies, including an Internet provider, a hospital, and a software development company. After his initial exposure to Python as the built-in scripting language of Blender, the popular 3D modeling and rendering suite, the language became his tool of choice for many projects. He lives happily in a small converted farm, with his partner, three cats, and twelve goats. This tranquil environment proved to be ideally suited to writing his first book, Blender 2.49 Scripting (Packt Publishing, 978-1-849510-40-0). He loves to help people with Blender and Python-related questions and may be contacted as 'varkenvarken' at http://www.blenderartists.org/ and maintains a blog on Pythonspecific subjects at http://michelanders.blogspot.com/. For Clementine, always.
About the Reviewers Michael Driscoll has been programming Python since the Spring of 2006 and has dabbled in other languages since the late nineties. He graduated from the University with a Bachelors of Science degree, majoring in Management Information Systems. Michael enjoys programming for fun and profit. His hobbies include Biblical apologetics, blogging about Python at http://www.blog.pythonlibrary.org/, and learning photography. Michael currently works for the local government, where he does programming with Python as much as possible. Michael was also a Technical Reviewer for Python 3: Object Oriented Programming by Dusty Phillips and Python Graphics Cookbook by Mike Ohlson de Fine (both by Packt Publishing). I would like to thank my friends and family for their support and the fun times they share with me. Most of all, I want to thank Jesus for saving me from myself.
Róman Joost discovered open source software in 1997. He is the project manager for user documentation for GNU Image Manipulation Program (GIMP). Róman also helped with German internationalization of GIMP. He has been contributing to GIMP and Zope open source projects for eight years. Róman has a Diplom-Informatiker (FH) from the University of Applied Sciences in Koethen (Anhalt). He has worked for Zope companies—Gocept GmbH & Co in Germany, Infrae in The Netherlands, and is currently working for a Zope company in Brisbane, Australia. For relaxation, he enjoys photography and digital painting with GIMP.
Tomi Juhola is a software development professional from Finland. He has a wide range of development experience from embedded systems to modern distributed enterprise systems in various roles such as tester, developer, consultant, and trainer. Currently, he works in a financial company and shares this time between development lead duties and helping other projects to adopt Scrum and Agile methodologies. He likes to spend his free time with new interesting development languages and frameworks. He has reviewed conference proposals, a Python development book, and has also published his own Master's theses on Agile embedded development.
Andrew Nicholson is a computer engineer with over fourteen years of professional experience in a broad range of computing technologies. He is currently a Technical Director with Infinite Recursion Pty Ltd.—a bespoke software engineering company located in Sydney, Australia. He is a passionate advocate and a participant in the free, libre, and open source software (FLOSS) community and has actively participated since 1999 contributing code, ideas, and energy in this engineering community. He was a Technical Reviewer for the book Python Testing: Beginner's Guide (2010), Packt Publishing. Nicholson has a B.Eng (Computer) [Honours 1] from Newcastle University, Australia and a M.Eng (Wireless) with Merit from Sydney University, Australia. Nicholson's biography can be read at http://www.infiniterecursion.com.au/ people/.
www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Table of Contents Preface Chapter 1: Choosing Your Tools Identifying the components of a web application Time for action – getting an overview of a web application Choosing suitable tools Time for action – choosing a delivery framework, also known as web server Time for action – choosing a server-side scripting language Time for action – choosing a database engine Time for action – deciding on object relational mappers Time for action – choosing a presentation framework Designing for maintainability and usability Testing Time for action – choosing a test framework Version management Usability Good looking – adhering to common GUI paradigms Themable Cross-browser compatible Cross-platform compatible
Maintainability Standards compliant
Security Reliable Robust Access control and authentication Confidentiality Integrity A final word on security
Help, I am confused! Time for action – maintaining overview Summary
1 7 7 8 10 11 12 14 15 17 18 18 19 19 20 20 21 21 22
22 22
23 23 23 24 24 25 25
25 26 28
Table of Contents
Chapter 2: Creating a Simple Spreadsheet Python 3 Time for action – installing Python 3 CherryPy Time for action – installing CherryPy Installing jQuery and jQuery UI Serving an application Time for action – serving a dummy application Time for action – serving HTML as dynamic content Who serves what: an overview HTML: separating form and content Time for action – a unit convertor HTML: form-based interaction JavaScript: using jQuery UI widgets Time for action – conversion using unitconverter.js jQuery selectors CSS: applying a jQuery UI theme to other elements Time for action – converting a unit convertor into a plugin JavaScript: creating a jQuery UI plugin Designing a spreadsheet application Time for action – serving a spreadsheet application HTML: keeping it simple JavaScript: creating a spreadsheet plugin The missing parts Summary
Chapter 3: Tasklist I: Persistence
29 30 30 31 31 32 33 34 36 37 38 39 40 40 42 43 45 46 51 51 52 52 58 58
59
Designing a tasklist application Time for action – creating a logon screen Serving a logon screen Setting up a session Expiring a session Designing a task list Time for action – running tasklist.py Python: the task module Time for action – implementing the task module Adding new tasks Deleting a task JavaScript: tasklist.js Time for action – styling the buttons JavaScript: tooltip.js Time for action – implementing inline labels [ ii ]
59 62 69 70 71 72 72 75 76 80 81 83 83 85 86
Table of Contents
CSS: tasklist.css Summary
87 90
Chapter 4: Tasklist II: Databases and AJAX The advantages of a database compared to a filesystem Choosing a database engine Database-driven authentication Time for action – authentication using a database Tasklist II – storing tasks in a database Improving interactivity with AJAX
91 92 92 93 94 99 99
Time for action – getting the time with AJAX Redesigning the Tasklist application Database design Time for action – creating the task database Time for action – retrieving information with select statements TaskDB – interfacing with the database Time for action – connecting to the database Time for action – storing and retrieving information Time for action – updating and deleting information Testing Time for action – testing factorial.py Now what have we gained? Time for action – writing unit tests for tasklistdb.py Designing for AJAX Click handlers
100 102 103 103 105 106 106 107 109 111 112 113 114 116 120
The application Time for action – putting it all together Have a go hero – refreshing the itemlist on a regular basis Summary
Chapter 5: Entities and Relations
121 123 125 126
127
Designing a book database The Entity class Time for action – using the Entity class Time for action – creating instances The Relation class Time for action – using the Relation class Relation instances Time for action – defining the Books database The delivery layer Time for action – designing the delivery layer Time for action – adding a new book [ iii ]
127 128 129 132 138 138 141 144 150 151 162
Table of Contents
Auto completion Time for action – using input fields with auto completion The presentation layer Time for action – using an enhanced presentation layer Summary
Chapter 6: Building a Wiki
165 166 168 168 170
171
The data layer Time for action – designing the wiki data model The delivery layer Time for action – implementing the opening screen The structural components The application methods Time for action – implementing a wiki topic screen Time for action – editing wiki topics Additional functionality Time for action – selecting an image Time for action – implementing a tag cloud Time for action – searching for words The importance of input validation Time for action – scrubbing your content Time for action – rendering content Summary
Chapter 7: Refactoring Code for Reuse Time for action – taking a critical look Refactoring Time for action – defining new entities: how it should look Metaclasses Time for action – using metaclasses MetaEntity and AbstractEntity classes Time for action – implementing the MetaEntity and AbstractEntity classes Relations Time for action – defining new relations: how it should look Implementing the MetaRelation and AbstractRelation classes Adding new methods to existing classes
172 172 175 176 177 179 180 182 185 185 190 192 195 196 200 201
203 203 205 205 206 207 208 209 217 217 219 222
Browsing lists of entities Time for action – using a table-based Entity browser Time for action – examining the HTML markup Caching The books application revisited Time for action – creating a books application, take two Summary [ iv ]
224 224 229 232 236 236 242
Table of Contents
Chapter 8: Managing Customer Relations A critical review Designing a Customer Relationship Management application Time for action – implementing a basic CRM Adding and editing values Time for action – adding an instance Time for action – editing an instance Adding relations Picklists Time for action – implementing picklists Summary
Chapter 9: Creating Full-Fledged Webapps: Implementing Instances Even more relations Time for action – showing one-to-many relationships Time for action – adapting MetaRelation Time for action – enhancing Display Time for action – enhancing Browse Access control Time for action – implementing access control Role-based access control Time for action – implementing role-based access control Summary
Chapter 10: Customizing the CRM Application Time for action – sorting Time for action – filtering Customization Time for action – customizing entity displays Time for action – customizing entity lists Time for action – adding a delete button Summary
243 243 244 244 248 249 251 257 259 259 262
263 263 264 266 270 271 274 275 278 279 283
285 285 290 292 292 298 301 302
Appendix A: References to Resources
303
Good old offline reference books Additional websites, wikis, and blogs
303 304
Appendix B: Pop Quiz Answers
307
Chapter 2, Creating a Simple Spreadsheet Chapter 3, Tasklist I: Persistence Chapter 4, Tasklist II: Databases and AJAX Chapter 5, Entities and Relations Chapter 6, Building a Wiki
Index
307 308 309 310 310
311 [v]
Preface Building your own Python web applications provides you with the opportunity to have great functionality, with no restrictions. However, creating web applications with Python is not straightforward. Coupled with learning a new skill of developing web applications, you would normally have to learn how to work with a framework as well. Python 3 Web Development Beginner's Guide shows you how to independently build your own web application that is easy to use, performs smoothly, and is themed to your taste—all without having to learn another web framework. Web development can take time and is often fiddly to get right. This book will show you how to design and implement a complex program from start to finish. Each chapter looks at a different type of web application, meaning that you will learn about a wide variety of features and how to add them to your customized web application. You will also learn to implement jQuery into your web application to give it extra functionality. By using the right combination of a wide range of tools, you can have a fully functional, complex web application up and running in no time. A practical guide to building and customizing your own Python web application, without the restriction of a pre-defined framework.
What this book covers
Chapter 1, Choosing Your Tools, looks at the many aspects of designing web applications. The idea is to provide you with an overview that may help you recognize components in subsequent chapters and give you some insight into the arguments used to decide which tool or library to use. We also illustrate some issues that are relevant when designing an application that does not deal with coding directly, such as security or usability. Chapter 2, Creating a Simple Spreadsheet, develops a simple spreadsheet application. The spreadsheet functionality will be entirely implemented in JavaScript plus jQuery UI, but on the server-side, we will encounter the application server, CherryPy, for the first time and we will extend it with Python code to deliver the page that contains the spreadsheet application dynamically.
Preface
Chapter 3, Tasklist I: Persistence, a full fledged web application needs functionality to store information on the server and a way to identify different users. In this chapter, we address both issues as we develop a simple application to maintain lists of tasks. Chapter 4, Tasklist II: Databases and AJAX, refactors the tasklist application developed in the previous chapter. We will use the SQLite database engine on the server to store items and will use jQuery's AJAX functionality to dynamically update the contents of the web application. On the presentation side, we will encounter jQuery UI's event system and will learn how to react on mouse clicks. Chapter 5, Entities and Relations, most real life applications sport more than one entity and often many of these entities are related. Modeling these relations is one of the strong points of a relational database. In this chapter, we will develop a simple framework to manage these entities and use this framework to build an application to maintain lists of books for multiple users. Chapter 6, Building a Wiki, develops a wiki application and in doing so we focus on two important concepts in building web applications. The first one is the design of the data layer. The wiki application is quite complex, and in this chapter, we try to see where the limitations in our simple framework lie. The second one is input validation. Any application that accepts input from all over the Internet should check the data it receives, and in this chapter, we look at both client-side and server-side input validation. Chapter 7, Refactoring Code for Reuse, after doing a substantial bit of work, it is often a good idea to take a step back and look critically at your own work to see if things could have been done better. In this chapter, we look at ways to make the entity framework more generally useful and employ it to implement the books application a second time. Chapter 8, Managing Customer Relations, there is more to an entity framework and CherryPy application code than merely browsing lists. The user must be able to add new instances and edit existing ones. This chapter is the start of the development of a CRM application that will be extended and refined in the final chapters. Chapter 9, Creating Full-Fledged Webapps: Implementing Instances, focuses on the design and implementation of the user interface components to add and maintain entities, and relations between entities, in a way that is independent of the type of entity. This functionality is immediately put to use in the CRM application that we develop. Managing user privileges is another issue we encounter as we explore the concept of role-based access control. Chapter 10, Customizing the CRM Application, is the final chapter and it extends our framework and thereby our CRM application by taking a look at browsing, filtering, and sorting large numbers of entities. We also take a look at what is needed to allow customization by the end user of the application's appearance and its functionality. [2]
Download from Wow! eBook <www.wowebook.com>
Preface
Appendix A, References to Resources, is a convenient overview of both Web and paper resources.
What you need for this book
Besides a computer running Windows or Linux to develop and test your applications, you will need the following pieces of open source software:
Python 3.2
CherryPy 3.2.0
jQuery 1.4.4
jQuery UI 1.8.6
How to obtain and install these packages is explained in detail in Chapter 2. We also use some additional plugins for jQuery and provide installation instructions where appropriate. You will also need a web browser to interact with your applications. The applications were tested on Firefox 3 and Internet Explorer 8, but any moderately recent versions of these browsers will probably work just as well, as will Chrome. The Firebug extension for Firefox, however, is a superior tool to debug the client-side of web applications, so you might want to try it if you have not done so already. Appendix A provides links to the necessary resources. Finally, you will need a text editor, preferably with syntax highlighting capabilities for Python as well as JavaScript and HTML. The author uses Notepad++ (http://notepad-plusplus.org/) on Windows and JOE (http://joe-editor.sourceforge.net/) on Linux, but the choice is entirely up to you.
Who this book is for
Moderately experienced Python programmers who want to learn how to create fairly complex, database-driven, cross browser compatible web applications that are maintainable and look good, will find this book of most use. All applications in the book are developed in Python 3, but experience with Python 2.x is sufficient to understand all examples. JavaScript plays an important supporting role in many of the example applications and some introductory level knowledge of JavaScript might be useful, but is not strictly necessary because the focus is mainly on Python development and the JavaScript code is used either as building blocks or explained in such detail that anyone comfortable with Python should be able to understand it.
[3]
Preface
Conventions
In this book, you will find several headings appearing frequently. To give clear instructions of how to complete a procedure or task, we use:
Time for action – heading 1.
Action 1
2.
Action 2
3.
Action 3
Instructions often need some extra explanation so that they make sense, so they are followed with:
What just happened? This heading explains the working of tasks or instructions that you have just completed. You will also find some other learning aids in the book, including:
Pop quiz – heading These are short multiple choice questions intended to help you test your own understanding.
Have a go hero – heading These set practical challenges and give you ideas for experimenting with what you have learned. You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning. Code words in text are shown as follows: "Running CherryPy's setup.py script installs a number of modules in Python's Lib\site-packages directory." A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: t=t+'