- Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.
- Requests to function-call mapping with support for clean and dynamic URLs.
- Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
- Convenient access to form data, file uploads, cookies, headers and other HTTP related metadata.
- Built-in HTTP development server and support for paste, fapws3, bjoern, Google App Engine, cherrypy or any other WSGI capable HTTP server.
For python 2.x:
pip install bottle
For python 3.x:
yaourt -S python-bottle
Note: To install on python 2.x, you need the package python-pip
To get started with your first bottle server, here is a basic hello world example:
from bottle import route, run @route('/:name') def index(name='World'): return 'Hello %s!' % name run(host='localhost', port=8080)
You should see this next:
Bottle server starting up (using WSGIRefServer())... Listening on http://localhost:8080/ Use Ctrl-C to quit.
FastCGI is a great way of interfacing with Bottle (from lighttpd, nginx, etc). To use it however we must hook Bottle into something. For a simple example, let's use FLUP (for python2.x)
pacman -S python-flup
And now for our hello world application:
from bottle import route, run, FlupFCGIServer @route('/:name') def index(name='World'): return 'Hello %s!' % name run(host='localhost', port=8080, server=FlupFCGIServer)
more details on alternative servers here
By default, Bottle does not support reloading of files. However there is an option to allow such a feature. Bottle does this by starting a new server in parallel to the one currently running, switching over to it, and then removing the older version.
from bottle import route, run @route('/:name') def index(name='World'): return 'Hello %s!' % name run(host='localhost', port=8080, reloader=True)
and to set the interval in which it reloads itself (default of 1 second):
run(host='localhost', port=8080, reloader=True, interval=0.5)
My application works, but only locally
Be sure if you are using Bottle directly as a web server (good for development, not recommend for production) to set the host to the ip address it will be accessed from and not 'localhost'
Bottle isn't automatically reloading when I make changes to one of my files!
The Bottle reloader checks on which applications have changed by monitoring the imported files. Make sure that you are globally importing the application modules that you want to be reloaded.
will work but not:
@route('/:name') def index(name='World'): import myapp return myapp.init(name)