Link
Group: Actions
Description
A Link is ordinary clickable text that navigates to another route
in your app or to an external website. Use a Link when the visitor
is going to another page ("About", "Read the rules"); use a
Button when the visitor is performing an action. Like a
button, clicking a link to one of your own routes submits the form
fields on the page.
Syntax
Link(text, url)
Link(text, url, arguments)
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
text |
str |
required | The visible link text. |
url |
route name, route function, or web address | required | The name of a target route ("rules"), the route function itself (rules), or a full external address ("https://example.com"). External addresses are detected automatically. |
arguments |
see Passing arguments | None |
Extra values delivered to the target route's parameters. |
Like every component, Link also accepts
styling and attribute keywords.
A route's name as a string works wherever in the file the route is defined, and it is the form the tutorials use. The function form lets an editor that checks names point out a misspelled route, but the function must already be defined when the page is built. Route targets on the Button page explains the difference.
Examples
Links between your own pages:
from drafter import *
@route
def index() -> Page:
return Page([
"Welcome to the museum.\n",
Link("Visit the gift shop", "shop")
])
@route
def shop() -> Page:
return Page([
"Everything is free today.\n",
Link("Back to the entrance", "index")
])
start_server()
An external link, which opens the other site instead of running a route:
from drafter import *
@route
def index() -> Page:
return Page([
"Drafter's code lives on ",
Link("GitHub", "https://github.com/drafter-edu/drafter"),
".\n"
])
start_server()
Notes
- A link whose
urlnames no existing route (and is not a working external address) stops the page with a points to non-existent page error. - Links render inline with surrounding text and do not force a line
break; end the previous string with
"\n"if you want the link on its own line. - External links leave your app; the visitor uses the browser's back button to return, and your app's state is still there when they do.
Accessibility
Link text should say where the link goes ("Visit the gift shop"), not "here" or "this". Screen-reader users often browse a list of a page's links out of context.
Related components
- Button: the counterpart component for performing actions.
- Argument: extra values a link can deliver to its route.
- Add and connect pages is the how-to guide for joining several pages with buttons and links.