Category Archives: Bottle (Python)

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

  1. Create and activate a new virtualenv or use a virtual machine.
    1. I use a VM for experimenting with Python libraries and frameworks, to keep it separate from my client work
  2. sudo pip install bottle
  3. Create hello_world.py with the code from the Bottle home page
  4. Run the script: python hello_world.py
  5. Check in your browser: http://localhost:8080/hello/world
    1. This should show “Hello world!”
    2. 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:

  1. Domains / Websites -> Websites
    1. Select the domain
  2. Click on “Add an application” -> Create a new application
    1. Name: bottle_hello_world
    2. Category: mod_wsgi
    3. Type: mod_wsgi / Python 2.7 (note: Bottle also works with Python 3.x)
    4. URL: /bottle-hello-world
    5. Save
    6. Click Save again
  3. ssh into the host
    1. cd webapps
    2. cd <app name> (e.g. bottle_hello_world)
    3. cd htdocs
    4. pip install bottle
  4. Check bottle is installed
    1. python2.7
    2. import bottle
  5. 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)

  6. Test it, e.g. http://cm-demo.com/bottle-hello-world/index.py/hello/fred
    1. This should show “Hello fred!”

 

Python, Bottle and websockets

Here is a simple websockets demo, using Python and Bottle, based on the example on the Bottle website

  1. Requirements:
    • pip install bottle
    • pip install python-dev
    • pip install gevent-websocket
  2. Create websockets.py, from the source code at http://bottlepy.org/docs/dev/async.html#finally-websockets
  3. python websockets.py
    • This starts the (websockets) server. Note that it doesn’t show any output
  4. Create websockets.html, again from http://bottlepy.org/docs/dev/async.html#finally-websockets
    • Make sure to change the websocket address (from “ws://example.com:8080/websocket”). On your local machine this should be “ws://localhost:8080/websocket”
  5. Load websockets.html in your browser (e.g. as a local file, at file:/// etc)
    • This should come up with an alert saying: Your message was ‘Hello, world’