Criar um virtualenv e entrar nele
O virtualenv
script de criação de CPython pode ser usado para PyPy
$ mkdir experiment
$ virtualenv -p $(which pypy) ./experiment
$ source ./experiment/bin/activate
Agora pip
está disponível
$ pip install Pillow bottle
Escrevendo um hello world com uma interface da web
import bottle
@bottle.route("/")
def hello():
return "hello, world"
if __name__ == "__main__":
bottle.run(port="8000")
Usando a biblioteca integrada Greenlet of PyPy
@greenlet.greenlet
def second():
print "now you are in the second greenlet"
first.switch()
@greenlet.greenlet
def first():
second.switch()
print "the return of the first greenlet"
first.switch()
Escrevendo um hello world com PIL
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from StringIO import StringIO
from bottle import route, response, debug, run
from PIL import Image, ImageDraw
def draw_text(text):
background = Image.new("RGBA", (500, 20), "white")
draw = ImageDraw.Draw(background)
draw.text((5, 5), text, fill="red")
return background
def get_image_bytes(image, format="PNG"):
image_buffer = StringIO()
image.save(image_buffer, format=format)
return image_buffer.getvalue()
@route("/:name")
@route("/")
def hello(name="world"):
image = draw_text("hello, %s" % name.lower())
image_bytes = get_image_bytes(image, "PNG")
response.content_type = "image/png"
return image_bytes
if __name__ == "__main__":
debug(True)
run(reloader=True, port=8001)