一个微型web框架

klar的Python项目详细描述


#klar

一个为好玩而构建的微型web框架

*参数注释
*jsonschema integration

``py
来自klar import app

app=app()

@app.get('/hello/<;name>;')
def hello(name:str,times:int=1):
返回"hello"*times+name
```

times=2'
hello klar
```


"additionalproperties":false,
}

@app.post('/product')
def create(body:product,db):
db.products.insert(body)
return{"ok":true}
````


schemas可以也应该从json或yaml文件导入

`````
````--app.py
`--schemas.json
`````

`````
{…}
}
````

``python
from schemas import product

@app.post('/product')
def create(body:product):
pass
`````

get_db_connection():
conn=somedb(url="localhost:3349")
return conn

import redis
app.provide('cache',(redis.redis,{'host':'localhost')
````

')
def get_article(article廑id:int,db,cache):
传递
````


预定义组件:

*会话
*cookie
*路由器

rest

``python
来自资源导入产品,目录

app=app()

app.resources(产品,目录,前缀="/v1")

db):
返回db.products.insert(body)

shift=10&limit=10
定义查询(shift:int,limit:int,db):
返回db.products.find().skip(shift).limit(shift)

curl-x补丁$host/v1/product/$id-d@body
定义修改(body:product,product-id:str):
返回db.products.update({u id:product},{'$set':body})

curl-x放入$host/v1/product/$id-d@body
def replace(body:product,product-id:str):
返回db.products.update({u id:product-id},body)

curl-x delete$host/v1/product/$id
def destroy(product_id:str):
返回db.products.delete({u id:product_id})
`````

db.products.update({u id:product嫒id},{'$inc':{'likes':1})

curl-x补丁$host/v1/product/$id/like
``````

res):
打印('%s not found'%req.path)
res.body="%s not found on this server"%req.path
`````

{{'$inc'{'loginccount':1}})
`````




```python
``````python
def login(emitter):
emitter.emit('user-login,user id=id)
`````

``后处理


```python
````python
`````````````



>res.body="%s(%s)%(callback,json.dumps(body))
res.headers["content type"]="应用程序/javascript"

@app.get('/resource')->;jsonp:
返回{"key":"value"}
`````


``python
@app.get('/resource')->;(jsonp,etag):
return{"key":"value"}
````

templates.home:
return{"key":"value"}
```

`templates.home`接受一个可选的dict作为参数
它基本上等同于:

``python
@app.get('/'):
return templates.home({"key":"value"}
````

` pip安装pystache`

使用`.mustache`作为扩展名

`````
--模板
--home.mustache
`````
``python
``导入模板.home
```
`
会话

会话取决于"缓存",但是klar没有内置

{'host':'localhost'})
````

``python
@app.post('/login')
def login(body,session):
``检查body.username和body.password
如果找到用户:
session.set('userid',,userid)

@app.post('/login')
def logout(session):
session.destroy()

@app.get('/admin')
def admin(session):
if session.get('userid'):
pass
`````

cookies

``python
cookies.get(key,默认值)
cookies.set(key,值)
cookies.delete(key)

cookies.set(key,value,httponly=true)
cookies.set\u 30天(key,值)
```

'path/to/public/dir')
```

27017
}
````

``python
from pymongo import mongoclient

@app.provide('db')
def db(config):
返回mongoclient(**config.mongo)
````

')
def user(id:str):
pass
````


get a link to previous handler

````
def another_handler(router):
id=3221)
```

`

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

推荐PyPI第三方库


热门话题
java HikariCP连接池Tomcat调试启用   java如何在javascript中访问JSON数组的元素?   jasper使用HttpServlet报告Java空白PDF   用于存储海量数据的java最佳集合   JBPM&springboot组织。流口水。坚持不懈jta。JtaTransactionManager在JNDI位置[java:comp/UserTransaction]未找到任何UserTransaction   java Grails maven插件需要Groovy 2.4.2   获取列表中访问最少的元素(Java 8)   java在IntelliJ中下载cn1lib不起作用   java Akka构造函数通过spring进行布线   swing在Java中创建JList时,如何更改条目的背景颜色?   为什么Java8Lambda允许访问非最终类变量?   javajavax。邮政MessaginException:无法连接到SMTP主机   java Android Webview高度在滚动后增加