![Page 2: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/2.jpg)
web2py start in 2007• one of the most popular web frameworks
• 2011 Bossie Award
• 2012 Technology of the Year Award
• 2 books
• about 8000+ registered users
![Page 3: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/3.jpg)
How did we get here?
![Page 4: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/4.jpg)
Priorities
• Ease of use (+ expressive, - maintenance)
• Security (no choices to developers)
• Batteries included
• Convention over configuration
• Always backward compatible (since 2007)
![Page 5: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/5.jpg)
web server DAL + database web IDEssl enabled SQLite design, deploy, manage
web2py
No installation. No configuration. Just Unzip and Click!
.zip
html, xml, json, rss, ics, pdf, rtf, xmlrpc, jsonrpc, soap,
ldap, pam, janrain, dropbox, google, CAS, OpenID, oauth 1&2, x509
marmin, markdown, google wallet, authorize.net, stripe.com
memcache, redis twitter bootstrap
auto-migrations
Batteries included
ticketing system
job scheduler
![Page 6: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/6.jpg)
Convention over configuration (a la RoR)
def index(): return "Hello world"
applications/appname/controllers/default.py
![Page 7: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/7.jpg)
Models
!!! book - title - authors - description - cover_image!!!
![Page 8: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/8.jpg)
Models
!!db.define_table( 'book', Field('title'), Field('authors'), Field('description'), Field('cover_image'))!!!
![Page 9: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/9.jpg)
Models
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'))!!!
![Page 10: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/10.jpg)
Insert example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!
![Page 11: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/11.jpg)
Insert example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!db.book.insert(title='web2py')
![Page 12: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/12.jpg)
Select example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!books = db(db.book.title=='web2py').select()
![Page 13: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/13.jpg)
Form example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!create_form = SQLFORM(db.book).process()
![Page 14: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/14.jpg)
Crud example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!edit_form = SQLFORM(db.book,1).process()
![Page 15: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/15.jpg)
Grid Example
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!grid = SQLFORM.grid(db.book)
![Page 16: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/16.jpg)
Auditing
!!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!auth.enable_record_versioning(db) # auditing
![Page 17: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/17.jpg)
Portability
db = DAL('sqlite://storage.db')!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!...
![Page 18: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/18.jpg)
Portability
db = DAL('postgres://user:password@localhost/test')!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!...
![Page 19: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/19.jpg)
Google App Engine
db = DAL('google:datastore')!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!...
![Page 20: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/20.jpg)
Mongo DB
db = DAL('mongodb://user:password@server:port/db')!db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)!...
![Page 21: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/21.jpg)
MVC
from gluon.tools import Authdb = DAL('sqlite://storage.db')auth = Auth(db)auth.define_tables()db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)
models/db.py
![Page 22: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/22.jpg)
MVC
!def index(): return "Hello world"!
controllers/default.py
![Page 23: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/23.jpg)
MVC
!!def index(): return {'g': SQLFORM.grid(db.book)}!
controllers/default.py
![Page 24: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/24.jpg)
MVC
@auth.requires_login()def index(): return {'g': SQLFORM.grid(db.book)}!def books(): return {'rows': db(db.book).select().as_list()}!
controllers/default.py
![Page 25: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/25.jpg)
MVC
{{extend 'layout.html'}}<h1>Interface to manage my books</h1><div> {{=g}}</div>
views/default/index.html
![Page 26: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/26.jpg)
Complete program
from gluon.tools import Authdb = DAL('sqlite://storage.db')auth = Auth(db)auth.define_tables()db.define_table( 'book', Field('title',requires=IS_NOT_EMPTY()), Field('authors','list:string'), Field('description','text'), Field('cover_image','upload'), auth.signature)
@auth.requires_login()def index(): return {'g': SQLFORM.grid(db.book)}
{{extend 'layout.html'}}<h1>Interface to manage my books</h1><div> {{=g}}</div>
views/default/index.py
controllers/default.pymodels/db.py
![Page 27: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/27.jpg)
Access Control
@auth.requires_login()@auth.requires_membership(‘admin’)@auth.requires_permissions(‘run’,’silly functions’)def index(): return {}!!!
![Page 28: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/28.jpg)
Federated Auth# app1/modeles/db.pyauth = Auth(db)
# app2/modeles/db.pyauth = Auth(db, cas_provider=‘http://…/app1/default/user/cas')
![Page 29: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/29.jpg)
appadmin/manage/authauth.settings.auth_manager_role = 'manager'if auth.user and not db.auth_group(role='manager'): auth.add_membership(auth.add_group('manager'))
![Page 30: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/30.jpg)
delegating [email protected](…)def index(): link = A(‘click me’, _href=URL(‘other_page’, user_signature=True)) return {‘link’:link}[email protected]_signature()def other_page(): …
![Page 31: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/31.jpg)
from gluon.scheduler import Schedulerscheduler = Scheduler(db)!def f(n): return sum(i for i in range(n))!scheduler.queue_task(f, pvars=dict(n=8), start_time=request.now, repeats=5, period=10, timeout=60, retry_failed=3)
Task scheduler
Task scheduler
web2py.py -K app web2py.py -K app web2py.py -K app
![Page 32: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/32.jpg)
Design
Link
Form
table or
grid
Ajax page
Reactive page
data: args,vars,session,cookies
![Page 33: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/33.jpg)
Ajax Widgets def index(): return {}!def grid(): return SQLFORM.grid(db)
Ajax components
....<div>{{=LOAD('default','grid',ajax=True)}}</div>...
views/default/index.py
![Page 34: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/34.jpg)
def index(): return auth.wiki()
The built-in wiki
is it a wiki? is it a blog? is it a CMS? is it a way to set permissions on content? is it a way to store pages in db? is it a way to store code in db?
![Page 35: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/35.jpg)
auth.add_memberhsip(auth.add_group(role=‘wiki_author’))auth.add_memberhsip(auth.add_group(role=‘wiki_editor’))!return auth.wiki(render=‘markmin’)return auth.wiki(render=‘html’)!return auth.wiki(manage_permissions = True)return auth.wiki(force_prefix=‘%s-‘ % auth.user.username)
The built-in wiki
![Page 36: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/36.jpg)
return auth.wiki(extra=dict(x = lambda text: ‘x’+text+’x’))!MARKMIN(“…”, extra=dict(par = lambda text: ‘(’+text+’)’)) ``hello``:par -> (hello)
The built-in wiki (extra)
![Page 37: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/37.jpg)
return auth.wiki(env = dict(double=lambda a: a+a))!MARKMIN(“…”, env = dict(double=lambda a: a+a)) @{double:hello} -> hellohello
The built-in wiki (env)
![Page 38: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/38.jpg)
def api(): if request.env.request_method == ‘GET’: return db(db.book).select().as_json() if request.env.request_method == ‘POST’ return {'id': db.book.insert(title=request.vars.title)} raise HTTP(400)
REST API
Restful API
![Page 39: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/39.jpg)
@response.restful()def api(): def GET(): return db(db.book).select().as_json() def POST(title): return {'id': db.book.insert(title=title)} return locals()
REST API
Restful API
![Page 40: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/40.jpg)
def api(): from gluon.contrib.hypermedia import Collections! policy = {‘book’:{’GET’:{},‘POST’:{},‘PUT’:{},‘DELETE’:{}}! return Collection(db).process(request,response,policy)
Collection + JSONSelf-Documenting Hypermedia API
![Page 41: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/41.jpg)
def pay(): from gluon.contrib.stripe import StripeForm form = StripeForm( pk='pk_test_6pRNASCoBOKtIshFeQd4XMUh', sk='sk_test_BQokikJOvBiI2HlWgH4olfQ2', amount=int(150), # $1.5 (amount is in cents) description=“you bought a candy!”) if form.process().accepted: # db.purchase.insert(receipt_id = form.response[‘id’]) redirect(URL(‘thankyou’)) return dict(form=form)
Stripe.com
![Page 42: web2py tutorial - DePaul Universitymdp.cti.depaul.edu/.../get/DePy2016/depy2016-web2py-tutorial.pdf · web2py start in 2007 • one of the most popular web frameworks! • 2011 Bossie](https://reader031.vdocument.in/reader031/viewer/2022021809/5c1d77a709d3f2826b8c5dc7/html5/thumbnails/42.jpg)
Conclusions and Challenges
• web2py is 8 years old and very mature
• The world has changed
• From Python 2 to Python 3
• Client side programming more important
• Docker best way to deploy
• JS? CSS?