Closed abidlabs closed 2 years ago
Both feels doable to me.
page_load
in our config json and frontend would respect that at the initial load of the webpage. WDYT @pngwn?
Example of Space that needs (and hacks) a on_page_load feature: https://huggingface.co/spaces/julien-c/persistent-data
- We've gotten requests from the Hugging Face team to have functions that run on page load. For example, it could be something as simple as a table that refreshes its content by loading from an external URL whenever you reload the page.
Let's add load into dependencies like this, WDYT @aliabid94 @pngwn @abidlabs? No component has id 0(and Blocks actually have id 0 within python Context), so let's make use of it like this. Updated the json in notion as well.
"dependencies": [
{
"targets": [0],
"trigger": "load",
"inputs": [],
"outputs": [],
"queue": False,
},
{
"targets": [0],
"trigger": "load",
"inputs": [1],
"outputs": [],
"queue": False,
},
{
"targets": [0],
"trigger": "load",
"inputs": [],
"outputs": [1],
"queue": False,
},
- We've also had requests for functions to call an external API after the prediction has been made:
I think we are currently supporting this, don't we? Can't we have functions without an input or output or both? @pngwn
The root Blocks
does not always have to have id 0
, because you can have multiple Blocks
defined within one Python session.
For example you could have:
blocks = gr.Blocks()
with blocks:
gr.Textbox()
blocks2 = gr.Blocks()
with blocks2:
gr.Textbox()
In this case, blocks2
would have an id
that is not 0
.
Which is to say that we should use the root Block id, whether it is or is not 0.
I think we are currently supporting this, don't we? Can't we have functions without an input or output or both? @pngwn
I don't think so, since in Block.set_event_trigger()
and in all of the component event trigger methods like change()
we do require all three function arguments fn
, inputs
, outputs
. I think we still need to handle the situation when inputs
or outputs
is None
, and update the dependencies in config suitably for these situations
The root
Blocks
does not always have to have id0
, because you can have multipleBlocks
defined within one Python session.For example you could have:
blocks = gr.Blocks() with blocks: gr.Textbox() blocks2 = gr.Blocks() with blocks2: gr.Textbox()
In this case,
blocks2
would have anid
that is not0
.Which is to say that we should use the root Block id, whether it is or is not 0.
That's correct, but it was OK if we used 0 as it was not going to affect anything.
This is the configs generated from this code:
import gradio as gr
blocks = gr.Blocks()
with blocks:
gr.Textbox()
blocks2 = gr.Blocks()
with blocks2:
gr.Textbox()
if __name__ == "__main__":
assert True
print(blocks.get_config_file())
print(blocks2.get_config_file())
{'mode': 'blocks', 'components': [{'id': 1, 'type': 'textbox', 'props': {'lines': 1, 'placeholder': None, 'default_value': '', 'name': 'textbox', 'label': None, 'css': {}}}], 'theme': 'default', 'layout': {'id': 0, 'children': [{'id': 1}]}, 'dependencies': []}
{'mode': 'blocks', 'components': [{'id': 3, 'type': 'textbox', 'props': {'lines': 1, 'placeholder': None, 'default_value': '', 'name': 'textbox', 'label': None, 'css': {}}}], 'theme': 'default', 'layout': {'id': 2, 'children': [{'id': 3}]}, 'dependencies': []}
But let's use like this without any target
"dependencies": [
{
"targets": [],
"trigger": "load",
"inputs": [],
"outputs": [],
"queue": False,
},
{
"targets": [],
"trigger": "load",
"inputs": [1],
"outputs": [],
"queue": False,
},
{
"targets": [],
"trigger": "load",
"inputs": [],
"outputs": [1],
"queue": False,
},
I think we are currently supporting this, don't we? Can't we have functions without an input or output or both? @pngwn
I don't think so, since in
Block.set_event_trigger()
and in all of the component event trigger methods likechange()
we do require all three function argumentsfn
,inputs
,outputs
. I think we still need to handle the situation wheninputs
oroutputs
isNone
, and update the dependencies in config suitably for these situations
We do accept empty list, wouldn't that work well?
click(fn, [], [])
or
click(fn,inputs=[], outputs=[])
I think this is more explicit and lets user know that there is no input or output.
Ok that seems reasonable. To summarize my understanding:
inputs=[]
in the config means that the function does not use any input componentoutputs=[]
in the config means the the function does not use any output componenttargets=[]
(and trigger="load"
) means that the function should run on page load (note: most of the time, a function that runs on page load will not need to use inputs, but this may not always be the case)Is that right?
How does a user define a function in Python to run on page load? Would it be like this:
with gr.Blocks() as bl:
t = gr.Textbox()
bl.load(lambda x:"Welcome! This page has loaded.", [], t)
Yes to all 👍
cc: @pngwn
Closed by #963 and #967.
2 related issues:
Implementation wise, it might look something like this (notice there is no
inputs
here):Implementation wise, it might look something like this (notice there is no
outputs
here):