-
Notifications
You must be signed in to change notification settings - Fork 1
/
bottle_example.py
37 lines (31 loc) · 1.02 KB
/
bottle_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from bottle import run, route, request, get, post, static_file, redirect
from gluino import wrapper, DAL, Field, SQLFORM, cache, IS_NOT_EMPTY
import time
# configure the gluino wrapper
wrapper.debug = True
wrapper.redirect = lambda status,url: redirect(url)
# create database and table
db=DAL('sqlite://storage.sqlite')
db.define_table('person',Field('name',requires=IS_NOT_EMPTY()))
# define action
@get('/')
@post('/')
@wrapper(view='templates/index.html',dbs=[db])
def index():
vars = wrapper.extract_vars(request.forms)
form = SQLFORM(db.person)
if form.accepts(vars):
message = 'hello %s' % form.vars.name
else:
message = 'hello anonymous'
people = db(db.person).select()
people = people.xml()
now = cache.ram('time',lambda:time.ctime(),10)
return locals()
# handle static files
@route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='static')
#start web server
if __name__=='__main__':
run(host='localhost', port=8080, reloader=True)