Tải bản đầy đủ (.pdf) (336 trang)

Tài liệu Python 3 Web Development Beginner''''s Guide ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.28 MB, 336 trang )

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 ()


www.it-ebooks.info
Credits
Author
Michel Anders
Reviewers
Michael Driscoll
Róman Joost
Tomi Juhola
Andrew Nicholson
Herjend Teny
Acquision Editor
Sarah Cullington
Development Editor
Neha Mallik
Technical Editors
Sakina Kaydawala
Gauri Iyer
Copy Editor
Leonard D'Silva
Project Coordinators
Poorvi Nair
Michelle Quadros
Proofreader
Mario Cecere
Indexer
Tejal Daruwale
Graphics
Nilesh Mohite
Producon Coordinator
Kruthika Bangera

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
and maintains a blog on Python-
specic subjects at />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 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
mes they share with me. Most of all, I want to thank Jesus for saving me

from myself.
Róman Joost discovered open source soware in 1997. He is the project manager for
user documentaon for GNU Image Manipulaon Program (GIMP). Róman also helped with
German internaonalizaon of GIMP. He has been contribung to GIMP and Zope open
source projects for eight years.
Róman has a Diplom-Informaker (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
relaxaon, he enjoys photography and digital painng with GIMP.
www.it-ebooks.info
Tomi Juhola is a soware 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 nancial company and shares this me between development lead
dues and helping other projects to adopt Scrum and Agile methodologies. He likes to spend
his free me with new interesng 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 compung technologies. He is currently a Technical Director
with Innite Recursion Pty Ltd.—a bespoke soware engineering company located in Sydney,
Australia. He is a passionate advocate and a parcipant in the free, libre, and open source
soware (FLOSS) community and has acvely parcipated since 1999 contribung code,
ideas, and energy in this engineering community. He was a Technical Reviewer for the book
Python Tesng: 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
/>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
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.

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
Chapter 1: Choosing Your Tools 7
Idenfying the components of a web applicaon 7

Time for acon – geng an overview of a web applicaon 8
Choosing suitable tools 10
Time for acon – choosing a delivery framework, also known as web server 11
Time for acon – choosing a server-side scripng language 12
Time for acon – choosing a database engine 14
Time for acon – deciding on object relaonal mappers 15
Time for acon – choosing a presentaon framework 17
Designing for maintainability and usability 18
Tesng 18
Time for acon – choosing a test framework 19
Version management 19
Usability 20
Good looking – adhering to common GUI paradigms 20
Themable 21
Cross-browser compable 21
Cross-plaorm compable 22
Maintainability 22
Standards compliant 22
Security 23
Reliable 23
Robust 23
Access control and authencaon 24
Condenality 24
Integrity 25
A nal word on security 25
Help, I am confused! 25
Time for acon – maintaining overview 26
Summary 28
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
Serving a logon screen 69
Seng up a session 70

Expiring a session 71
Designing a task list 72
Time for acon – running tasklist.py 72
Python: the task module 75
Time for acon – implemenng the task module 76
Adding new tasks 80
Deleng a task 81
JavaScript: tasklist.js 83
Time for acon – styling the buons 83
JavaScript: toolp.js 85
Time for acon – implemenng inline labels 86
www.it-ebooks.info
Table of Contents
[ iii ]
CSS: tasklist.css 87
Summary 90
Chapter 4: Tasklist II: Databases and AJAX 91
The advantages of a database compared to a lesystem 92
Choosing a database engine 92
Database-driven authencaon 93
Time for acon – authencaon using a database 94
Tasklist II – storing tasks in a database 99
Improving interacvity with AJAX 99
Time for acon – geng the me with AJAX 100
Redesigning the Tasklist applicaon 102
Database design 103
Time for acon – creang the task database 103
Time for acon – retrieving informaon with select statements 105
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 – using input elds with auto compleon 166
The presentaon layer 168

Time for acon – using an enhanced presentaon layer 168
Summary 170
Chapter 6: Building a Wiki 171
The data layer 172
Time for acon – designing the wiki data model 172
The delivery layer 175
Time for acon – implemenng the opening screen 176
The structural components 177
The applicaon methods 179
Time for acon – implemenng a wiki topic screen 180
Time for acon – eding wiki topics 182
Addional funconality 185
Time for acon – selecng an image 185
Time for acon – implemenng a tag cloud 190
Time for acon – searching for words 192
The importance of input validaon 195
Time for acon – scrubbing your content 196
Time for acon – rendering content 200
Summary 201
Chapter 7: Refactoring Code for Reuse 203
Time for acon – taking a crical look 203
Refactoring 205
Time for acon – dening new enes: how it should look 205
Metaclasses 206
Time for acon – using metaclasses 207
MetaEnty and AbstractEnty classes 208
Time for acon – implemenng the MetaEnty and AbstractEnty classes 209
Relaons 217
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
Access control 274
Time for acon – implemenng access control 275

Role-based access control 278
Time for acon – implemenng role-based access control 279
Summary 283
Chapter 10: Customizing the CRM Applicaon 285
Time for acon – sorng 285
Time for acon – ltering 290
Customizaon 292
Time for acon – customizing enty displays 292
Time for acon – customizing enty lists 298
Time for acon – adding a delete buon 301
Summary 302
Appendix A: References to Resources 303
Good old oine reference books 303
Addional websites, wikis, and blogs 304
Appendix B: Pop Quiz Answers 307
Chapter 2, Creang a Simple Spreadsheet 307
Chapter 3, Tasklist I: Persistence 308
Chapter 4, Tasklist II: Databases and AJAX 309
Chapter 5, Enes and Relaons 310
Chapter 6, Building a Wiki 310
Index 311
www.it-ebooks.info
www.it-ebooks.info
Preface
Building your own Python web applicaons provides you with the opportunity to have great
funconality, with no restricons. However, creang web applicaons with Python is not
straighorward. Coupled with learning a new skill of developing web applicaons, 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 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
the previous chapter. We will use the SQLite database engine on the server to store items
and will use jQuery's AJAX funconality to dynamically update the contents of the web

applicaon. On the presentaon side, we will encounter jQuery UI's event system and will
learn how to react on mouse clicks.
Chapter 5, Enes and Relaons, most real life applicaons sport more than one enty and
oen many of these enes are related. Modeling these relaons is one of the strong points
of a relaonal database. In this chapter, we will develop a simple framework to manage
these enes and use this framework to build an applicaon to maintain lists of books for
mulple users.
Chapter 6, Building a Wiki, develops a wiki applicaon and in doing so we focus on two
important concepts in building web applicaons. The rst one is the design of the data layer.
The wiki applicaon is quite complex, and in this chapter, we try to see where the limitaons
in our simple framework lie. The second one is input validaon. Any applicaon 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 validaon.
Chapter 7, Refactoring Code for Reuse, aer doing a substanal bit of work, it is oen a good
idea to take a step back and look crically at your own work to see if things could have been
done beer. In this chapter, we look at ways to make the enty framework more generally
useful and employ it to implement the books applicaon a second me.
Chapter 8, Managing Customer Relaons, there is more to an enty framework and CherryPy
applicaon code than merely browsing lists. The user must be able to add new instances and
edit exisng ones. This chapter is the start of the development of a CRM applicaon that will
be extended and rened in the nal chapters.
Chapter 9, Creang Full-Fledged Webapps: Implemenng Instances, focuses on the design
and implementaon of the user interface components to add and maintain enes,
and relaons between enes, in a way that is independent of the type of enty. This
funconality is immediately put to use in the CRM applicaon 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 Applicaon, is the nal chapter and it extends our
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 ( 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
complex, database-driven, cross browser compable web applicaons that are maintainable
and look good, will nd this book of most use. All applicaons in the book are developed

in Python 3, but experience with Python 2.x is sucient to understand all examples.
JavaScript plays an important supporng role in many of the example applicaons 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.
www.it-ebooks.info
Preface
[ 4 ]
Conventions
In this book, you will nd several headings appearing frequently.
To give clear instrucons of how to complete a procedure or task, we use:
Time for action – heading
1. Acon 1
2. Acon 2
3. Acon 3
Instrucons oen need some extra explanaon so that they make sense, so they are
followed with:
What just happened?
This heading explains the working of tasks or instrucons that you have just completed.
You will also nd some other learning aids in the book, including:
Pop quiz – heading
These are short mulple choice quesons intended to help you test your own understanding.
Have a go hero – heading
These set praccal challenges and give you ideas for experimenng with what you have
learned.
You will also nd a number of styles of text that disnguish between dierent kinds of
informaon. Here are some examples of these styles, and an explanaon 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:
<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.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this

book—what you liked or may have disliked. Reader feedback is important for us to
develop tles that you really get the most out of.
To send us general feedback, simply send an e-mail to
, and
menon the book tle via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the
SUGGEST A TITLE form on
www.packtpub.com or e-mail
If there is a topic that you have experse in and you are interested in either wring or
contribung to a book, see our author guide on
www.packtpub.com/authors.
www.it-ebooks.info
Preface
[ 6 ]
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you
to get the most from your purchase.
Downloading the example code for this book
You can download the example code les for all Packt books you have purchased from your
account at . If you purchased this book elsewhere, you can
visit
and register to have the les e-mailed directly
to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do
happen. If you nd a mistake in one of our books—maybe a mistake in the text or the
code—we would be grateful if you would report this to us. By doing so, you can save other
readers from frustraon and help us improve subsequent versions of this book. If you
nd any errata, please report them by vising


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
/>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
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 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:
 Indenfying the components that a web applicaon consists of
 Choosing suitable tools
 Considering what designing for maintainability and usability implies

There is a lot of ground to cover, so let's get started.
Identifying the components of a web application
A web applicaon is not a monolithic object. In designing such an applicaon, it might
help focus if you look at an applicaon as a collecon of related objects, each with its well-
dened purpose. This can be done with mulple levels of detail and even the mile high view
may already give some valuable insights.
www.it-ebooks.info
Choosing Your Tools
[ 8 ]
Time for action – getting an overview of a web application
From the following picture shown, it should be clear that a web applicaon is not a singular
thing. It consists of parts that reside on a server and parts that run on the computer of the
user. Both halves are just as important; although the server may hold the applicaon data
and implement the logic to modify that data following requests from the user, the data is
displayed by the part of the web applicaon running in the browser on the client computer
and the user signals his/her request by interacng with the user interface components in the
browser, for example, by clicking on an "OK" buon.
 Think about your applicaon and consider both server and client-side. The
advantage of looking at the individual halves is that we might make choices
that are opmal for the specic half.
 Look at the general requirements for the client half. For example, because we want
to oer the user a sophiscated user interface, we opt for the jQuery UI library. This
decision does not touch the overall design decision on the server, because apart
from delivering the les that the jQuery UI library consists of, the choice of user
interface library has no impact on the choice of the database engine or the server
operang system for example.
 Look at the requirements for the server half. For example, consider which
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:
 Fetching the content to structure the data (oen HTML les)
 Running JavaScript code to enhance the presentaon of the data
 Allowing interacon with the user

Of course we could zoom in even further to reveal addional detail like the operang
system on the client and the server, or even the hardware and the network components and
although occasionally useful, this would generally be overkill. With the main components
clearly idened, we can take the next step and choose suitable tools to implement these
components.
www.it-ebooks.info
Choosing Your Tools
[ 10 ]
Choosing suitable tools
If you want to develop quality applicaons, you need suitable tools. Tools, of course, do not
guarantee quality, but they can make life a lot easier. When developing web applicaons,
there are two kinds of tools you need to consider: the ones you use to design, build, test,
and deploy your applicaon, like editors, version management systems, test frameworks,
and maybe a package tool, and the tools that deliver your applicaon to the end user. That
last set of tools consists of a whole chain of components, from server operang system, web
server, and database engine, all the way to the web browser and the JavaScript libraries used
to display and interact with the applicaon.
When we start a project, we have to know which tools we need and have to understand the
capabilies and limitaons of the many variaons of these tools. There are, for example,
quite a few JavaScript libraries that may be used to provide cross-browser compable user
interacon.
The trick is to make an informed choice. These choices are not necessarily limited to open
source tools. If budget permits, it might be worthwhile to have the benet of the special
features many commercial development tools and libraries oer, but in this book, we limit
ourselves to open source and/or free resources. This makes sense as the cost of tooling and
licenses in small projects can make a signicant dent in a budget.
The opportunity to use free tools might not exist for the deployment environment. You may
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

×