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

docs: todo example to use a class for state #667

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kecnry
Copy link
Collaborator

@kecnry kecnry commented Jun 4, 2024

This PR updates the todo example to place all state in a (re-usable) TodoItem class, instead of a dataclass. This allows avoiding the need for Ref and .fields.

If there is no other example for Ref and fields in the docs, then maybe this shouldn't be merged, but at least shows the difference between the two implementations.

in place of dataclass with Ref() and fields
@@ -141,7 +97,7 @@ def create_new_item(*ignore_args):
]


# We store out reactive state, and our logic in a class for organization
# We store our reactive state, and our logic in a class for organization
# purposes, but this is not required.
# Note that all the above components do not refer to this class, but only
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

these in-line comments may need updating?

Copy link

@hsunner hsunner left a comment

Choose a reason for hiding this comment

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

There seem to be a bug in the TodoEdit class. It will update the edited TodoItem even when "close" is pressed.
Also including some comments on how the example could be made more enlightening (at least to me).

@@ -141,7 +96,7 @@ def create_new_item(*ignore_args):
]
Copy link

Choose a reason for hiding this comment

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

A comment here would be helpful explaining why this is globally declared and not created as a class attribute inside State or inside State.init().

"""Takes a reactive todo item and allows editing it. Will not modify the original item until 'save' is clicked."""
copy = solara.use_reactive(todo_item.value)
copy = TodoItem(todo_item.text, todo_item.done)
Copy link

Choose a reason for hiding this comment

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

It appears to me that this does not create a "deep" copy, but uses the same reactive objects in the todo_item, so that any change to the "copy" will actually modify also the todo_item.

Comment on lines 101 to 102
# Note that all the above components do not refer to this class, but only
# to do the Todo items.
Copy link

Choose a reason for hiding this comment

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

This could use further justification and explanation.

@@ -151,25 +106,23 @@ def create_new_item(*ignore_args):

Copy link

Choose a reason for hiding this comment

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

If this is an example of how to manage state, it would be even more valuable to show how the todos can be reused. Now the Page component is effectively a Todo component, but it's not reusable as State can only manage a single list of Todos.

@@ -183,14 +136,16 @@ def TodoStatus():
solara.Success("All done, awesome!", dense=True)


state = State(initial_items)


@solara.component
def Page():
with solara.Card("Todo list", style="min-width: 500px"):
Copy link

Choose a reason for hiding this comment

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

Please consider doing a separate Todo component card and show how it can be reused. Again, this may be another example (a dashboard of TODO lists for different people/tasks where you can add or remove entire TODOs?)

@@ -183,14 +136,16 @@ def TodoStatus():
solara.Success("All done, awesome!", dense=True)


state = State(initial_items)
Copy link

Choose a reason for hiding this comment

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

It would be nice with a comment explaining how the state object can still work for multiple clients (virtual kernels), despite it not being declared a solara.reactive.

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