Skip to content

Adds ability to provide a gr.update() dictionary even if postprocess=False #2385

Merged
abidlabs merged 8 commits into
prepostexfrom
updict
Oct 4, 2022
Merged

Adds ability to provide a gr.update() dictionary even if postprocess=False #2385
abidlabs merged 8 commits into
prepostexfrom
updict

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Oct 3, 2022

Previously, if postprocess was False in an event trigger, the gr.update() dictionary would not work. This PR fixes that and can be tested by running the example code in the issue:

import gradio as gr

base64image = "data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo+Pj/+pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP+8vH9QUK+vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5MLypoG8wKOuwsP/g4P/Q0IcwKEswKMl8aJ9fX2xjdOtGRs/Pz+Dg4GImIP8gIH0sKEAwKKmTiKZ8aB/f39Wsl+LFt8dgUE9PT5x5aHBwcP+AgP+WltdgYMyZfyywz78AAAAAAAD///8AAP9mZv///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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vOSu4QG6UvxBRydcpKsav++Ca6G8A6Pr1x2kVMyHwsVxUALDq/krnrhPSOzXG1lUTIoffqGR7Goi2MAxbv6O2kEG56I7CSlRsEFKFVyovDJoIRTg7sugNRDGqCJzJgcKE0ywc0ELm6KBCCJo8DIPFeCWNGcyqNFE06ToAfV0HBRgxsvLThHn1oddQMrXj5DyAQgjEHSAJMWZwS3HPxT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw=="

def infer(x):
    return base64image, gr.update(visible=True)

with gr.Blocks() as demo:
    prompt = gr.Textbox()
    image = gr.Image()
    run_button = gr.Button()
    share_button = gr.Button("share", visible=False)
    
    run_button.click(infer, prompt, [image, share_button], postprocess=False)
    
    demo.launch()

Fixes: #2378

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Oct 3, 2022

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2385-all-demos

@abidlabs abidlabs changed the base branch from main to prepostex October 3, 2022 21:46
Copy link
Copy Markdown
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

Thank you @abidlabs ! Looks good to me - left some minor suggestions.

I'll review the other PR once this one is merged.

Comment thread test/test_blocks.py
Comment thread test/test_blocks.py
Comment thread gradio/examples.py
Comment thread gradio/blocks.py
continue
block = self.blocks[output_id]
if getattr(block, "stateful", False):
if not utils.is_update(predictions[i]):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unrelated to your PR but why do we not set the value of a state to be an update?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm good point. I suppose that if the update includes a value key, we should assign the value to the state.

I think this check was introduced because if a function returns values in a component-dictionary format (e.g. {textbox: "hi"}), we create placeholder updates for the rest of the components, and those placeholder components are a singleton dictionary: {"__type__"="generic_update"}. And we wouldn't want to assign this dictionary itself to the state.

Let me make a fix and add a test!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's fine if we keep it for now too - don't mean to slow down this PR. I was just curious hehe

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok actually gr.State() doesn't have an update() method at the moment, so passing in a dictionary of updates for gr.State() is not supported. There's no strong reason to add update() when a function can just return the value itself, so I'll leave this out for now.

@abidlabs
Copy link
Copy Markdown
Member Author

abidlabs commented Oct 4, 2022

Thanks for the review and great catches @freddyaboulton! Merging into the other branch

@abidlabs abidlabs merged commit fb3ae25 into prepostex Oct 4, 2022
@abidlabs abidlabs deleted the updict branch October 4, 2022 16:32
abidlabs added a commit that referenced this pull request Oct 4, 2022
* fixing examples

* adding parameters

* postprocess

* examples

* added tests

* changelog

* Update CHANGELOG.md

* added docstrings

* Adds ability to provide a `gr.update()` dictionary even if `postprocess=False`  (#2385)

* docs

* fixes

* added docs, fixed test

* changelog

* fix suggestoins

* formatting

* fix tests

* fix tests
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.

gr.update() doesn't work if postprocess=False in event trigger

2 participants