Reflex Logo

Intro

Gallery

Hosting

Components

New

Learn

Components

API Reference

Onboarding

Pages

/

Routes

Pages in Reflex allow you to define components for different URLs. This section covers creating pages, handling URL arguments, accessing query parameters, managing page metadata, and handling page load events.

You can create a page by defining a function that returns a component. By default, the function name will be used as the route, but you can also specify a route.

def index():
    return rx.text("Root Page")


def about():
    return rx.text("About Page")


def custom():
    return rx.text("Custom Route")


app = rx.App()

app.add_page(index)
app.add_page(about)
app.add_page(custom, route="/custom-route")

In this example we create three pages:

  • index - The root route, available at /
  • about - available at /about
  • /custom - available at /custom-route

You can also use the @rx.page decorator to add a page.

@rx.page(route="/", title="My Beautiful App")
def index():
    return rx.text("A Beautiful App")

This is equivalent to calling app.add_page with the same arguments.

Pages can also have nested routes.

@rx.page(route="/nested/page")
def nested_page():
    return rx.text("Nested Page")


app = rx.App()

This component will be available at /nested/page.

The router.page.path attribute allows you to obtain the path of the current page from the router data, for dynamic pages this will contain the slug rather than the actual value used to load the page.

To get the actual URL displayed in the browser, use router.page.raw_path. This will contain all query parameters and dynamic path segments.

class State(rx.State):
    def some_method(self):
        current_page_route = self.router.page.path
        current_page_url = self.router.page.raw_path
        # ... Your logic here ...

In the above example, current_page_route will contain the route pattern (e.g., /posts/[id]), while current_page_url will contain the actual URL (e.g., http://example.com/posts/123).

To get the full URL, access the same attributes with full_ prefix.

Example:

class State(rx.State):
    @rx.var
    def current_url(self) -> str:
        return self.router.page.full_raw_path

In this example, running on localhost should display http://localhost:3000/user/hey/posts/3/

You can use the router.session.client_ip attribute to obtain the IP address of the client associated with the current state.

class State(rx.State):
    def some_method(self):
        client_ip = self.router.session.client_ip
        # ... Your logic here ...
← LibraryDynamic Routing →

Did you find this useful?

HomeGalleryChangelogIntroductionHosting