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

first pass at rendering iterables #335

Merged
merged 6 commits into from
Dec 16, 2023
Merged

Conversation

tgberkeley
Copy link
Collaborator

No description provided.

@tgberkeley tgberkeley requested a review from Alek99 December 9, 2023 00:44
Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great tom.

i think we also need a note (alertbox) somewhere on the page that lets people know the type signature of the functions does not matter at all to foreach. It's the type annotation on the state var that determines what operations are available (e.g. when nesting).


# Rendering Iterables

You will often want to display multiple similar components from a collection of data. The `rx.foreach` component takes an `iterable(list, tuple or dict)` and a `function` that renders each item in the list. This is useful for dynamically rendering a list of items defined in a state.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it should say either

... component takes an iterable (list, tuple, or dict)

or

... component takes a list, tuple, or dict

Probably the first one, since it makes it more clear that the iterable is one arg of which different types may be supplied.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

docs/rendering_iterables.md Outdated Show resolved Hide resolved
Comment on lines 68 to 70
lambda color, index: colored_box_index(
color, index
),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it not work if colored_box_index function is passed directly? or is this just to show that a lambda can also be used?

If the latter, then i'd suggest just collapsing the body of colored_box_index into the lambda function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed


## Dictionary

We can iterate through a `dict` data structure using a `foreach`. When the dict is passed through to the function that renders each item, it is presented as a list of key-value pairs `([1, "blue"],[2, "red"], [3, "green"])`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the example would look more familiar as a list of tuple, rather than a tuple of list... and using something other than int indexing might illustrate the use case better.

[("sky", "blue"), ("balloon", "red"), ("grass", "green")]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed


## Nested examples

`rx.foreach` can be used with nested state vars. Here we use nested `foreach` components to render the nested state vars. The `rx.foreach(project["technologies"], get_badge)` inside of the `project_item` function, renders the `dict` values which are `lists`. The `rx.box(rx.foreach(NestedState.projects, project_item))` inside of the `projects_example` function renders each of the `dicts` inside of the overall state var `list`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`rx.foreach` can be used with nested state vars. Here we use nested `foreach` components to render the nested state vars. The `rx.foreach(project["technologies"], get_badge)` inside of the `project_item` function, renders the `dict` values which are `lists`. The `rx.box(rx.foreach(NestedState.projects, project_item))` inside of the `projects_example` function renders each of the `dicts` inside of the overall state var `list`.
`rx.foreach` can be used with nested state vars. Here we use nested `foreach` components to render the nested state vars. The `rx.foreach(project["technologies"], get_badge)` inside of the `project_item` function, renders the `dict` values which are `list`. The `rx.box(rx.foreach(NestedState.projects, project_item))` inside of the `projects_example` function renders each `dict` inside of the overall state var `projects`.

we should avoid code literal unless the value is the actual exact name of some code you would type... dict instead of dicts and list instead of lists

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

```


If you want an example where not all of the values in the dict are the same type then check out the example on [var operations using foreach]("https://reflex.dev/docs/state/var-operations/#get-item-(indexing)").
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want an example where not all of the values in the dict are the same type then check out the example on [var operations using foreach]("https://reflex.dev/docs/state/var-operations/#get-item-(indexing)").
If you want an example where not all of the values in the dict are the same type then check out the example on [var operations using foreach]("/docs/state/var-operations/#get-item-(indexing)").

link should be relative

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to use : {var_operations.path}

tgberkeley and others added 2 commits December 12, 2023 14:27
Co-authored-by: Masen Furer <m_github@0x26.net>
@tgberkeley tgberkeley requested a review from masenf December 12, 2023 22:59
Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very helpful docs!

}


def display_colors(color: list):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if this would look better with a fuller annotation, like list[str, list[str]]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to include this

@tgberkeley tgberkeley merged commit e9b60dd into main Dec 16, 2023
1 check passed
@Alek99 Alek99 deleted the rendering-iterables-docs branch December 20, 2023 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants