Write responsive web apps in full python
—
URL routing system with pattern matching, named routes, and support for both interactive and non-interactive views. Includes route decorators and programmatic route registration.
URL route definition and matching utilities.
class Route:
def __init__(self, raw_pattern: str, view, name: str = '',
interactive: bool = True, http_pass_through: bool = False,
frontend_view=None):
"""
Create a route definition.
Args:
raw_pattern (str): URL pattern with optional parameters
view: View class or function
name (str): Route name for reverse lookups
interactive (bool): Whether route supports interactivity
http_pass_through (bool): HTTP pass-through mode
frontend_view: Optional frontend view
"""
# Route matching constant
MATCH_ALL: int # Matches any URL patternMethods for registering routes on the application.
# Available on App class
def route(self, raw_pattern: str | int, name: str = '', interactive: bool = True,
http_pass_through: bool = False, frontend_view=None):
"""
Route decorator for views.
Args:
raw_pattern (str | int): URL pattern or MATCH_ALL constant
name (str): Route name
interactive (bool): Interactive support
http_pass_through (bool): HTTP pass-through
frontend_view: Frontend view function
"""
def add_route(self, route: 'Route'):
"""
Add route programmatically.
Args:
route (Route): Route object to add
"""from lona import App, Route, View, MATCH_ALL
from lona.html import HTML, H1
app = App(__file__)
# Using decorator (recommended)
@app.route('/')
class HomeView(View):
def handle_request(self, request):
return HTML(H1('Home Page'))
@app.route('/users/<int:user_id>', name='user_profile')
class UserView(View):
def handle_request(self, request):
user_id = request.match_info['user_id']
return HTML(H1(f'User {user_id}'))
# Programmatic route registration
class ApiView(View):
def handle_request(self, request):
return JsonResponse({'api': 'v1'})
api_route = Route('/api/v1', ApiView, name='api', interactive=False)
app.add_route(api_route)
# Catch-all route
@app.route(MATCH_ALL, name='not_found')
class NotFoundView(View):
def handle_request(self, request):
return HTML(H1('Page Not Found'))from typing import Union, Optional, Dict, Any, Type, Callable
RoutePattern = Union[str, int] # String pattern or MATCH_ALL constant
ViewHandler = Union[Type['View'], Callable]
RouteParams = Dict[str, Any]
MatchInfo = Dict[str, Union[str, int, float]]Install with Tessl CLI
npx tessl i tessl/pypi-lona