-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Adds an every
parameter to every component
#2806
Conversation
I like the idea @abidlabs ! Let me think about this some more. The one thing that I don't like is that you wouldn't be able to use that component in any other event because there is no way to cancel the initial load event running on a loop. So something like this would behave buggy in my opinion, i.e. subsequent events created by slider change would show up alongside the intial load event, creating a flicker. import pandas as pd
import gradio as gr
import random
def run_query(n_points=10):
return pd.DataFrame({"data": [random.random() for _ in range(n_points)]})
with gr.Blocks() as demo:
max_value = gr.Slider(minimum=1, maximum=20, step=2)
df = gr.DataFrame(run_query, every=1)
max_value.change(run_query, inputs=max_value, outputs=df, every=1)
demo.queue().launch() |
You're right, we should expose the event in case a user wants to cancel it or do something else with it -- how about |
Let me know if you see any additional concerns @freddyaboulton. Otherwise, will go ahead and add this in & finalize the PR |
Makes sense @abidlabs ! |
Ok sounds good, I'll implement it this way |
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2806-all-demos |
All righty, this should be all set for review. I had to change the way we do load events to expose the event, but everything should be backwards compatible. Now, you can do something like this: import pandas as pd
import gradio as gr
import random
def run_query(n_points=10):
return pd.DataFrame({"data": [random.random() for _ in range(n_points)]})
with gr.Blocks() as demo:
max_value = gr.Slider(minimum=1, maximum=20, step=2)
df = gr.DataFrame(run_query, every=1)
max_value.change(run_query, inputs=max_value, outputs=df, every=1, cancels=[df.load_event])
demo.queue().launch() |
I'm hesitant to expand our API in what feels like unintuitive to me. We've never attached event listeners via Component constructors before. I would prefer: df = gr.Dataframe(value=fn)
df.every(60) which is at least parallel to |
I think right now, with gr.Blocks() as demo:
df = gr.Dataframe()
demo.every(fn=fn, inputs=None, outputs=df, rate=60) so would prefer our shorthand to be: df = gr.Dataframe(value=fn)
evt = df.every(rate=60) I prefer this because I don't like how much we've expanded the API for the shorthand - not only adding event listeners in the constructor, but also cancelling this event via Component.load_event |
Hmm, but we already do attach event listeners via Component constructors. When you do: df = gr.Dataframe(value=fn) It creates a |
Synced with @aliabid94, he'll think about this API a little more, so let's hold off merging for now |
Ok I think this is fine for now, I still do think our API is becoming unintuitive but I can't think of a simpler way without major changes. |
Ok but what if they only want to cancel a specific event? Also, what does "all attached event listeners" mean? Any events in which that component is an input or output? I feel like that complicates things, or at least should be a separate PR where we can discuss that |
my suggestion was, to |
I meant where the component is the triggering event, but I guess in the context of the dataframe default value function, the dataframe wouldn't even be the triggering component. |
Correct, it would only be the output component, so probably doesn't make sense. I've added the |
If we think passing the component into
I am ok with |
I like that suggestion @freddyaboulton |
Hmm I still feel like it is complicating user's experience -- consider a user who sees So unless there are other objections, I'll go ahead and merge this in? |
Thanks the feedback guys! |
As I was writing the BigQuery Guide, I realized that it would be very nice if every component had an
every
parameter that could be used if the component's initial value was a function. It would allow you to do something like this:And in fact, it would basically make the need for a
gr.Panel()
abstraction completely unnecessary. Lmk if you guys think this is a good idea @freddyaboulton @aliabid94 @dawoodkhan82. Implementation was straightforward to feel free to push back if this is not a good idea for any reason