Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Base Vars

Vars are any fields in your app that may change over time. A Var is directly rendered into the frontend of the app.

Base vars are defined as fields in your State class.

They can have a preset default value. If you don't provide a default value, you must provide a type annotation.

AAPL

Current Price: $150

Change: 4%

In this example ticker and price are base vars in the app, which can be modified at runtime.

Accessing state variables on different pages

State is just a python class and so can be defined on one page and then imported and used on another. Below we define TickerState class on the page state.py and then import it and use it on the page index.py.

Backend-only Vars

Any Var in a state class that starts with an underscore is considered backend only and will not be synchronized with the frontend. Data associated with a specific session that is not directly rendered on the frontend should be stored in a backend-only var to reduce network traffic and improve performance.

They have the advantage that they don't need to be JSON serializable, however they must still be cloudpickle-able to be used with redis in prod mode. They are not directly renderable on the frontend, and may be used to store sensitive values that should not be sent to the client.

For example, a backend-only var is used to store a large data structure which is then paged to the frontend using cached vars.

Page 1 / 10

Page Size

    _backend[0] = 62

    _backend[1] = 38

    _backend[2] = 0

    _backend[3] = 13

    _backend[4] = 34

    _backend[5] = 46

    _backend[6] = 89

    _backend[7] = 84

    _backend[8] = 43

    _backend[9] = 96

Using rx.field / rx.Field to improve type hinting for vars

When defining state variables you can use rx.Field[T] to annotate the variable's type. Then, you can initialize the variable using rx.field(default_value), where default_value is an instance of type T.

This approach makes the variable's type explicit, aiding static analysis tools in type checking. In addition, it shows you what methods are allowed to modify the variable in your frontend code, as they are listed in the type hint.

Below are two examples:

Here State.x, as it is typed correctly as a boolean var, gets better code completion, i.e. here we get options such as to_string() or equals().

Here State.x, as it is typed correctly as a dict of str to list of int var, gets better code completion, i.e. here we get options such as contains(), keys(), values(), items() or merge().