swinf是一个用于小型web应用程序的简单微框架,除了python标准的freety之外没有其他依赖项。
swinf的Python项目详细描述
swinf是一个用于小型web应用程序的简单微框架,除了python标准的freety之外没有其他依赖项。
它提供了一个内置的http服务器和一个简单的路由绑定机制。
命令
运行命令:swinf-admin.py startproject newprojectswinf将创建一个项目目录。
在当前项目目录中,有一个main.py和三个子目录:
控制器:
containing controllers.
视图:
containing view template files. subdirecties: static/: contains static files static/images: images here static/style: css files here static/script: js files here static/files: other static files here
型号:
you can put your database controlling code here.
您可以在controller目录中添加一些控制器,然后运行main.py,它就会工作。
模板
目前,swinf有一个名为simple template的简单模板引擎。
第三方物流语法如下
<!-- in a tpl file --> {% # multiline code def bold_wrapper(txt): return "<b>" + txt + "</b>" endef %} %% # sigleline code %% if name: <h1> hello {{name}}!</h1> %% else: <h1> Hello World!</h1> %% endif <ul> %% for i in range(100): <li>no: {{i}}</li> %% endfor </ul>
要使用模板,可以使用如下代码:
fromswinf.templateimporttemplate# pass tpl sourcehtml=template("<h1>hello {{name}}",name='world')# pass a tpl filehtml=template(path='index.tpl',name='world')
示例
在swinf中,没有类似于配置文件的url.py,而是有两种简单的路由配置方式:
- 类似瓶子的路径绑定机制
fromswinf.swinfimportrunfromswinf.selectorimportroute# a simple controller@route('/hello/:name')defhello(name):return'<h1>Hello %s!</h1>'%name.title()run(host='localhost',port=8080)
- 更简单的路由绑定机制
# module1.pyfromswinf.selectorimporthandler# --------- your code here -----------@handler("GET")defhello():return'<h1>Hello</h1>'@handler("GET")defworld():return'<h1>World</h1>'
这将自动将路由/module1/hello绑定到处理程序controller.module1.hello和/module1/world绑定到处理程序controller.module1.world。
您不必手动添加路由。