or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

channel-communication.mdcore-framework.mdevents-interactivity.mdexception-handling.mdfile-management.mdhtml-system.mdindex.mdresponse-system.mdrouting-urls.mdstatic-files-assets.md

routing-urls.mddocs/

0

# Routing and URLs

1

2

URL routing system with pattern matching, named routes, and support for both interactive and non-interactive views. Includes route decorators and programmatic route registration.

3

4

## Capabilities

5

6

### Route Class and Constants

7

8

URL route definition and matching utilities.

9

10

```python { .api }

11

class Route:

12

def __init__(self, raw_pattern: str, view, name: str = '',

13

interactive: bool = True, http_pass_through: bool = False,

14

frontend_view=None):

15

"""

16

Create a route definition.

17

18

Args:

19

raw_pattern (str): URL pattern with optional parameters

20

view: View class or function

21

name (str): Route name for reverse lookups

22

interactive (bool): Whether route supports interactivity

23

http_pass_through (bool): HTTP pass-through mode

24

frontend_view: Optional frontend view

25

"""

26

27

# Route matching constant

28

MATCH_ALL: int # Matches any URL pattern

29

```

30

31

### App Routing Methods

32

33

Methods for registering routes on the application.

34

35

```python { .api }

36

# Available on App class

37

def route(self, raw_pattern: str | int, name: str = '', interactive: bool = True,

38

http_pass_through: bool = False, frontend_view=None):

39

"""

40

Route decorator for views.

41

42

Args:

43

raw_pattern (str | int): URL pattern or MATCH_ALL constant

44

name (str): Route name

45

interactive (bool): Interactive support

46

http_pass_through (bool): HTTP pass-through

47

frontend_view: Frontend view function

48

"""

49

50

def add_route(self, route: 'Route'):

51

"""

52

Add route programmatically.

53

54

Args:

55

route (Route): Route object to add

56

"""

57

```

58

59

#### Usage Example

60

61

```python

62

from lona import App, Route, View, MATCH_ALL

63

from lona.html import HTML, H1

64

65

app = App(__file__)

66

67

# Using decorator (recommended)

68

@app.route('/')

69

class HomeView(View):

70

def handle_request(self, request):

71

return HTML(H1('Home Page'))

72

73

@app.route('/users/<int:user_id>', name='user_profile')

74

class UserView(View):

75

def handle_request(self, request):

76

user_id = request.match_info['user_id']

77

return HTML(H1(f'User {user_id}'))

78

79

# Programmatic route registration

80

class ApiView(View):

81

def handle_request(self, request):

82

return JsonResponse({'api': 'v1'})

83

84

api_route = Route('/api/v1', ApiView, name='api', interactive=False)

85

app.add_route(api_route)

86

87

# Catch-all route

88

@app.route(MATCH_ALL, name='not_found')

89

class NotFoundView(View):

90

def handle_request(self, request):

91

return HTML(H1('Page Not Found'))

92

```

93

94

## Types

95

96

```python { .api }

97

from typing import Union, Optional, Dict, Any, Type, Callable

98

99

RoutePattern = Union[str, int] # String pattern or MATCH_ALL constant

100

ViewHandler = Union[Type['View'], Callable]

101

RouteParams = Dict[str, Any]

102

MatchInfo = Dict[str, Union[str, int, float]]

103

```