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

added an example showing bidirectional binding between python/react for complex objects #9

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

Conversation

paddymul
Copy link
Collaborator

bi-directional binding between jupyter and python works... as long as you replace the entire object from both sides. As is react best practice.

Copy link
Collaborator

@kolibril13 kolibril13 left a comment

Choose a reason for hiding this comment

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

Nice example!
I am wondering:
When I use the first example and type
w.simple_dict["foo"] = 3000
into the next cell, it does not directly update the table, only after pressing the button the next time the value 3000 occurs.
However, when I type

w.simple_dict = {'foo':99, 'bar':99, 'baz':99}

in the next cell, all values are updated immediately without pressing the button again.
Why is that not the case?

"\n",
" @observe('count')\n",
" def _observe_count(self, change):\n",
" new_val = self.count\n",
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
" new_val = self.count\n",

unused variable

examples/DictUpdateExamples.ipynb Outdated Show resolved Hide resolved
"\n",
" @observe('count')\n",
" def _observe_count(self, change):\n",
" new_val = self.count\n",
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
" new_val = self.count\n",

@kolibril13
Copy link
Collaborator

and one more observation:
when I change the parameter bar in the second example

z = UpdateDictFromReactWidget()
z.simple_dict["bar"] = 3000

bar does not update to 3000 as well when I press the increment bar button.
bar only gets the new value after pressing the times confetti button.
As before,
z.simple_dict = {'foo':2, 'bar':2, 'baz':2} updates everything immediately.

@maartenbreddels
Copy link
Contributor

maartenbreddels commented Apr 28, 2023 via email

paddymul and others added 2 commits May 1, 2023 08:53
Co-authored-by: Jan-Hendrik Müller <44469195+kolibril13@users.noreply.github.com>
Co-authored-by: Jan-Hendrik Müller <44469195+kolibril13@users.noreply.github.com>
Copy link
Contributor

@maartenbreddels maartenbreddels left a comment

Choose a reason for hiding this comment

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

Useful example! I think a few comments would help get the message across.

"outputs": [],
"source": [
"class UpdateDictFromPythonWidget(ipyreact.ReactWidget):\n",
" #note that when we add these traitlets, they will automatically be made available to the react component\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
" #note that when we add these traitlets, they will automatically be made available to the react component\n",
" #note that when we add these traits with sync=True, they will automatically be made available to the react component\n",

" @observe('count')\n",
" def _observe_count(self, change):\n",
" new_val = self.count\n",
" sd = self.simple_dict.copy()\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a note saying that traitlets cannot detect mutations to a dict, and therefore we need to make a copy.

" export const DictUpdate = ({fullDict, set_fullDict}) => {\n",
" return (<button onClick={() => {\n",
" console.log('dict update')\n",
" const newVal = {...fullDict};\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Also could use a comment here that we do a (shallow) copy so we don't mutate the original object, otherwise changes detection would go unnoticed.

@kolibril13

This comment was marked as off-topic.

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.

3 participants