Differences between revisions 2 and 21 (spanning 19 versions)
Revision 2 as of 2008-09-24 13:18:16
Size: 3639
Editor: HuangYi
Comment:
Revision 21 as of 2008-11-26 16:01:59
Size: 27
Editor: HuangYi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
== wsgiref ==

wsgiref 提供一些 web 开发的基础功能,最有用的就是提供了一个简单的开发服务器。

{{{#!python
import wsgiref
def hello_app(environ, start_response):
    start_response('200 OK', [('Content-type','text/plain')])
    yield 'Hello world\n'
    for key, value in environ.items():
        yield '%s : %s\n' % (key, value)

server = wsgiref.make_server('localhost', 8000, hello_app)
server.serve_forever()
}}}

== mako ==

mako 是一个模板引擎,通过将动态的数据填充到模板中,可以用来生成复杂的页面。

{{{#!python
from mako import Template
tmpl = Template('./simple.html')
print tmpl.render(data = {'a':1, 'b':2})
}}}

simple.html
{{{
<html>
  <head>
    <title>简单mako模板</title>
  </head>
  <body>
    <h5>Hello World!</h5>
    <ul>
      % for key, value in environ.items():
      <li>
        ${key} - ${value}
      <li>
      % endfor
    </ul>
  </body>
</html>
}}}

和 wsgiref 整合:

{{{#!python
import wsgiref
from mako import Template
def hello_app(environ, start_response):
    tmpl = Template('./simple.html')
    content = tmpl.render(data=environ)
    start_response('200 OK', [('Content-type','text/plain')])
    return [content]

server = wsgiref.make_server('localhost', 8000, hello_app)
server.serve_forever()
}}}

== sqlalchemy ==

sqlalchemy 是一个 ORM,提供python对象与关系数据库之间的映射,通过 python 对象,
对关系数据库进行操纵。

model.py
{{{#!python
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///data.db')
Session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
Base = declarative_base()

class Dictionay(Base):
    __tablename__ = 't_dictionay'
    key = Column('key', String(255), primary_key=True)
    value = Column('value', String(255))

# 创建数据库
Base.metadata.create_all(engine)

session = Session()
for item in ['python','ruby','java']:
    dictionay = Dictionay(key=item, value=item.upper())
    session.add(dictionay)

session.commit()

for dictionay in session.query(Dictionary):
    print dictionay.name, dictionay.value
}}}

上面是个 demo,下面我们把它分解,并放到web应用中去。

model.py
{{{#!python
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///data.db')
Session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
Base = declarative_base()

class Dictionay(Base):
    __tablename__ = 't_dictionay'
    key = Column('key', String(255), primary_key=True)
    value = Column('value', String(255))
}}}

create_db.py
{{{#!python
from model import Base, Session, Dictionary

# 创建数据库
Base.metadata.create_all(engine)

# 插入初始数据
session = Session()
for item in ['python','ruby','java']:
    dictionay = Dictionay(key=item, value=item.upper())
    session.add(dictionay)

session.commit()
}}}

和 wsgiref 整合
{{{#!python
import wsgiref
from mako import Template
from model import Session, Dictionary
def hello_app(environ, start_response):
    session = Session()
    dictionaries = session.Query(Dictionary)
    data = dict([(dictionary.name, dictionary.value) for dictionary in dictionaries])

    tmpl = Template('./simple.html')
    content = tmpl.render(data=data)

    start_response('200 OK', [('Content-type','text/plain')])
    return [content]

server = wsgiref.make_server('localhost', 8000, hello_app)
server.serve_forever()
}}}
from webob import Request

from webob import Request

ObpLovelyPython/AbtWebModules (last edited 2009-12-25 07:15:55 by localhost)