Python: Bottle Framework

Introduction

The Python 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. The framework provides for the following functionalities
  • Routing - Requests to function-call mapping with support for clean and dynamic URLs.
  • Templates - Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
  • Utilities - Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
  • Server - Built-in HTTP development server and support for paste, fapws3, bjoern, gae, cherrypy or any other WSGI capable HTTP server.

Installation

Bottle does not depend on any external libraries. You can just download bottle.py into your project directory and start coding:
$ wget https://bottlepy.org/bottle.py
If you really want to go the pip way, you can install it using
sudo pip install bottle

Example Usage

from bottle import route, run, template

@route('/hello/')
def index(name):
    return template('Hello {{name}}!', name=name)

run(host='localhost', port=8080)
Run this script or paste it into a Python console, then point your browser to http://localhost:8080/hello/world. That’s it.
The route() decorator binds a piece of code to an URL path. In this case, we link the /hello path to the hello() function. This is called a route (hence the decorator name) and is the most important concept of this framework. You can define as many routes as you want. Whenever a browser requests a URL, the associated function is called and the return value is sent back to the browser. It’s as simple as that.
The run() call in the last line starts a built-in development server. It runs on localhost port 8080 and serves requests until you hit Control-c. You can switch the server backend later, but for now a development server is all we need. It requires no setup at all and is an incredibly painless way to get your application up and running for local tests.

More Information

You can check out the website for more details. Specifically Recipes and API Documentation
This video gives a basic introduction to the topic.

Comments