Masking the web.py magic
Project description
Pythonic web.py
===========
Making `web.py` request handling more pythonic.
```python
import lessweb
def hello(ctx, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
urls = ('GET', '/(.*)', hello)
app = lessweb.application(urls, locals())
```
All those magical web.* are now available in context variable.
```python
def hello(ctx):
user_data = ctx.input(color=[])
if 'color' not in ctx.cookies and not user_data.color:
ctx.setcookies('color', 'blue')
return 'Hello, from ' + ctx.env.get('REMOTE_HOST') or ctx.ip
urls = ('GET', '/', hello)
import lessweb
app = lessweb.application(urls, locals())
```
Only a part of web.py request handling has been masked with `less.web`. Rest of them are required to
do what they did already.
```python
import web
import lessweb
def redirect_to_referer(ctx):
referer = ctx.env.get('HTTP_REFERER', 'http://webpy.org')
raise web.seeother(referer)
urls = ('GET', '/', redirect_to_referer)
app = lessweb.application(urls, locals())
```
It's implemented as a tiny pythonic wrapper around `web.py` request handling.
The idea is to allow all request/response manipulation through `ctx`(context) of the handler function rather than module(`web`) level functions. `ctx` is basically `web.ctx` of `web.py`, which is already used to set and access request context. It also wraps other module level functionality of `web.py` such as `header`, `input`, `cookies`, `sessions` to yield a more pythonic interface.
To use sessions you have to configure session store in config as follows.
```python
web.config.sessionstore = web.session.DiskStore('sessions') # for disk store, other stores can be used similarly.
```
===========
Making `web.py` request handling more pythonic.
```python
import lessweb
def hello(ctx, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
urls = ('GET', '/(.*)', hello)
app = lessweb.application(urls, locals())
```
All those magical web.* are now available in context variable.
```python
def hello(ctx):
user_data = ctx.input(color=[])
if 'color' not in ctx.cookies and not user_data.color:
ctx.setcookies('color', 'blue')
return 'Hello, from ' + ctx.env.get('REMOTE_HOST') or ctx.ip
urls = ('GET', '/', hello)
import lessweb
app = lessweb.application(urls, locals())
```
Only a part of web.py request handling has been masked with `less.web`. Rest of them are required to
do what they did already.
```python
import web
import lessweb
def redirect_to_referer(ctx):
referer = ctx.env.get('HTTP_REFERER', 'http://webpy.org')
raise web.seeother(referer)
urls = ('GET', '/', redirect_to_referer)
app = lessweb.application(urls, locals())
```
It's implemented as a tiny pythonic wrapper around `web.py` request handling.
The idea is to allow all request/response manipulation through `ctx`(context) of the handler function rather than module(`web`) level functions. `ctx` is basically `web.ctx` of `web.py`, which is already used to set and access request context. It also wraps other module level functionality of `web.py` such as `header`, `input`, `cookies`, `sessions` to yield a more pythonic interface.
To use sessions you have to configure session store in config as follows.
```python
web.config.sessionstore = web.session.DiskStore('sessions') # for disk store, other stores can be used similarly.
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
lessweb.py-0.1.1.tar.gz
(2.0 kB
view details)
File details
Details for the file lessweb.py-0.1.1.tar.gz
.
File metadata
- Download URL: lessweb.py-0.1.1.tar.gz
- Upload date:
- Size: 2.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4f143d76aa8ffa0188d0111ffcf78437b0623fd14e4bbdf6aed6196802a1382 |
|
MD5 | a2f4b7dc36c32214ddf6ec0e0d6be6f2 |
|
BLAKE2b-256 | 374a27711ec1657d602b928916f05c9ca48b4d7a6b17962f8a3d40b2b986fb92 |