Tài liệu Python 3 Web Development Beginner''''s Guide - Pdf 10

www.it-ebooks.info
Python 3 Web Development
Beginner's Guide
Use Python to create, theme, and deploy unique web
applicaons
Michel Anders
BIRMINGHAM - MUMBAI
www.it-ebooks.info
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 transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon 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 informaon about all of the
companies and products menoned in this book by the appropriate use of capitals. However,
Packt Publishing cannot guarantee the accuracy of this informaon.
First published: May 2011
Producon 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])

Cover Work
Kruthika Bangera
www.it-ebooks.info
About the Author
Michel Anders, aer his chemistry and physics studies where he spent more me on
computer simulaons 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 dierent
companies, including an Internet provider, a hospital, and a soware development company.
Aer his inial exposure to Python as the built-in scripng 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 wring his rst book, Blender 2.49
Scripng (Packt Publishing, 978-1-849510-40-0).
He loves to help people with Blender and Python-related quesons and may be contacted as
'varkenvarken' at
http://www.blenderartists.org/ and maintains a blog on Python-
specic subjects at http://michelanders.blogspot.com/.
For Clemenne, always.
www.it-ebooks.info
About the Reviewers
Michael Driscoll has been programming Python since the Spring of 2006 and has
dabbled in other languages since the late ninees. He graduated from the University with
a Bachelors of Science degree, majoring in Management Informaon Systems. Michael
enjoys programming for fun and prot. His hobbies include Biblical apologecs, 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

people/
.
www.it-ebooks.info
www.PacktPub.com
Support les, eBooks, discount offers, and more
You might want to visit www.PacktPub.com for support les and downloads related to your
book.
Did you know that Packt oers eBook versions of every book published, with PDF and ePub
les available? You can upgrade to the eBook version at
www.PacktPub.com and as a print
book customer, you are entled 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 collecon of free technical arcles, sign up for a
range of free newsleers, and receive exclusive discounts and oers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant soluons to your IT quesons? PacktLib is Packt's online digital book
library. Here, you can access, read and search across Packt's enre 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 enrely free books. Simply use your login credenals for
immediate access.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface 1

www.it-ebooks.info
Table of Contents
[ ii ]
Chapter 2: Creang a Simple Spreadsheet 29
Python 3 30
Time for acon – installing Python 3 CherryPy 30
Time for acon – installing CherryPy 31
Installing jQuery and jQuery UI 31
Serving an applicaon 32
Time for acon – serving a dummy applicaon 33
Time for acon – serving HTML as dynamic content 34
Who serves what: an overview 36
HTML: separang form and content 37
Time for acon – a unit convertor 38
HTML: form-based interacon 39
JavaScript: using jQuery UI widgets 40
Time for acon – conversion using unitconverter.js 40
jQuery selectors 42
CSS: applying a jQuery UI theme to other elements 43
Time for acon – converng a unit convertor into a plugin 45
JavaScript: creang a jQuery UI plugin 46
Designing a spreadsheet applicaon 51
Time for acon – serving a spreadsheet applicaon 51
HTML: keeping it simple 52
JavaScript: creang a spreadsheet plugin 52
The missing parts 58
Summary 58
Chapter 3: Tasklist I: Persistence 59
Designing a tasklist applicaon 59
Time for acon – creang a logon screen 62

TaskDB – interfacing with the database 106
Time for acon – connecng to the database 106
Time for acon – storing and retrieving informaon 107
Time for acon – updang and deleng informaon 109
Tesng 111
Time for acon – tesng factorial.py 112
Now what have we gained? 113
Time for acon – wring unit tests for tasklistdb.py 114
Designing for AJAX 116
Click handlers 120
The applicaon 121
Time for acon – pung it all together 123
Have a go hero – refreshing the itemlist on a regular basis 125
Summary 126
Chapter 5: Enes and Relaons 127
Designing a book database 127
The Enty class 128
Time for acon – using the Enty class 129
Time for acon – creang instances 132
The Relaon class 138
Time for acon – using the Relaon class 138
Relaon instances 141
Time for acon – dening the Books database 144
The delivery layer 150
Time for acon – designing the delivery layer 151
Time for acon – adding a new book 162
www.it-ebooks.info
Table of Contents
[ iv ]
Auto compleon 165

Time for acon – dening new relaons: how it should look 217
Implemenng the MetaRelaon and AbstractRelaon classes 219
Adding new methods to exisng classes 222
Browsing lists of enes 224
Time for acon – using a table-based Enty browser 224
Time for acon – examining the HTML markup 229
Caching 232
The books applicaon revisited 236
Time for acon – creang a books applicaon, take two 236
Summary 242
www.it-ebooks.info
Table of Contents
[ v ]
Chapter 8: Managing Customer Relaons 243
A crical review 243
Designing a Customer Relaonship Management applicaon 244
Time for acon – implemenng a basic CRM 244
Adding and eding values 248
Time for acon – adding an instance 249
Time for acon – eding an instance 251
Adding relaons 257
Picklists 259
Time for acon – implemenng picklists 259
Summary 262
Chapter 9: Creang Full-Fledged Webapps: Implemenng Instances 263
Even more relaons 263
Time for acon – showing one-to-many relaonships 264
Time for acon – adapng MetaRelaon 266
Time for acon – enhancing Display 270
Time for acon – enhancing Browse 271

Python 3 Web Development Beginner's Guide shows you how to independently build your
own web applicaon 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 me and is oen ddly to get right. This book will show you
how to design and implement a complex program from start to nish. Each chapter looks
at a dierent type of web applicaon, meaning that you will learn about a wide variety
of features and how to add them to your customized web applicaon. You will also learn
to implement jQuery into your web applicaon to give it extra funconality. By using the
right combinaon of a wide range of tools, you can have a fully funconal, complex web
applicaon up and running in no me.
A praccal guide to building and customizing your own Python web applicaon, without the
restricon of a pre-dened framework.
What this book covers
Chapter 1, Choosing Your Tools, looks at the many aspects of designing web applicaons.
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
applicaon that does not deal with coding directly, such as security or usability.
Chapter 2, Creang a Simple Spreadsheet, develops a simple spreadsheet applicaon. The
spreadsheet funconality will be enrely implemented in JavaScript plus jQuery UI, but
on the server-side, we will encounter the applicaon server, CherryPy, for the rst me
and we will extend it with Python code to deliver the page that contains the spreadsheet
applicaon dynamically.
www.it-ebooks.info
Preface
[ 2 ]
Chapter 3, Tasklist I: Persistence, a full edged web applicaon needs funconality to store
informaon on the server and a way to idenfy dierent users. In this chapter, we address
both issues as we develop a simple applicaon to maintain lists of tasks.
Chapter 4, Tasklist II: Databases and AJAX, refactors the tasklist applicaon developed in

framework and thereby our CRM applicaon by taking a look at browsing, ltering,
and sorng large numbers of enes. We also take a look at what is needed to allow
customizaon by the end user of the applicaon's appearance and its funconality.
www.it-ebooks.info
Preface
[ 3 ]
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 applicaons, you will
need the following pieces of open source soware:
 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 addional plugins for jQuery and provide installaon instrucons where appropriate.
You will also need a web browser to interact with your applicaons. The applicaons 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 applicaons, 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 highlighng capabilies for Python
as well as JavaScript and HTML. The author uses Notepad++ (
http://notepad-plus-
plus.org/
) on Windows and JOE (http://joe-editor.sourceforge.net/) on Linux,
but the choice is enrely up to you.
Who this book is for
Moderately experienced Python programmers who want to learn how to create fairly

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:
<div id="main">
<ul>
<li>one</li>
<li class="highlight">two</li>
<li>three</li>
</ul>
www.it-ebooks.info
Preface
[ 5 ]
</div>
<div id="footer">footer text</div>
When we wish to draw your aenon to a parcular part of a code block, the relevant lines
or items are set in bold:
t=t+'<thead class="ui-widget-header">
<tr class="ui-helper-reset"><th></th>';
for(i=0;i<opts.cols;i=i+1){
t=t+'<th class="ui-helper-reset">' +
String.fromCharCode(65+i)+"</th>";
}
Any command-line input or output is wrien as follows:
python -c "import cherrypy"
New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in the text like this: "New books or authors may
be added by clicking the Add new buon."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.

nd any errata, please report them by vising
http://www.packtpub.com/support,
selecng your book, clicking on the errata submission form link, and entering the details
of your errata. Once your errata are veried, your submission will be accepted and the
errata will be uploaded on our website, or added to any list of exisng errata, under the
Errata secon of that tle. Any exisng errata can be viewed by selecng your tle from
http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt,
we take the protecon of our copyright and licenses very seriously. If you come across any
illegal copies of our works, in any form, on the Internet, please provide us with the locaon
address or website name immediately so that we can pursue a remedy.
Please contact us at
[email protected] with a link to the suspected pirated material.
We appreciate your help in protecng our authors, and our ability to bring you valuable
content.
Questions
You can contact us at [email protected] if you are having a problem with any
aspect of the book, and we will do our best to address it.
www.it-ebooks.info
1
Choosing Your Tools
In this chapter, we look at the many aspects of designing web applicaons.
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.
Also, as this book covers more than just developing example applicaons, we
illustrate some issues that are relevant when designing an applicaon that does
not deal with coding directly, like security or usability.
In this chapter, we will be:

implementaon language to use. We select Python as the language to implement
the server-side code but if we had compelling arguments to switch to C#, we could
do so without the need to change anything on the client.
If we zoom in on our web applicaon, an image emerges of many interacng layers, each
encapsulang a well dened piece of funconality. Everywhere two layers touch, informaon
ows through a well dened interface (API). This helps in the separaon of concepts (our
applicaon is only talking to the database layer to store and retrieve persistent data and only
to the web server to return data upon request) but in pracce, the separaon between these
layers isn't completely clear in all circumstances. For example, the server-side part of our
applicaon is actually an integral part of the web server.
This simple schemac of a web applicaon is virtually idencal to a regular client-server
architecture. However, when we look more closely at the implementaon of the client and
the interacon between client and server, dierences will emerge as we will see in the next
secon where we zoom in a bit closer.
www.it-ebooks.info
Chapter 1
[ 9 ]
What just happened?
With both halves of the applicaon idened, we can now zoom in on each individual half.
This will enable us to get a more detailed image, that will help us to make informed decisions
regarding the smaller components that make up our applicaon.
The main components are easy to idenfy:
 The data store holds data on the server (it is oen a database engine, somemes
just les on the lesystem).
 The server-side applicaon services requests that are passed through from the web
server.
 The web server forwards those responses to the client again and may serve stac
les as well.
The web browser takes care of running the client side of the applicaon, but within the
browser, we can idenfy several layers of acvies. These consist of:

well develop your applicaon on your own Linux box, but test and deploy it on a Windows
server. The laer needs a license that will not be free, but even open source opons are not
always free. Many companies nowadays shi to deploying their applicaons to the cloud
and even though these machines might be running an open source operang system, you
pay not only for CPU power and bandwidth but also for support, the laer being crucial in
applicaons that will lose you money if they are not running. However, using open source
tools in general gives you a much wider choice because many tools run equally well on any
plaorm.
In the following secons, we will look at the many components that make up the tool chain
and will try to show what arguments were used for the choices made for developing the
applicaons in this book and what (if any) viable alternaves are there. Note that some
arguments are quite subjecve and the choice nally made does not necessarily indicate that
the alternave is bad; we certainly are not aempng to start ame wars over which tool is
beer. We simply list requirements for applicaon development as we see it and try to nd
the tools suitable for the task. In some situaons, another tool might be beer, but for this
book, we try to nd a matching toolset that can be used for all sample applicaons that are
free (as in beer) and easy to learn and use.
www.it-ebooks.info


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status