LinuxDay2013 - Web2py: make the web easier

Preview:

DESCRIPTION

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

Citation preview

Make the web easier

Davide MarzioniLinux Day San Severino 2013

Web2pyWeb2py

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

Linux Day San Severino 2013

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

Linux Day San Severino 2013

Interazione web

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.

Linux Day San Severino 2013

Python - Chi lo usa

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#

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#

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

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

Linux Day San Severino 2013

Flusso dati Web2py

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

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()

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)

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}}

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>

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}}

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)

Linux Day San Severino 2013

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

{{=form}}

Linux Day San Severino 2013

Conclusione

• Domande?

• Commenti?

• Chiarimenti?