An example app – Automatic (de)serialisation – An example app



An example app – Automatic (de)serialisation – An example app

0 0


pico-presentation

A presentation on building web apps with Pico

On Github fergalwalsh / pico-presentation

Note

This is a presentation about Pico but also a web app using Pico.

To fully experience this presentation you should:

  • pip install pico
  • cd pico-presentation-master/
  • python -m pico.server
  • Open localhost:8800
  • Click any Run button you see!

It seems you are not running this presentation with the pico server!

Please go back to the previous slide to learn how to run this properly

Building web applications with Pico

Fergal Walsh

Python Developer @ Metglobal

github.com/fergalwalsh/pico

Contents

  • Quick intro
  • Some examples
    • Automatic & custom (de)serialisation
    • Classes
    • Authentication
    • Sessions
    • Streaming
  • The Protocol
  • Alternative clients
  • Deployment

An example app

A simple notebook

A plain python module


                

A web service


                


                    
// This slide
pico.load_as("examples.notes2", "notes2")
run.onclick = function(){
    notes2.read(1, displayMessage)
}

Run

Write a Python module:


                

Call your Python functions from Javascript:


                

Automatic (de)serialisation

Strings


                    
// This slide
pico.load_as("examples.example", "example")
run.onclick = function(){
    example.hello("World", displayMessage)
}

Run

Strings


                    
// This slide
pico.load_as("examples.example1", "example1")
run.onclick = function(){
    example1.search("world", displayMessage)
}

Run

Numbers


                    
// This slide
pico.load_as("examples.example2", "example2")
run.onclick = function(){
    example2.multiply(6, 7, displayMessage)
}

Run

Dates


                    
// This slide
pico.load_as("examples.example3", "example3")
run.onclick = function(){
    var today = new Date()
    example3.days_until_new_year(today, displayMessage)
}

Run

Files


                    
// This slide
pico.load_as("examples.example5", "example5")
slide.querySelector("input").onchange = function(){
    var file = this.files[0]
    example5.upload_image(file, function(filename){
        slide.querySelector("img").src = filename
    }).done(displayMessage)
}

Custom Types


                    
// This slide
pico.load_as("examples.example6", "example6")
run.onclick = function(){
    example6.where(function(point){
        displayMessage(JSON.stringify(point))
    })
}

Run

An example app

A simple notebook


                

Classes


                    
pico.load_as("examples.notebook", "notebook")
run.onclick = function(){
    var nb = new notebook.Notebook("fergalwalsh")
    nb.notes_list(showNoteList)
}

Authentication


                    
pico.load_as("examples.notebook", "notebook")
run.onclick = function(){
    var nb = new notebook.Notebook()
    pico.authenticate(nb, "fergalwalsh", "test")
    nb.notes_list(showNoteList)
}

Basic Authentication with CURL

curl --user "fergalwalsh:test" \
http://localhost:8800/pico/notebook/Notebook/notes_list/

Streaming Responses


                    
pico.load_as("examples.streaming", "streaming")
run.onclick = function(){
    streaming.notifications(function(time){
        var d = new Date(time)
        displayMessage(d.toLocaleString())
    })
}

Run

Alternative Clients

client.py: A python Pico client (for RPC)

Any http lib: the protocol is easier than REST!Easily call your api from iOS, Android

cURL

The Protocol

Function Call URL
pico.load("example")
http://localhost:8800/pico/example
example.hello()
/pico/example/hello
example.hello("world")
/pico/example/hello?message="world"
nb = notes.Notebook()
 ! 
nb.read(10)
/pico/notes/Notebook/read?id=10
nb.save(note)
/pico/notes/Notebook/save?note='{"id":10, "content": "Bla bla"}'
nb = notes.Notebook("stuff")
 ! 
nb.read(10)
notes/Notebook/read?id=10&_init=["stuff"]

GET or POST?

What ever you want! This is not REST!

pico.js usually uses POST, except when a function call has no arguments

Deployment

Development server: python -m pico.server

WSGI handler: e.g. Apache mod_wsgi, uwsgi, gunicorn, gevent.pywsgi

Google App Engine

django-pico: use Pico as part of a larger Django project

Dependencies

Python >= 2.7 < 3.0 (untested with Python 3.x)

gevent (Optional, needed to use streaming functions)

No other external Python libraries required.

No external JS libraries required (jQuery not required!)

</presentation>

pip install pico github.com/fergalwalsh/pico