1. Trang chủ
  2. » Công Nghệ Thông Tin

IT training python web frameworks khotailieu

83 22 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 83
Dung lượng 2,54 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

CHAPTER 1 Python Web FrameworkLandscape There are many options for building web applications with Python.Python’s powerful yet flexible nature makes it perfect for this task.It’s a good

Trang 1

Carlos de la Guardia

Python Web Frameworks

Trang 3

Carlos de la Guardia

Python Web Frameworks

Trang 4

[LSI]

Python Web Frameworks

by Carlos de la Guardia

Copyright © 2016 O’Reilly Media, Inc All rights reserved.

Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://safaribooksonline.com) For more information, contact our corporate/institutional sales department:

800-998-9938 or corporate@oreilly.com.

Editor: Allyson MacDonald

Production Editor: Shiny Kalapurakkel

Copyeditor: Gillian McGarvey

Proofreader: Charles Roumeliotis Interior Designer: David Futato

Cover Designer: Karen Montgomery

February 2016: First Edition

Revision History for the First Edition

Trang 5

Table of Contents

Introduction v

1 Python Web Framework Landscape 1

Web Framework List 3

2 Some Frameworks to Keep an Eye On 31

Django 31

Flask 35

Tornado 40

Bottle 45

Pyramid 48

CherryPy 53

3 What’s the Right Framework for You? 59

Don’t Look for Absolute Bests 59

Start by Defining Your Goals 61

Desirable Features 61

4 Developing Your Own Framework 63

Why Create a Framework? 63

Parts of a Basic WSGI Framework 64

Framework Building Blocks 65

Some Useful Resources 66

5 Summary 67

A Python Web Development Fundamentals 69

Trang 7

At the time of this writing, the web development landscape is domi‐nated by JavaScript tools Frameworks like ReactJS and AngularJSare very popular, and many things that were previously done on theserver are handled on the client side by these frameworks This isnot limited to the client Server-side JavaScript frameworks likeNodeJS are also prominent

Does that mean that languages like Python should throw in thetowel and forget about web applications? On the contrary Python is

a very powerful language that is easy to learn and provides a fastdevelopment pace It has many mature libraries for web-relatedtasks, from object-relational mapping (ORM) to web scraping.Python is also a fabulous “glue” language for making disparate tech‐nologies work together In this era where JSON APIs and communi‐cation with multiple systems are so important, Python is a greatchoice for server-side web development And it’s great for full-scaleweb applications, too!

There are many web frameworks for Python; some provide morefacilities than others, some offer a greater degree of flexibility ormore extensibility Some try to provide everything you need for aweb application and require the use of very specific components,whereas others focus on giving you the bare minimum so that youcan pick only the components your application needs

Among these frameworks, there are dozens that have a significantnumber of users How do newcomers to the language choose theright one for their needs? The easiest criterion would probably bepopularity, and there are two or three frameworks that will easily befound doing web searches or asking around This is far from ideal,

Trang 8

however, and leaves the possibility of overlooking a framework that

is better suited to a developer’s needs, tastes, or philosophy

In this report, we will survey the Python web framework landscape,giving aspiring web developers a place to start their selection pro‐cess for a web framework We will look in some detail at a few of theavailable frameworks, as well as give pointers about how to pick one,and even how to go about creating your own

Hopefully, this will make it easier for new developers to find what’savailable, and maybe give experienced Python developers an idea ortwo about how other web frameworks do things

What Do Web Frameworks Do?

A web application is not a standalone program but part of the web

“pipeline” that brings a website to a user’s browser There’s muchmore to it than your application code working under the hood tomake the web work, and having a good understanding of the otherpieces of the puzzle is key to being a good web developer In caseyou are new to web development or need a refresher, take a look atAppendix A to get your bearings

When writing a web application, in addition to writing the code thatdoes the “business logic” work, it’s necessary to figure out things likewhich URL runs which code, plus take care of things like security,sessions, and sending back attractive and functional HTML pages.For a web service, perhaps we need a JSON rendering of theresponse instead of an HTML page Or we might require both

No matter what our application does, these are parts of it that veryconceivably could be used in other, completely different applica‐

tions This is what a web framework is: a set of features that are com‐

mon to a wide range of web applications

Exactly which set of features a framework provides can vary a lotamong frameworks Some frameworks offer a lot of functionality,including URL routing, HTML templating systems, ORMs to inter‐act with relational databases, security, sessions, form generation, and

more These are sometimes referred to as full-stack frameworks Other frameworks, known by many as micro frameworks, offer a

much less varied set of features and focus on simplicity They usuallyoffer URL routing, templating, and not much else

