gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
32.41k stars 2.42k forks source link

state.change listener with deep hash check #8446

Closed aliabid94 closed 3 months ago

aliabid94 commented 3 months ago

state.change event listeners now do "deep checks", by deep hashing the old value. This makes @gr.render logic much cleaner. See demo/todo_list changes to see how much simpler the code is now because state values can be edited directly to induce a change.

Closes: https://github.com/gradio-app/gradio/issues/8436

gradio-pr-bot commented 3 months ago

🪼 branch checks and previews

• Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
:unicorn: Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/18ae92782cf6061a6ac1055698779537e9e17b34/gradio-4.32.2-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@18ae92782cf6061a6ac1055698779537e9e17b34#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/18ae92782cf6061a6ac1055698779537e9e17b34/gradio-client-0.20.1.tgz
gradio-pr-bot commented 3 months ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio minor

With the following changelog entry.

state.change listener with deep hash check

Maintainers or the PR author can modify the PR title to modify this entry.

#### Something isn't right? - Maintainers can change the version label to modify the version bump. - If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can [update the changelog file directly](https://github.com/gradio-app/gradio/edit/deep_check_state_change/.changeset/young-lamps-press.md).
pngwn commented 3 months ago

How expensive is this?

aliabid94 commented 3 months ago

How expensive is this?

Pretty cheap I would think. It runs a hash function for every element in the nested object, and only gets run if there's a state change listener involved. In regular python the same hash function is called whenever you add any element to a dictionary or a set, so hashing is already quite optimized.

abidlabs commented 3 months ago

It might be worth adding a note on the State.change() event listener docstring that this could get expensive if the State object is a sequence with a large number of elements

aliabid94 commented 3 months ago

It might be worth adding a note on the State.change() event listener docstring that this could get expensive if the State object is a sequence with a large number of elements

I don't think it's possible to directly add docs to the event of a single component. Regardless will update docs and guides on gr.State in a follow up PR.