Difference between revisions of "User:Zvxy/Bottle"

From ArchWiki
Jump to: navigation, search
m (categorized)
(Installing Bottle: fix AUR installation instruction)
Line 16: Line 16:
== Installing Bottle ==
== Installing Bottle ==
For python 2.x:
Install {{Package AUR|python-bottle}} (for Python 3) or {{Package AUR|python2-bottle}} (for Python 2) from the [[AUR]].
pip install bottle
For python 3.x:
yaourt -S python-bottle
''Note: To install on python 2.x, you need the package [http://www.archlinux.org/packages/extra/any/python-pip/ python-pip]''
== Hello World ==
== Hello World ==

Revision as of 17:23, 11 August 2011

From Bottle: Python Web Framework:



  • Routing:
  • Templates:
  • Utilities:
  • Server:

Installing Bottle

Install Template:Package AUR (for Python 3) or Template:Package AUR (for Python 2) from the AUR.

Hello World

To get started with your first bottle server, here is a basic hello world example:

from bottle import route, run
def index(name='World'):
    return 'Hello %s!' % name

run(host='localhost', port=8080)

To start Bottle, just simply run it using python

python server.py

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

run(host='', port=8080)

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.

import myapp

will work but not:

def index(name='World'):
    import myapp
    return myapp.init(name)