Bottle – Python micro framework
Like Flask, Bottle is a Python micro-framework. It is so micro that it only consists of a single file. Whilst Flask is already a fairly small framework, some developers prefer Bottle, mainly for its easy of setup (single file, no dependencies)
The Bottle home page gives a Hello World example
Local trial
- Create and activate a new virtualenv or use a virtual machine.
- I use a VM for experimenting with Python libraries and frameworks, to keep it separate from my client work
- sudo pip install bottle
- Create hello_world.py with the code from the Bottle home page
- Run the script: python hello_world.py
- Check in your browser: http://localhost:8080/hello/world
- This should show “Hello world!”
- Also try it with your own name, e.g. http://localhost:8080/hello/Coen
On a server – using wsgi
For Python I use WebFaction, which makes it very easy to create new Python applications
In your WebFaction control panel:
- Domains / Websites -> Websites
- Select the domain
- Click on “Add an application” -> Create a new application
- Name: bottle_hello_world
- Category: mod_wsgi
- Type: mod_wsgi / Python 2.7 (note: Bottle also works with Python 3.x)
- URL: /bottle-hello-world
- Save
- Click Save again
- ssh into the host
- cd webapps
- cd <app name> (e.g. bottle_hello_world)
- cd htdocs
- pip install bottle
- Check bottle is installed
- python2.7
- import bottle
- Adapt code for wsgi (based on instructions at http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi) and replace the contents of index.py with:
import os
# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))import bottle
from bottle import route, run, template
application = bottle.default_app()@route(‘/hello/<name>’)
def index(name):
return template(‘<b>Hello {{name}}</b>!’, name=name) - Test it, e.g. http://cm-demo.com/bottle-hello-world/index.py/hello/fred
- This should show “Hello fred!”