Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move docs to use dataclasses #1072

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions docs/api-reference/browser_storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Represents a state Var that is stored as a cookie in the browser. Currently only supports string values.

Parameters
Parameters

- `name` : The name of the cookie on the client side.
- `path`: The cookie path. Use `/` to make the cookie accessible on all pages.
Expand Down Expand Up @@ -144,19 +144,18 @@ rx.button(

# Serialization Strategies

If a non-trivial data structure should be stored in a `Cookie` or `LocalStorage` var it needs to
be serialized before and after storing it. It is recommended to use `rx.Base` for the data
which provides simple serialization helpers and works recursively in complex object structures.
If a non-trivial data structure should be stored in a `Cookie` or `LocalStorage` var it needs to be serialized before and after storing it. It is recommended to use a dataclass for the data which provides simple serialization helpers and works recursively in complex object structures.

```python demo exec
import reflex as rx
import dataclasses


class AppSettings(rx.Base):
@dataclasses.dataclass
class AppSettings:
theme: str = 'light'
sidebar_visible: bool = True
update_frequency: int = 60
error_messages: list[str] = []
error_messages: list[str] = dataclasses.field(default_factory=list)


class ComplexLocalStorageState(rx.State):
Expand Down
6 changes: 5 additions & 1 deletion docs/components/conditional_rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ We can also reuse a `cond` component several times by defining it within a funct
In this example we define the function `render_item`. This function takes in an `item`, uses the `cond` to check if the item `is_packed`. If it is packed it returns the `item_name` with a `✔` next to it, and if not then it just returns the `item_name`.

```python demo exec
class ToDoListItem(rx.Base):
import dataclasses

@dataclasses.dataclass
class ToDoListItem:
item_name: str
is_packed: bool

Expand Down Expand Up @@ -271,6 +274,7 @@ def cond_style_example():
),
)
```

## Multiple Conditional Statements

The `rx.match` component in Reflex provides a powerful alternative to`rx.cond` for handling multiple conditional statements and structural pattern matching. This component allows you to handle multiple conditions and their associated components in a cleaner and more readable way compared to nested `rx.cond` structures.
Expand Down
5 changes: 4 additions & 1 deletion docs/components/rendering_iterables.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ We can also use `foreach` with the `cond` component.
In this example we define the function `render_item`. This function takes in an `item`, uses the `cond` to check if the item `is_packed`. If it is packed it returns the `item_name` with a `✔` next to it, and if not then it just returns the `item_name`. We use the `foreach` to iterate over all of the items in the `to_do_list` using the `render_item` function.

```python demo exec
class ToDoListItem(rx.Base):
import dataclasses

@dataclasses.dataclass
class ToDoListItem:
item_name: str
is_packed: bool

Expand Down
9 changes: 7 additions & 2 deletions docs/library/tables-and-data-grids/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ def foreach_table_example():
It is also possible to define a `class` such as `Person` below and then iterate through this data structure, as a `list[Person]`.

```python
class Person(rx.Base):
import dataclasses

@dataclasses.dataclass
class Person:
full_name: str
email: str
group: str
Expand All @@ -248,8 +251,10 @@ For filtering the `rx.input` component is used. The data is filtered based on th


```python demo exec
import dataclasses

class Person(rx.Base):
@dataclasses.dataclass
class Person:
full_name: str
email: str
group: str
Expand Down
6 changes: 4 additions & 2 deletions docs/vars/custom_vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from pcweb.pages.docs import vars

As mentioned in the [vars page]({vars.base_vars.path}), Reflex vars must be JSON serializable.

This means we can support any Python primitive types, as well as lists, dicts, and tuples. However, you can also create more complex var types by inheriting from `rx.Base`.
This means we can support any Python primitive types, as well as lists, dicts, and tuples. However, you can also create more complex var types by inheriting from `rx.Base` or decorating them as dataclasses with `@dataclasses.dataclass`.

## Defining a Type

Expand All @@ -18,8 +18,10 @@ Once defined, we can use it as a state var, and reference it from within a compo

```python demo exec
import googletrans
import dataclasses

class Translation(rx.Base):
@dataclasses.dataclass
class Translation:
original_text: str
translated_text: str

Expand Down
7 changes: 5 additions & 2 deletions docs/vars/var-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,13 @@ def projects_example() -> rx.Component:
return rx.box(rx.foreach(ProjectsState.projects, project_item))
```

The previous example had only a single type for each of the dictionaries `keys` and `values`. For complex multi-type data, you need to use a `Base var`, as shown below.
The previous example had only a single type for each of the dictionaries `keys` and `values`. For complex multi-type data, you need to use a dataclass, as shown below.

```python demo exec
class ActressType(rx.Base):
import dataclasses

@dataclasses.dataclass
class ActressType:
actress_name: str
age: int
pages: list[dict[str, str]]
Expand Down
10 changes: 5 additions & 5 deletions templates/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ author: "Reflex"
image: "dashboard.webp"
demo: "https://dashboard-new.reflex.run/"
source: "https://github.com/reflex-dev/templates/tree/main/dashboard"
meta: [
{"name": "keywords", "content": ""},
]
meta: [{ "name": "keywords", "content": "" }]
---

The following is a dashboard to interactively display data some data. It is a good starting point for building more complex apps that require data visualization.
Expand All @@ -33,12 +31,14 @@ Right now the apps reads from a local CSV file. You can modify this by changing
Additionally you will want to change the `Item` class to match the data in your CSV file.

```python
class Item(rx.Base):
import dataclasses

@dataclasses.dataclass
class Item:
"""The item class."""

name: str
payment: float
date: str
status: str
```

Loading