Fast and simple WSGI micro web-framework for small web applications with no dependencies other than the Python Standard Library.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core application management and URL routing functionality including the main Bottle class, route decorators, URL parameter handling, and application mounting.
The Bottle class represents a web application instance with routes, configuration, plugins, and error handlers.
class Bottle:
def __init__(self, **kwargs):
"""
Create a new Bottle application.
Parameters:
- **kwargs: deprecated catchall and autojson options
"""
@property
def config(self) -> ConfigDict:
"""Application configuration dictionary."""
@property
def routes(self) -> list:
"""List of installed Route instances."""
@property
def router(self) -> Router:
"""Router instance for request matching."""
def run(self, **kwargs):
"""Start development server with this application."""
def wsgi(self, environ, start_response):
"""
WSGI interface for the application.
Parameters:
- environ: dict, WSGI environment
- start_response: callable, WSGI start_response function
Returns:
iterator: response iterator
"""
def mount(self, prefix, app, **options):
"""
Mount another WSGI application or Bottle instance.
Parameters:
- prefix: str, URL prefix for mounted app
- app: WSGI application or Bottle instance
- **options: mount options
"""
def merge(self, routes):
"""
Merge routes from another application.
Parameters:
- routes: list or Bottle, routes to merge
"""
def add_hook(self, name, func):
"""
Add a hook callback.
Parameters:
- name: str, hook name
- func: callable, hook callback
"""
def reset(self, route=None):
"""
Reset routes and clear plugin cache."""
def close(self):
"""
Close application and clean up resources."""Register URL patterns with handler functions using decorators or direct method calls.
def route(path=None, method='GET', callback=None, **options):
"""
Register a route with the default application.
Parameters:
- path: str, URL pattern (e.g., '/hello/<name>')
- method: str, HTTP method ('GET', 'POST', etc.)
- callback: function, handler function (when used directly)
- **options: route options (name, apply, skip, etc.)
Returns:
function: decorator function or Route instance
"""
def get(path=None, callback=None, **options):
"""Register a GET route. Equivalent to route(path, method='GET')."""
def post(path=None, callback=None, **options):
"""Register a POST route. Equivalent to route(path, method='POST')."""
def put(path=None, callback=None, **options):
"""Register a PUT route. Equivalent to route(path, method='PUT')."""
def delete(path=None, callback=None, **options):
"""Register a DELETE route. Equivalent to route(path, method='DELETE')."""
def patch(path=None, callback=None, **options):
"""Register a PATCH route. Equivalent to route(path, method='PATCH')."""For Bottle application instances, use the same methods:
class Bottle:
def route(self, path=None, method='GET', callback=None, **options): ...
def get(self, path=None, method='GET', **options): ...
def post(self, path=None, method='POST', **options): ...
def put(self, path=None, method='PUT', **options): ...
def delete(self, path=None, method='DELETE', **options): ...
def patch(self, path=None, method='PATCH', **options): ...Extract parameters from URL patterns using angle bracket syntax.
Usage examples:
@route('/hello/<name>')
def hello(name):
return f'Hello {name}!'
@route('/user/<id:int>')
def show_user(id):
return f'User ID: {id}'
@route('/static/<filepath:path>')
def serve_static(filepath):
return static_file(filepath, root='/static/')Supported filters:
:int - matches integers:float - matches floating point numbers:path - matches paths including slashes:re[...] - matches regular expressionsRegister error handlers for HTTP status codes.
def error(code=500, callback=None):
"""
Register an error handler for HTTP status codes.
Parameters:
- code: int, HTTP status code (404, 500, etc.)
- callback: function, error handler function
Returns:
function: decorator function
"""
class Bottle:
def error(self, code=500, callback=None): ...Usage:
@error(404)
def error_404(error):
return 'Page not found!'
@error(500)
def error_500(error):
return 'Internal server error!'Mount sub-applications or WSGI applications at URL prefixes.
def mount(prefix, app, **options):
"""
Mount an application at a URL prefix.
Parameters:
- prefix: str, URL prefix (e.g., '/api')
- app: Bottle or WSGI application
- **options: mounting options
"""
class Bottle:
def mount(self, prefix, app, **options): ...Generate URLs for named routes.
def url(routename, **kwargs):
"""
Generate URL for a named route.
Parameters:
- routename: str, name of the route
- **kwargs: URL parameters
Returns:
str: generated URL
"""
class Bottle:
def get_url(self, routename, **kwargs): ...Global application management for module-level shortcuts.
class AppStack(list):
"""Stack of Bottle applications for module-level shortcuts."""
def __call__(self):
"""Return the current default application."""
def push(self, app=None):
"""Push new application to stack."""
# Global instances
app: AppStack # Current default application stack
default_app: AppStack # Alias for app
apps: AppStack # Alias for appIndividual route mappings with metadata and configuration.
class Route:
def __init__(self, app, rule, method, callback, **options):
"""
Create a route instance.
Parameters:
- app: Bottle, parent application
- rule: str, URL pattern
- method: str, HTTP method
- callback: function, handler function
- **options: route configuration
"""
@property
def rule(self) -> str:
"""URL pattern for this route."""
@property
def method(self) -> str:
"""HTTP method for this route."""
@property
def callback(self):
"""Handler function for this route."""
@property
def name(self) -> str:
"""Route name for URL building."""
@property
def plugins(self) -> list:
"""List of plugins applied to this route."""
@property
def skiplist(self) -> set:
"""Set of plugins to skip for this route."""
@property
def config(self) -> dict:
"""Route configuration dictionary."""
def get_undecorated_callback(self):
"""Get original callback if decorated."""
def get_callback_args(self):
"""Get callback function argument names."""
def get_config(self, key, default=None):
"""Get route configuration value."""
def reset(self):
"""Reset route and clear plugin cache."""
def prepare(self):
"""Prepare route for execution (apply plugins)."""Maps HTTP requests to routes with URL parameter extraction.
class Router:
def add(self, rule, method, target, **options):
"""
Add a route to the router.
Parameters:
- rule: str, URL pattern
- method: str, HTTP method
- target: any, route target (usually Route instance)
- **options: router options
"""
def match(self, path, method='GET'):
"""
Match request path and method to a route.
Parameters:
- path: str, request path
- method: str, HTTP method
Returns:
tuple: (target, url_args) or (None, None)
"""Install with Tessl CLI
npx tessl i tessl/pypi-bottle