Trang 9

This emphasis on size (micro and full-stack) can sometimes be con‐fusing Are we referring to the framework’s codebase? Are microframeworks for small applications and full-stack frameworks forlarge applications? Also, not all frameworks easily fit into one ofthese categories If a framework has lots of features but makes most

of them optional, does that still count as full-stack?

From an experienced developer point of view, it could make sense to

examine frameworks in terms of decisions made Many features

offered by frameworks, like which ORM it supports or which tem‐plating system it’s bundled with, imply a decision to use that specifictool instead of other similar components

Obviously, the more decisions made by the framework, the less deci‐sions the developer needs to make That means more reliance on theway the framework works, more knowledge of how its parts fittogether, and more integrated behavior—all within the confines ofwhat the web framework considers a web application Conversely, if

a developer needs to make more decisions, they’ll have more work

to do, but they will also have more control over their application,and can concentrate on the parts of a framework they specificallyneed

Even if the framework makes these decisions, most of them are notset in stone A developer can change these decisions, maybe byreplacing certain components or libraries The trade-off is losingsome framework functionality in return for that freedom

There are many Python web frameworks Besides size and decisionsmade for the developer, many of them offer unique features or spe‐cial twists on what a web appplication should do Some developerswill immediately feel attracted to some frameworks, or concludeafter some analysis that one of them is better suited for the specificproject they have in mind Regardless of the chosen framework, it’salways a good idea to be aware of the variety of other availableframeworks so that a better choice can be made if necessary

Trang 11

CHAPTER 1 Python Web Framework

Landscape

There are many options for building web applications with Python.Python’s powerful yet flexible nature makes it perfect for this task.It’s a good idea to know what’s available before going in that direc‐tion, though Perhaps one of the many existing options will suit yourneeds and save you a ton of work

To make it easier to know at a glance what frameworks are out there,the following list shows 30 web frameworks that are active and havemore than 1,000 monthly downloads at the time of this writing Foreach framework, the list presents the following information:

Slogan

This is a short phrase that comes from the framework’s site ordocumentation and attempts to convey the spirit of the frame‐work according to its creators

Trang 12

Relative popularity

A very crude attempt at gauging a project’s popularity, by nor‐malizing the number of monthly downloads and generating ascore Its purpose is only to give the reader a general idea abouthow one framework compares to another in terms of number ofusers For example, Django, which is the Python frameworkwith the largest number of downloads, has 10 stars At the otherend of the spectrum, BlueBream, which is barely above 1,000downloads, has one star This popularity scale should not betaken too seriously

Persistence

Many web applications require a storage layer of some sort, usu‐ally a database Because of this, most web frameworks aredesigned to use one or more specific data persistence options

Trang 13

This is another very common feature of web frameworks TheHTML markup for an application page is usually written in atemplating language

Web Framework List

Appier

Joyful Python Web App development.

Appier is an object-oriented Python web framework built for fast app development It’s as lightweight as possible, but not toolightweight It gives you the power of bigger frameworks, withoutthe complexity

Trang 15

The Zope Web Framework.

BlueBream is an open source web application server, framework,and library created by the Zope community and formerly known asZope 3 It is best suited for medium to large projects split into manyinterchangeable and reusable components

• Built on top of Zope 3

• Full stack, but with distributed architecture

• Mature, well-tested components

• Object database

Trang 16

Web application framework for the impatient.

Bobo is a lightweight framework for creating WSGI web applica‐tions Its goal is to be easy to use and remember

• Subroutes for multiple-step URL matching

• JSON request bodies

• Automatic response generation, based on return value

Bottle

Fast and simple WSGI framework for small web applications.

Bottle is a fast, simple, and lightweight WSGI micro web frameworkfor Python It is distributed as a single-file module and has nodependencies other than the Python Standard Library

Trang 17

A Minimalist Python Web Framework.

CherryPy allows developers to build web applications in much thesame way they would build any other object-oriented Python pro‐gram

Trang 19

• Proactive URL route checking

• Improved middleware paradigm

Cyclone

Facebook’s Tornado on top of Twisted.

Cyclone is a web server framework for Python that implements theTornado API as a Twisted protocol

Trang 20

The web framework for perfectionists with deadlines.

Django is a high-level Python Web framework that encourages rapiddevelopment and clean, pragmatic design

Trang 21

An unladen web framework for building APIs and app backends.

Falcon is a minimalist, high-performance web framework for build‐ing RESTful services and app backends with Python

Trang 22

Web development one drop at a time.

A micro framework based on Werkzeug, Jinja2, and good intentions

