Bottle Python Microframework: Build Lightweight APIs & Web Apps

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Bottle: The Complete Guide to the Python Microframework

Introduction to Bottle

Bottle is a minimalist Python microframework for building web applications and REST APIs. The entire framework is written in a single file under 100 KB, making it an ideal choice for small projects, prototypes, embedded systems, and microservices. Bottle provides all the essential components to run a full-featured web application with zero external dependencies.

Features and Benefits of Bottle

Lightweight and Simple

Bottle requires no external dependencies and can be installed with a single command. The framework consists of just one file, simplifying deployment and code comprehension.

Built-in Capabilities

  • Built-in HTTP server for development
  • Its own SimpleTemplate Engine
  • WSGI support for production deployment
  • Automatic route parameter type detection
  • Handling of JSON, forms, files, and cookies

Architectural Flexibility

Bottle is compatible with any Python library for databases, template engines, and WSGI servers. This makes it easy to integrate into existing systems.

Installation and Basic Setup

Installing Bottle

pip install bottle

Creating Your First Application

Create a file called app.py:

from bottle import route, run

@route('/')
def index():
    return "Hello from Bottle!"

@route('/hello/<name>')
def greet(name):
    return f"Hello, {name}!"

if __name__ == "__main__":
    run(host='localhost', port=8080, debug=True)

Running the Application

python app.py

The application will be available at http://localhost:8080

Routing and Request Handling

Basic Routes

from bottle import route, get, post, put, delete

@route('/')
def home():
    return "Home Page"

@get('/users')
def get_users():
    return "List of Users"

@post('/users')
def create_user():
    return "Creating User"

@put('/users/<id:int>')
def update_user(id):
    return f"Updating User {id}"

@delete('/users/<id:int>')
def delete_user(id):
    return f"Deleting User {id}"

Route Parameters

Bottle supports various parameter types:

@route('/user/<id:int>')
def user_by_id(id):
    return f"User ID: {id}"

@route('/article/<slug:path>')
def article_by_slug(slug):
    return f"Article: {slug}"

@route('/price/<amount:float>')
def price(amount):
    return f"Price: {amount}"

@route('/profile/<username:re:[a-zA-Z0-9_]+>')
def profile(username):
    return f"Profile: {username}"

Handling Query Parameters

from bottle import request

@route('/search')
def search():
    query = request.query.get('q', '')
    category = request.query.get('category', 'all')
    return f"Searching '{query}' in category '{category}'"

Working with Request Data

Handling Forms

from bottle import request, post

@post('/submit')
def submit_form():
    name = request.forms.get('name')
    email = request.forms.get('email')
    age = request.forms.get('age', type=int)
    
    return f"Received data: {name}, {email}, {age}"

Working with JSON

from bottle import request, response

@post('/api/data')
def handle_json():
    data = request.json
    
    # Process the data
    result = {
        'status': 'success',
        'received': data,
        'message': 'D                    

Recommendations