-
Notifications
You must be signed in to change notification settings - Fork 15
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
docs: Render Lists #1061
Open
dgodinez-dh
wants to merge
7
commits into
deephaven:main
Choose a base branch
from
dgodinez-dh:dag_RenderLists
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
docs: Render Lists #1061
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8611541
docs: Render Lists
dgodinez-dh e06403b
list comprehension
dgodinez-dh 3e5b482
filter
dgodinez-dh c57c36c
merge latest
dgodinez-dh 79d41e0
fix merge
dgodinez-dh bd3ea49
Apply suggestions from code review
dgodinez-dh 33a88de
keys
dgodinez-dh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Render Lists | ||
|
||
You will often want to display multiple similar components from a collection of data. You can use Python [`filter`](https://docs.python.org/3/library/functions.html#filter) and [`list comprehensions`](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) with `deephaven.ui` to filter and transform your list of data into an list of components. | ||
|
||
## Render data from lists | ||
|
||
Here is an example list of content: | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
@ui.component | ||
def content_list(): | ||
return ui.flex( | ||
ui.text("apple: fruit"), | ||
ui.text("broccoli: vegetable"), | ||
ui.text("banana: fruit"), | ||
ui.text("yogurt: dairy"), | ||
ui.text("carrot: vegetable"), | ||
direction="column", | ||
) | ||
|
||
|
||
my_content_list = content_list() | ||
``` | ||
|
||
![my_content_list](../_assets/render_lists1.png) | ||
|
||
The only difference among those list items is their contents, their data. You will often need to show several instances of the same component using different data when building interfaces. Here is a short example of how to generate a list of items from an list of data: | ||
|
||
1. Move the data into a list | ||
2. Use list comprehension to map the list of data to a list of components | ||
3. Use the list of components in your component | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
food = [ | ||
"apple: fruit", | ||
"broccoli: vegetable", | ||
"banana: fruit", | ||
"yogurt: dairy", | ||
"carrot: vegetable", | ||
] | ||
|
||
|
||
@ui.component | ||
def content_list(data): | ||
# map the text items to components | ||
components = [ui.text(item) for item in data] | ||
return ui.flex(components, direction="column") | ||
|
||
|
||
my_content_list = content_list(food) | ||
``` | ||
|
||
## Filter lists of items | ||
|
||
If you want a way to only show items of type vegetable, you can use the Python `filter` function to return just those items. | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
food = [ | ||
"apple: fruit", | ||
"broccoli: vegetable", | ||
"banana: fruit", | ||
"yogurt: dairy", | ||
"carrot: vegetable", | ||
] | ||
|
||
|
||
@ui.component | ||
def content_list(data, data_type): | ||
# filter for items that end with the desired data type | ||
filtered = list(filter(lambda item: item.endswith(data_type), data)) | ||
# map the text items to components | ||
components = [ui.text(item) for item in filtered] | ||
return ui.flex(components, direction="column") | ||
|
||
|
||
my_content_list = content_list(food, "vegetable") | ||
``` | ||
|
||
![my_content_list2](../_assets/render_lists2.png) | ||
|
||
## Keep list items in order with keys | ||
|
||
Keys tell `deephaven.ui` which list item each component corresponds to, so that it can match them up later. This becomes important if your list items can move (e.g. due to sorting), get inserted, or get deleted. A well-chosen key helps `deephaven.ui` infer what exactly has happened, and make the correct updates. | ||
|
||
Rather than generating keys on the fly, you should include them in your data. | ||
|
||
### Where to get your key | ||
|
||
Different sources of data provide different sources of keys: | ||
|
||
- Data from a database: If your data is coming from a database, you can use the database keys/IDs, which are unique by nature. | ||
- Locally generated data: If your data is generated and persisted locally, use an incrementing counter or a package like `uuid` when creating items. | ||
|
||
### Rules of keys | ||
|
||
- Keys must be unique among siblings. However, it is okay to use the same keys for items in different lists. | ||
- Keys must not change. Do not generate them while rendering. | ||
|
||
In this example, the `ui_cells` component can add cell which can be deleted. The line `key=str(i)` is commented out, so the cell components do not have keys. If the user tries to delete a cell in the middle of the component, the last cell will be deleted instead. Comment in the line that sets the key. Now the correct cell will be deleted. | ||
|
||
```python | ||
from deephaven import ui | ||
import itertools | ||
|
||
|
||
@ui.component | ||
def ui_cell(label="Cell"): | ||
text, set_text = ui.use_state("") | ||
return ui.text_field(label=label, value=text, on_change=set_text) | ||
|
||
|
||
@ui.component | ||
def ui_deletable_cell(i, delete_cell): | ||
return ui.flex( | ||
ui_cell(label=f"Cell {i}"), | ||
ui.action_button( | ||
ui.icon("vsTrash"), | ||
aria_label="Delete cell", | ||
on_press=lambda: delete_cell(i), | ||
), | ||
align_items="end", | ||
# comment in this line to fix | ||
# key=str(i), | ||
) | ||
|
||
|
||
@ui.component | ||
def ui_cells(): | ||
id_iter, set_id_iter = ui.use_state(lambda: itertools.count()) | ||
cells, set_cells = ui.use_state(lambda: [next(id_iter)]) | ||
|
||
def add_cell(): | ||
set_cells(lambda old_cells: old_cells + [next(id_iter)]) | ||
|
||
def delete_cell(delete_id: int): | ||
set_cells(lambda old_cells: [c for c in old_cells if c != delete_id]) | ||
|
||
return ui.view( | ||
[ui_deletable_cell(i, delete_cell) for i in cells], | ||
ui.action_button(ui.icon("vsAdd"), "Add cell", on_press=add_cell), | ||
overflow="auto", | ||
) | ||
|
||
|
||
cells = ui_cells() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're missing talking about the
key
prop here. Each value in the list should have a key, and the React docs go into explicitly: https://react.dev/learn/rendering-lists#why-does-react-need-keysIn terms of deephaven.ui, it can be observed why you need keys when running a todo app. See an example without keys: #731
If you comment out the
key=str(i)
line, you'll see it screws up if you delete any of the cells except the last one; it will always "delete" the last cell:By adding the key, it knows how to track the elements and delete the correct one.
Should also write it with list comprehension, and possibly refactor the child todo component into a
ui_deletable_cell
or something.