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

gr.Blocks().load() order #5275

Closed
jonathanasdf opened this issue Aug 21, 2023 · 2 comments
Closed

gr.Blocks().load() order #5275

jonathanasdf opened this issue Aug 21, 2023 · 2 comments

Comments

@jonathanasdf
Copy link

Unlike other events it doesn't look like the load() event has a .then() function

How can I guarantee ordering for multiple load() events?

@jonathanasdf
Copy link
Author

jonathanasdf commented Aug 21, 2023

Hm just saw #4304 so this should be working...

figured out part of it after some testing. the initial event's function can't be None.

with gr.Blocks() as gr_demo:
    gr_url_params = gr.JSON({}, visible=False)
    gr_demo.load(
        None,
        outputs=[gr_url_params],
        _js='function(){return Object.fromEntries(new URLSearchParams(window.location.search));}',
    ).then(lambda params: print(params), inputs=[gr_url_params])
gr_demo.launch()

The above does not work, the print does not get triggered

replacing None with lambda: None triggers the print, but it doesn't seem to capture the correct value for the url params

with gr.Blocks() as gr_demo:
    gr_url_params = gr.JSON({}, visible=False)
    gr_demo.load(
        lambda: None,
        outputs=[gr_url_params],
        _js='function(){return Object.fromEntries(new URLSearchParams(window.location.search));}',
    ).then(lambda params: print(params), inputs=[gr_url_params])
gr_demo.launch()

@jonathanasdf
Copy link
Author

Ok finally this worked, the interface for working with _js is confusing and not well documented..

import gradio as gr
with gr.Blocks() as gr_demo:
    gr_url_params = gr.JSON({}, visible=False)
    gr_demo.load(
        lambda params: params,
        inputs=[gr_url_params],
        outputs=[gr_url_params],
        _js='function(){return Object.fromEntries(new URLSearchParams(window.location.search));}',
    ).then(lambda params: print(params), inputs=[gr_url_params])
gr_demo.launch()

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

No branches or pull requests

1 participant