Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def set_event_trigger(
outputs: List[Component],
preprocess=True,
queue=False,
no_target: bool = False,
) -> None:
"""
Adds an event to the component's dependencies.
Expand All @@ -45,6 +46,7 @@ def set_event_trigger(
fn: Callable function
inputs: input list
outputs: output list
no_target: if True, sets "targets" to [], used for Blocks "load" event
Returns: None
"""
# Support for singular parameter
Expand All @@ -56,7 +58,7 @@ def set_event_trigger(
Context.root_block.fns.append((fn, preprocess))
Context.root_block.dependencies.append(
{
"targets": [self._id],
"targets": [self._id] if not no_target else [],
"trigger": event_name,
"inputs": [block._id for block in inputs],
"outputs": [block._id for block in outputs],
Expand Down Expand Up @@ -243,3 +245,19 @@ def __exit__(self, *args):
Context.root_block = None
else:
self.parent.children.extend(self.children)

def load(
self, fn: Callable, inputs: List[Component], outputs: List[Component]
) -> None:
"""
Adds an event for when the demo loads in the browser.

Parameters:
fn: Callable function
inputs: input list
outputs: output list
Returns: None
"""
self.set_event_trigger(
event_name="load", fn=fn, inputs=inputs, outputs=outputs, no_target=True
)
7 changes: 7 additions & 0 deletions gradio/test_data/blocks_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@
"outputs": [12],
"queue": False,
},
{
"targets": [],
"trigger": "load",
"inputs": [],
"outputs": [14],
"queue": False,
},
],
}

Expand Down
10 changes: 4 additions & 6 deletions test/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def test_xray(self):
}
ct_model = lambda diseases, img: {disease: 0.1 for disease in diseases}

xray_blocks = gr.Blocks()

with xray_blocks:
with gr.Blocks() as demo:
gr.components.Markdown(
"""
# Detect Disease From Scan
Expand Down Expand Up @@ -48,10 +46,10 @@ def test_xray(self):
ct_run.click(
ct_model, inputs=[disease, ct_scan], outputs=ct_results
)
textbox = gr.components.Textbox()
demo.load(lambda x: x, [], [textbox])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This example doesn't make sense to me. The lambda function takes in an input but none is provided. Can we replace with a function that doesn't take in any inputs for it to make more sense. For example:

from datetime import datetime

def get_time():
  now = datetime.now()
  return now.strftime("%H:%M:%S")

demo.load(get_time, [], [textbox])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh I actually did that, but later I thought no need for a function definition, but you are right :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

btw it was just a test to check the config, not a demo, but updating now.


_ = gr.components.Textbox()

self.assertEqual(XRAY_CONFIG, xray_blocks.get_config_file())
self.assertEqual(XRAY_CONFIG, demo.get_config_file())


if __name__ == "__main__":
Expand Down