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

Have gr.on set value at start as well #9065

Merged
merged 3 commits into from
Aug 12, 2024
Merged

Conversation

aliabid94
Copy link
Collaborator

If you have a demo like this:

with gr.Blocks() as demo:
    a = gr.Number(1)
    b = gr.Number(1)
    c = gr.Number()

    @gr.on(inputs=[a,b], outputs=c)
    def add(x,y):
        return x + y

demo.launch()

c will only be set to the value of a + b when they change, not initially when the demo runs. Usually users would want the initial value to also be a function of the inputs (when no trigger is specified).

This is also consistent with the behaviour of gr.render and value=fn, e.g. both of these would set c to 2 intitially:

with gr.Blocks() as demo:
    a = gr.Number(1)
    b = gr.Number(1)

    @gr.render(inputs=[a, b])
    def renderfn(x, y):
        gr.Number(x + y)

demo.launch()
with gr.Blocks() as demo:
    a = gr.Number(1)
    b = gr.Number(1)
    c = gr.Number(lambda x, y: x + y, inputs=[a, b])

demo.launch()

Technically breaking so going into 5.0

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Aug 7, 2024

🪼 branch checks and previews

Name Status URL
Website ready! Website preview

@abidlabs
Copy link
Member

abidlabs commented Aug 7, 2024

lgtm one note above

@aliabid94 aliabid94 merged commit 370e01a into 5.0-dev Aug 12, 2024
16 of 17 checks passed
@aliabid94 aliabid94 deleted the demo_load_for_on_listeners branch August 12, 2024 21:26
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