-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.py
92 lines (69 loc) · 2.46 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
import web
from web.contrib.template import render_jinja
from webpy_debugtoolbar import DebugToolbarExtension
# some settings
PROJ_ROOT = os.path.dirname(__file__)
TEMPLATE_DIR = os.path.join(PROJ_ROOT, 'templates')
render = render_jinja(TEMPLATE_DIR)
urls = (
'/', 'index',
)
web.config.debug = True
web.config.DEBUG_TB_PANELS = {
'DEBUG_TB_INTERCEPT_REDIRECTS': True,
'DEBUG_TB_PANELS': (
'webpy_debugtoolbar.panels.versions.VersionDebugPanel',
'webpy_debugtoolbar.panels.profiler.ProfilerDebugPanel',
'webpy_debugtoolbar.panels.timer.TimerDebugPanel',
'webpy_debugtoolbar.panels.headers.HeaderDebugPanel',
'webpy_debugtoolbar.panels.request_vars.RequestVarsDebugPanel',
'webpy_debugtoolbar.panels.logger.LoggingPanel',
'webpy_debugtoolbar.panels.sqla.SQLAlchemyDebugPanel',
)
}
# SQLAlchemy settings
engine = create_engine(
'sqlite:///tutorial.db',
encoding='utf8',
echo=False,
)
# db means the session of sqlalchemy.(To be different from session of web.py)
db = scoped_session(sessionmaker(bind=engine))
# some models
Base= declarative_base()
class Account(Base):
__tablename__ = 'account'
uid = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
Base.metadata.create_all(engine)
# add urls and global static file handler for webpy debug toolbar
app_wrapper = DebugToolbarExtension.app_wrapper(urls, globals())
web.config.app = app = web.application(*app_wrapper)
if web.config.debug:
DebugToolbarExtension(app)
# for session panel
web.config.session = web.session.Session(app, web.session.DiskStore('session'))
web.config.session.test_session = ['hello', 'foo'] # just for testing
# for logging panel
web.config.proj_root = PROJ_ROOT
# for SQLAlchemy panel
web.config.SECRET_KEY = 'webpy_debugtoolbar_secret' # whatever string u like
web.config.engine = engine
class index:
def GET(self):
# test logger
import logging
logging.error('hello world.')
# test SQLAlchemy
count = db.query(Account.uid).count()
name='webpy name %s' % (count + 1)
db.query(Account.uid).filter_by(uid=1).first()
db.add(Account(name=name))
db.commit()
return render.index(locals())
if __name__ == "__main__":
app.run()