You are on page 1of 36

Python Web App

Web Framework(s)

keitheis
PycTW 2011

keitheis

@gmail.com

Software Developer
Web Developer
Python
ID Python Keith
Python Taiwan

PycTW 2011

web

ZH.Wikipedia:
200827

WebsiteHTML

-
199186
PycTW 2011


App Application

PycTW 2011

app

...

python

Python
1990 Guido van Rossum ( GvR BDFL)

TIOBE
2007
2010

PycTW 2011

http://wiki.python.org.tw/Python


print hello world?

PycTW 2011

hello world

hello world.[php|p6|py]
<? print hello world;
hello world.say;
print hello world

PycTW 2011


echo hello world;

PycTW 2011

<?php

hello.php


echo Fear, Uncertainty, and Doubt;

Apache

MySQL
PhP

PycTW 2011

FUD

FUD
echo FUD;

Apache
RewriteCond *.p* RewriteRule @@ = = > <
MySQL
mysql -e @racle$$$$$$$$$$$$$$$$$$QL
PhP
<? php //

PycTW 2011

pe
rl
6

ng

la

Go

Er
la
n

y
p
.

PycTW 2011


Exercise #50 on

The Hard Way

Learn Python The Hard Way, 2ed


http://learnpythonthehardway.org/book/ex50.html

Your First Website


lpthw.web
web.py

PycTW 2011

The Hard Way


import web
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == "__main__":
app.run()

PycTW 2011


import bottle

@bottle.route('/')
def index():
return 'Hello World'

bottle.run(host='localhost', port=8080)

PycTW 2011

app.py

PHP


Apache
MySQL
PHP

PycTW 2011

Python

PycTW 2011



Python
easy_installdistribute pip
Python 3
virtualenv
install bottle...
Window$
PycTW 2011

Python

Python


Python
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py
$ pip install bottle
update: http://quest.keitheis.org/post/6713569631/pip-install-on-windows-linux-mac
PycTW 2011

yak shaving

XXX





PycTW 2011

http://learnpythonthehardway.org/book/ex46.html

Pyramid

PycTW 2011

Pylons Project

PycTW 2011

Pyramid

Pylons Project

Python Web Framework


The Shrinking Python Web Framework World by Ian Bicking
http://blog.ianbicking.org/2007/08/21/the-shrinking-python-web-framework-world/


Pylons & repoze.bfg Merger - or - Where'd Pylons 2.0 go?
https://groups.google.com/d/topic/pylons-discuss/l_qhijQpoo4/discussion

Pylons ( benbangert) 100% coverage repoze.bfg ( 80


commit )

PyCon: State of Pylons/TurboGears 2/repoze.bfg


http://jjinux.blogspot.com/2011/04/pycon-state-of-pylonsturbogears.html

PycTW 2011

N ot Built by Aliens

Golden Framework Awards

Pylons Project: Pyramid 1.0, TurboGears 2.1.1


Bottle 0.9, Flask
web2py 1.96.4
Django 1.3
Zope, CherryPy, web.py, Tornado...
(rails III)
PycTW 2011

http://wiki.python.org/moin/WebFrameworks








...

PycTW 2011

What pattern else?


HTTPServer?
Paster
Tornado
nginx (uWSGI)
Apache
cherokee
PycTW 2011

web server

database

NoDatabase
To SQL
SQLite, PostgreSQL, MySQL, Percona...
Not to SQL (NoSQL)
redis, MongoDB, CouchDB, Hadoop, BigTable, Memcache...

PycTW 2011

template engine

${escape(foo)} {{bar | escape}}% if <% if %>


${escape(default(get_name(user), "No Name"))}
{{ user | get_name | default('No Name') | escape }}
Django
(Jython, Google App Engine, IronPython, Heroku...)
Jinja2MakoChameleon (ZPT)...

PycTW 2011

Web Server Gateway Interface


http://blog.ez2learn.com/2010/01/27/introduction-to-wsgi/

GI
PSGI - Perl version of WSGI
Rack - Ruby version of WSGI

PycTW 2011

WSGI

Pylons

WSGI middleware

http://k0s.org/mozilla/craft/wsgi.png

2009 rz

Non-blocking WSGI

mailer

Rails 3: Action Mailer


pyramid_mailer
kai/lib/mail.py

https://bitbucket.org/bbangert/kai/

Lamson web
a framework for building kickass email applications

http://amix.dk/blog/post/19608

PycTW 2011

HTTP
pip install Requests

testing HTTP

urllib?

r2 = requests.get(
'http://localhost:5000')

import requests
>>> r.headers['content-type']
r = requests.get(
'http://google.com')

>>> r.content

>>> r.status_code

request.put(...

401

request.delete(...

http://pypi.python.org/pypi/requests
PycTW 2011

Flask

patterns for flask

Larger Applications

Form Validation with WTForms

Application Factories

Template Inheritance

Application Dispatching

Message Flashing

Deploying with Distribute

AJAX with jQuery

Deploying with Fabric

Custom Error Pages

Using SQLite 3 with Flask

Lazily Loading Views

SQLAlchemy in Flask

MongoKit in Flask

Uploading Files

Adding a fav icon

Caching
View Decorators
PycTW 2011

http://flask.pocoo.org/docs/patterns/

MVC

SQL HTML
<!DOCTYPE html> <!-- HTML5 -->
<html><body>
<script>
<?phq $sql = select hours.* from my_time; ?>
var feel=<? $result = db->query($sql);
if ($result == $many) echo ; ?>;
alert(feel);
</script></body></html>

PycTW 2011

PycTW 2011

You might also like