linuxday2013 - web2py: make the web easier

20
Make the web easier Davide Marzioni Linux Day San Severino 2013 Web2py Web2py

Upload: davide-marzioni

Post on 10-Jun-2015

266 views

Category:

Education


1 download

DESCRIPTION

Web2py presentato al LinuxDay2013 presso l'ITT Divini San Severino Marche Zip allegato: https://www.dropbox.com/s/6ngyavo71jmwxwg/web2py.zip

TRANSCRIPT

Page 1: LinuxDay2013 - Web2py: make the web easier

Make the web easier

Davide MarzioniLinux Day San Severino 2013

Web2pyWeb2py

Page 2: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

About• Laureato in Ingegneria Informatica e dell'automazione

presso l'università politecnica delle Marche• Sviluppatore software• Appassionato di Linux e dell'open source dal 2000

Page 3: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Sommario• L'interazione web• Python• Web2py• Dimostrazioni pratiche

Page 4: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Interazione web

Page 5: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Python• Sintassi chiara e semplice• Facile da imparare• Orientato agli oggetti• Typing dinamico• Multipiattaforma• Modulare ed estendibile (C, C++, C#, Java, .Net)• Utilizzato come linguaggio di scripting

Python rilasciato da Guido van Rossum in 1991. Il linguaggio è basato su un modello aperto e sviluppato dalla comunità e gestito dalla fondazione no-profit Python Software Foundation.

Page 6: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Python - Chi lo usa

Page 7: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

print "Hello World!"using System;namespace HelloWorld{ class Hello { static void Main() { System.Console.WriteLine("Hello World!"); } }

}

Python - Comparazione

PythonC#

Page 8: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

def Add(x, y): result = x + y; return result

a = 5b = 2c = Add(a, b)print "Il risultato e'", c

using System;namespace AddFunction{ class Program { static void Main() { int a = 5; int b = 2; int c = Add(a, b); System.Console.WriteLine( "Il risultato e' {0}", c); }

public int Add(int x, int y) { int result = x + y; return result; } }

}

Python - Comparazione

PythonC#

Page 9: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Web2py

Free open source full-stack framework

for rapid development of

• fast

• scalable

• secure

• portable

database-driven web-based applications.

Written and programmable in Python.http://www.web2py.com

Page 10: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Model - View - ControllerModelRappresentazione dei datiChe dati ho a disposizione, di che tipo sono, ...

ViewPresentazione dei datiChe dati voglio che siano visualizzati e come

ControllerLogica dell'applicazioneCome processo i dati che ho a disposizione

Page 11: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Flusso dati Web2py

Page 12: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Applicazione di esempio - Blog• Creazione di un semplice blog.

• L'applicazione deve poter visualizzare una lista di tutti i messaggi (post).

• Cliccando sul titolo del messaggio si deve poter vedere il post in dettaglio (titolo, testo, data di pubblicazione)

• Chi è registrato al sito può inserire un nuovo messaggio.

NOTA:Le prossime slide non sono una lezione di informatica, ma servono

solo a dimostrare la semplicità e le potenzialità di web2py

Page 13: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Modello - db.pydb = DAL('sqlite://storage.sqlite', pool_size=1)

from datetime import datetimefrom gluon.tools import Auth

auth = Auth(db)auth.define_tables()

db.define_table('post', Field('title', 'string'), Field('content', 'text'), Field('created_at', 'datetime', default=datetime.now()))

db.post.title.requires = IS_NOT_EMPTY()db.post.content.requires = IS_NOT_EMPTY()

Page 14: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Controllore - default.py def index(): lista_post = db().select(db.post.ALL) return dict(lista_post=lista_post)

def view(): post_id = request.args(0) post = db(db.post.id==post_id).select(db.post.ALL).first() return dict(post=post)

Page 15: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Vista - default/index.html{{extend 'layout.html'}}

{{for post in lista_post:}}<h2> {{=A(post['title'], _href=URL('default', 'view',

args=post['id']))}}</h2><h5> {{=post['created_at']}}</h5><p> {{=post['content']}}</p><hr />{{pass}}

Page 16: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Vista - default/view.html{{extend 'layout.html'}}

{{=A('Indietro', _href=URL('default', 'index'), _class='btn')}}

<h1> {{=post['title']}}</h1><h5> {{=post['created_at']}}</h5><p> {{=post['content']}}</p>

Page 17: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Vista - default/index.html - 2{{extend 'layout.html'}}

{{if auth.is_logged_in():}} {{=A('Nuovo', _href=URL('default', 'new'),

_class='btn')}}{{pass}}

{{for post in lista_post:}}<h2> {{=A(post['title'], _href=URL('default', 'view', args=post['id']))}}</h2><h5> {{=post['created_at']}}</h5><p> {{=post['content']}}</p><hr />{{pass}}

Page 18: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Controllore - default.py - 2 def index(): lista_post = db().select(db.post.ALL) return dict(lista_post=lista_post)

def view(): post_id = request.args(0) post = db(db.post.id==post_id).select(db.post.ALL).first() return dict(post=post)

@auth.requires_login() def new(): form = SQLFORM(db.post) if form.process().accepted: response.flash = 'Post inserito!' redirect(URL('default', 'index')) return dict(form=form)

Page 19: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Vista - default/new.html{{extend 'layout.html'}}

{{=form}}

Page 20: LinuxDay2013 - Web2py: make the web easier

Linux Day San Severino 2013

Conclusione

• Domande?

• Commenti?

• Chiarimenti?