Size: 1821
Comment:
|
Size: 8382
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
||'''status'''|| 草稿|校对|正式 || AuthorName|| 完成度:15%|| | ##草稿|校对|正式 ||'''status'''|| 校对 || HuangYi,ZoomQuiet|| 完成度:15%|| |
Line 39: | Line 40: |
== 为啥介多框架?! == 还有人站着说话不腰痛的分析"[http://xlp223.yculblog.com/post.1634226.html 为何有如此多的python web 框架?]" {{{脚注: 访问地址:http://xlp223.yculblog.com/post.1634226.html 精巧地址:http://tinyurl.com/6yajuo }}} * E文出处:[http://bitworking.org/news/Why_so_many_Python_web_frameworks Why so many Python web frameworks?] {{{脚注: 访问地址:http://bitworking.org/news/Why_so_many_Python_web_frameworks 精巧地址:http://tinyurl.com/6kv9j3 }}} {{{注意: 在上述文章中,作者为了展示任何人可以通过Python快速创建自个儿的Web应用框架, 当场使用 9个文件创建并运行了一个含有足够功能的框架! 详细请参看本章最后的小结部分 }}} === Joe Gregorio的超级框架 === 组成:: 1. model.py{{{#!python from sqlalchemy import Table, Column, String import dbconfig entry_table = Table('entry', dbconfig.metadata, Column('id', String(100), primary_key=True), Column('title', String(100)), Column('content', String(30000)), Column('updated', String(20), index=True) ) }}} 1. dbconfig.py{{{#!python from sqlalchemy import * metadata = BoundMetaData('sqlite:///tutorial.db') }}} 1. manage.py{{{#!python import os, sys def create(): from sqlalchemy import Table import model for (name, table) in vars(model).iteritems(): if isinstance(table, Table): table.create() def run(): import urls if os.environ.get("REQUEST_METHOD", ""): from wsgiref.handlers import BaseCGIHandler BaseCGIHandler(sys.stdin, sys.stdout, sys.stderr, os.environ).run(urls.urls) else: from wsgiref.simple_server import WSGIServer, WSGIRequestHandler httpd = WSGIServer(('', 8080), WSGIRequestHandler) httpd.set_app(urls.urls) print "Serving HTTP on %s port %s ..." % httpd.socket.getsockname() httpd.serve_forever() if __name__ == "__main__": if 'create' in sys.argv: create() if 'run' in sys.argv: run() }}} 1. view.py{{{#!python import selector import view urls = selector.Selector() urls.add('/blog/', GET=view.list) urls.add('/blog/{id}/', GET=view.member_get) urls.add('/blog/;create_form', POST=view.create, GET=view.list) urls.add('/blog/{id}/;edit_form', GET=view.member_get, POST=view.member_update) }}} 1. model.py{{{#!python import robaccia import model def list(environ, start_response): rows = model.entry_table.select().execute() return robaccia.render(start_response, 'list.html', locals()) def member_get(environ, start_response): id = environ['selector.vars']['id'] row = model.entry_table.select(model.entry_table.c.id==id).execute().fetchone() return robaccia.render(start_response, 'entry.html', locals()) def create(environ, start_response): pass def create_form(environ, start_response): pass def member_edit_form(environ, start_response): pass def member_update(environ, start_response): pass }}} 1. robaccia.py{{{#!python import kid import os extensions = { 'html': 'text/html', 'atom': 'application/atom+xml' } def render(start_response, template_file, vars): ext = template_file.rsplit(".") contenttype = "text/html" if len(ext) > 1 and (ext[1] in extensions): contenttype = extensions[ext[1]] template = kid.Template(file=os.path.join('templates', template_file), **vars) body = template.serialize(encoding='utf-8') start_response("200 OK", [('Content-Type', contenttype)]) return [body] }}} 1. list.html{{{ <?xml version="1.0" encoding="utf-8"?> <html xmlns:py="http://purl.org/kid/ns#>"> <head> <title>A Robaccia Blog</title> </head> <div py:for="row in rows.fetchall()"> <h2>${row.title}</h2> <div>${row.content}</div> <p><a href="./${row.id}/">${row.updated}</a></p> </div> </html> }}} |
|
Line 40: | Line 175: |
1. 创建数据库{{{ ~$ python manage.py create }}} 1. 初始化数据{{{ ~$ python Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import model >>> i = model.entry_table.insert() >>> i.execute(id='first-post', title="Some Title", content="Some pithy text...", updated="2006-09-01T01:00:00Z") >>> i.execute(id='second-post', title="Moving On", content="Some not so pithy words...", updated="2006-09-01T01:01:00Z") }}} 1. 独立运行{{{ ~$ python manage.py run Serving HTTP on 0.0.0.0 port 8080 ... }}} 1. 通过 cgi 在各种Web服务器中运行{{{ #!/usr/bin/python2.4 import manage manage.run() }}} 能力:: * 通过 SQLAlchemy 进行对象化高效数据库操作的 * SQLAlchemy官方网站 http://www.sqlalchemy.org/ * 使用 WSGI(Web 服务器网关接口)模式运行的 * WSGI官方网站: http://www.wsgi.org/wsgi/What_is_WSGI * 精巧地址: http://bit.ly/3IkgU4 * 可对URL进行映射管理的 * 使用 Kid 模板系统,高效输出数据的 * Kid官方网站 http://www.kid-templating.org/ * 精巧地址: http://bit.ly/2YDxPd * 全功能Web应用框架! == 使用 == |
|
Line 44: | Line 219: |
问题:: | == 问题 == |
Line 49: | Line 224: |
探讨:: | == 探讨 == |
Line 52: | Line 227: |
Line 53: | Line 229: |
小结:: | Eurasia3 前面已经 * 用 895 行单文件实现了 ZODB * 下载地址: http://code.google.com/p/eurasia/source/browse/trunk/Eurasia/shelve2.py * 精巧地址: http://bit.ly/1yzuqZ * 这次更过分了, Mako 只有 377 行。把类似于 Mako 的模板读进来, 解析成语法树, 然后编译成 Python 程序 (和 Mako 一样是编译型模板) * 下载地址: http://code.google.com/p/eurasia/source/browse/trunk/Eurasia/template.py * 精巧地址: http://bit.ly/1rjgdz * 081020(16:24:22) 沈崴-wileishn: 支持 Mako 的 <%def name=""> <%call expr=""> <% python code %> ${...} {{{ from template import compile, Template s = '''\ <%def name="test2(a, b)"> ${caller.test3()} or ${context.get('test3', None)()} %if a: ${a} %elif b: ${b} %else: <%write('hello')%> %endif %for i in xrange(100): ${str(i)} %endfor </%def> <%def name="test(c, d)"> <%call expr="test2(1, 2)"> <%def name="test3()"> <% x = 1 %> hello world! in test3 </%def> </%call> </%def> ''' print compile(s) m = Template(s) print m.test('aaa', 'bbb') }}} shelve2 已经在实用了。这个模板会被用在建立 Eurasia3 项目主站上 == 小结 == |
Line 66: | Line 291: |
创建 by -- ::-- ZoomQuiet [[[DateTime(2008-10-05T14:54:06Z)]]] | 创建 by -- ::-- ["kongove"] [[[DateTime(2008-10-30T11:10:42Z)]]] |
PCS304 Web应用框架纵览
- 概述
- 简洁的介绍知识点的来由
Python Web应用框架纵论 :: +-- 导论 | +-- 现状 | \-- 为什么Python 中有这么多框架? +-- 分类 | +-- 如何来理解各种框架? | +-- 框架的框架 | +-- 轻型框架 | +-- 一站式框架 | \-- 模板系统 +-- 细说(按照历史顺序,选择经典框架来介绍,没有PCS独立章节的,和故事没有直接提及的) | +-- Zope/Plone (请潘俊勇撰写) | +-- Quixote (请 阿北或是朞其它豆瓣成员 撰写) | +-- Quixote (请 阿北 撰写) | +-- Django (HY自写) | \-- UliWeb (请Limodou撰写) +-- 选择 | +-- 个人 | +-- 团队 | \-- 企业 \-- 小结 \-- 选择的痛苦
- 简洁的介绍知识点的来由
为啥介多框架?!
还有人站着说话不腰痛的分析"[http://xlp223.yculblog.com/post.1634226.html 为何有如此多的python web 框架?]" {{{脚注: 访问地址:http://xlp223.yculblog.com/post.1634226.html 精巧地址:http://tinyurl.com/6yajuo }}}
E文出处:[http://bitworking.org/news/Why_so_many_Python_web_frameworks Why so many Python web frameworks?]
{{{脚注: 访问地址:http://bitworking.org/news/Why_so_many_Python_web_frameworks 精巧地址:http://tinyurl.com/6kv9j3 }}}
{{{注意: 在上述文章中,作者为了展示任何人可以通过Python快速创建自个儿的Web应用框架, 当场使用 9个文件创建并运行了一个含有足够功能的框架! 详细请参看本章最后的小结部分 }}}
Joe Gregorio的超级框架
- 组成
model.py
dbconfig.py
manage.py
1 import os, sys 2 3 def create(): 4 from sqlalchemy import Table 5 import model 6 for (name, table) in vars(model).iteritems(): 7 if isinstance(table, Table): 8 table.create() 9 10 def run(): 11 import urls 12 if os.environ.get("REQUEST_METHOD", ""): 13 from wsgiref.handlers import BaseCGIHandler 14 BaseCGIHandler(sys.stdin, sys.stdout, sys.stderr, os.environ).run(urls.urls) 15 else: 16 from wsgiref.simple_server import WSGIServer, WSGIRequestHandler 17 httpd = WSGIServer(('', 8080), WSGIRequestHandler) 18 httpd.set_app(urls.urls) 19 print "Serving HTTP on %s port %s ..." % httpd.socket.getsockname() 20 httpd.serve_forever() 21 22 if __name__ == "__main__": 23 if 'create' in sys.argv: 24 create() 25 if 'run' in sys.argv: 26 run()
view.py
model.py
1 import robaccia 2 import model 3 4 def list(environ, start_response): 5 rows = model.entry_table.select().execute() 6 return robaccia.render(start_response, 'list.html', locals()) 7 8 def member_get(environ, start_response): 9 id = environ['selector.vars']['id'] 10 row = model.entry_table.select(model.entry_table.c.id==id).execute().fetchone() 11 return robaccia.render(start_response, 'entry.html', locals()) 12 13 def create(environ, start_response): 14 pass 15 def create_form(environ, start_response): 16 pass 17 def member_edit_form(environ, start_response): 18 pass 19 def member_update(environ, start_response): 20 pass
robaccia.py
1 import kid 2 import os 3 4 extensions = { 5 'html': 'text/html', 6 'atom': 'application/atom+xml' 7 } 8 9 def render(start_response, template_file, vars): 10 ext = template_file.rsplit(".") 11 contenttype = "text/html" 12 if len(ext) > 1 and (ext[1] in extensions): 13 contenttype = extensions[ext[1]] 14 15 template = kid.Template(file=os.path.join('templates', template_file), **vars) 16 body = template.serialize(encoding='utf-8') 17 18 start_response("200 OK", [('Content-Type', contenttype)]) 19 return [body]
list.html
<?xml version="1.0" encoding="utf-8"?> <html xmlns:py="http://purl.org/kid/ns#>"> <head> <title>A Robaccia Blog</title> </head> <div py:for="row in rows.fetchall()"> <h2>${row.title}</h2> <div>${row.content}</div> <p><a href="./${row.id}/">${row.updated}</a></p> </div> </html>
- 使用
创建数据库
~$ python manage.py create
初始化数据
~$ python Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import model >>> i = model.entry_table.insert() >>> i.execute(id='first-post', title="Some Title", content="Some pithy text...", updated="2006-09-01T01:00:00Z") >>> i.execute(id='second-post', title="Moving On", content="Some not so pithy words...", updated="2006-09-01T01:01:00Z")
独立运行
~$ python manage.py run Serving HTTP on 0.0.0.0 port 8080 ...
通过 cgi 在各种Web服务器中运行
import manage manage.run()
- 能力
- 通过 SQLAlchemy 进行对象化高效数据库操作的
SQLAlchemy官方网站 http://www.sqlalchemy.org/
- 使用 WSGI(Web 服务器网关接口)模式运行的
WSGI官方网站: http://www.wsgi.org/wsgi/What_is_WSGI
精巧地址: http://bit.ly/3IkgU4
- 可对URL进行映射管理的
- 使用 Kid 模板系统,高效输出数据的
Kid官方网站 http://www.kid-templating.org/
精巧地址: http://bit.ly/2YDxPd
- 全功能Web应用框架!
- 通过 SQLAlchemy 进行对象化高效数据库操作的
使用
- 以实用的小例子,快速介绍知识点涉及技术的应用方式方法
- 根据实例进行语法的快速讲解;注意给出资料指引
- 经典代码举例
问题
- 指出知识点应用中常常遇见的问题,和解决思路
- 根据实用体验,说明如何理解以及实际使用时的理性思考方向
探讨
- 进一步介绍相关的或是深入的知识领域...
- 组织实用的技巧类代码
Eurasia3 前面已经
- 用 895 行单文件实现了 ZODB
- 这次更过分了, Mako 只有 377 行。把类似于 Mako 的模板读进来, 解析成语法树, 然后编译成 Python 程序 (和 Mako 一样是编译型模板)
- 081020(16:24:22) 沈崴-wileishn:
支持 Mako 的 <%def name=""> <%call expr=""> <% python code %> ${...}
from template import compile, Template s = '''\ <%def name="test2(a, b)"> ${caller.test3()} or ${context.get('test3', None)()} %if a: ${a} %elif b: ${b} %else: <%write('hello')%> %endif %for i in xrange(100): ${str(i)} %endfor </%def> <%def name="test(c, d)"> <%call expr="test2(1, 2)"> <%def name="test3()"> <% x = 1 %> hello world! in test3 </%def> </%call> </%def> ''' print compile(s) m = Template(s) print m.test('aaa', 'bbb')
shelve2 已经在实用了。这个模板会被用在建立 Eurasia3 项目主站上
小结
- 总体回顾,给出相关阅读和思考指引
反馈
创建 by -- ::-- ["kongove"] [DateTime(2008-10-30T11:10:42Z)]