Trang 23

• RESTful request dispatching

• Allows modular applications with plugins

Trang 24

• Generic views and models

• Functional CRUD patterns

• Automatic RESTful interface

• Automatic URL routing

Grok

A smashing web framework.

Grok uses the Zope Component Architecture and builds on Zopeconcepts like content objects (models), views, and adapters Its sim‐plicity lies in using convention over configuration and sensibledefaults when wiring components together

Trang 25

• Convention over configuration

• Takes advantage of full Zope toolkit

Trang 26

• Focus on web services

• Integrates Twisted concepts like deferreds

Trang 27

A micro web framework with superpowers.

Morepath is a Python WSGI micro framework It uses routing, butthe routing is to models Morepath is model-driven and flexible,which makes it expressive

• Simple, flexible permissions

• Easy to extend and override

Trang 28

Web framework based on Asyncio stack.

Muffin is a fast, simple, and asyncronous web framework forPython 3

A framework to make writing web applications in Python easy.

Pylons 1.0 is a lightweight web framework emphasizing flexibilityand rapid development

Trang 29

• Uses existing and well-tested Python packages

• Extensible application design

• Minimalist, component-based philosophy

Pyramid

The start small, finish big, stay finished framework.

Pyramid is a general, open source, Python web application develop‐ment framework Its primary goal is to make it easier for a Pythondeveloper to create web applications

Author

Chris McDonough, Agendaless Consulting

Website

https://trypyramid.com

Trang 30

• Powerful configuration system

• Overridable asset specifications

Trang 31

• Ideal for long-polling and websockets

• Can scale to tens of thousands of open connections

TurboGears

The web framework that scales with you.

TurboGears is a Python web framework based on the ObjectDis‐patch paradigm It is meant to make it possible to write both smalland concise applications in Minimal mode or complex applications

in Full Stack mode

Trang 32

Building the engine of your Internet.

An extensible framework for Python programming, with specialfocus on event-based network programming and multiprotocolintegration Twisted includes twisted.web, a web application serverbased on the concept of resources

Trang 33

twisted.web.template

Features

• Takes advantage of Twisted networking power

• Allows “spreadable” web servers (multiple servers answerrequests on same port)

• Can use any WSGI application as a resource

Uliweb

Unlimited Python web framework.

Uliweb is a full-stacked Python-based web framework It has threemain design goals: reusability, configurability, and replaceability Itsfunctionality revolves around these goals

Trang 34

It’s elementary, my dear Watson.

A framework designed to get out of your way and let you code yourapplication rather than spend time wrangling with the framework Itfollows the “convention over configuration” ideal

Trang 36

Everything in one package with no dependencies.

Free open source full-stack framework for rapid development offast, scalable, secure, and portable database-driven web-based appli‐cations

Trang 37

Taking Google App Engine’s webapp to the next level!

webapp2 is a lightweight Python web framework compatible withGoogle App Engine’s webapp

• Compatible with webapp

• Better URI routing and exception handling

• Extras package with optional utilities

WebPages

A Python web framework.

This project was designed for web developers who want to do more

in less time To create a new project with Hello World and a database

connection, you only need a few minutes

Trang 38

• Convention over configuration

• Settings per component

• User authentication out of the box

• ORM with simplified syntax

wheezy.web

Python’s fastest web framework.

A lightweight, high-performance, high-concurrency WSGI webframework with the key features to build modern, efficient webapplications

Author

Andriy Kornatskyy

Website

http://wheezyweb.readthedocs.org

Trang 41

CHAPTER 2 Some Frameworks to

Keep an Eye On

As we have seen, there are many Python web frameworks to choosefrom In fact, there are too many to be able to cover every one indetail in this report Instead, we will take a deeper look at six of themost popular There is enough diversity here to give the readersome idea about how different frameworks work and what a webapplication’s code looks like when using them

For each framework, we are going to give a general description, dis‐cuss some key features, look at some sample code, and talk a bitabout when it should be used When possible, code for a simplesingle-file application will be shown Quick start instructionsassume Python and pip or easy_install are present on the system

It is also recommended that you use virtualenv (or pyvenv forPython 3.3+) to create an isolated environment for your application.For simplicity, the examples do not show the setup of pip and virtualenv See Appendix A for help with any of these tools

Django

Django is without a doubt the most popular web framework forPython at the time of this writing Django is a high-level framework,designed to take care of most common web application needs.Django makes a lot of decisions for you, from code layout to secu‐rity It’s also very well documented, so it’s very easy to get a project

Ngày đăng: 12/11/2019, 22:28

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN