42
web2py tutorial [email protected]

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

Embed Size (px)

Citation preview

Page 1: 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

web2py tutorial

[email protected]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@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

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

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

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?