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

您不必手动添加路由。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
使用jaxrpc的Java eclipse WebService客户端   java编程方式在对象上写入名称   java Spring批处理:重试后跳过   java Android错误:错误:任务执行失败:应用程序:transformClassesWithDexForDebug'   带有清单文件nullPointerException的java Android元数据   spring Java Quartz调度作业停止运行   JavaMockito:如何在不调用实际方法的情况下,模拟带有参数和无效返回类型的静态方法?   java Tomcat连接池问题无法在关闭的连接上调用方法   java如何交换列表中的项目?   java如何停止线程并通过Toast在线程中正确显示文本?   java为什么连续写入OutputStream时偏移量0不会导致重复字节?   java我无法生成头文件   不兼容的返回类型错误java   修改值后键值对的java Jolt转换规范   java有自动更新Javadoc的工具吗?   java线程如何在ints自身实例类中共享变量   java继承一个非gwt模块   java Hibernate xml配置   使用netty4异步调用的java链接HTTP请求响应