Fix reload mode for jupyter notebook and stateful demos (#8599)

* Remove deprecated method

* add changeset

* Fix bug

* Update test

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2024-06-25 05:36:36 +01:00 committed by GitHub
parent 65afffd34b
commit ca125b728a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 14 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---
fix:Fix reload mode for jupyter notebook and stateful demos

View File

@ -59,32 +59,27 @@ class JupyterReloader(BaseReloader):
def load_ipython_extension(ipython):
reloader = JupyterReloader(ipython)
@magic_arguments()
@argument("--demo-name", default="demo", help="Name of gradio blocks instance.")
@argument(
@magic_arguments() # type: ignore
@argument("--demo-name", default="demo", help="Name of gradio blocks instance.") # type: ignore
@argument( # type: ignore
"--share",
default=False,
const=True,
nargs="?",
help="Whether to launch with sharing. Will slow down reloading.",
)
@register_cell_magic
@needs_local_scope
@register_cell_magic # type: ignore
@needs_local_scope # type: ignore
def blocks(line, cell, local_ns):
"""Launch a demo defined in a cell in reload mode."""
args = parse_argstring(blocks, line)
args = parse_argstring(blocks, line) # type: ignore
exec(cell, None, local_ns)
demo: gr.Blocks = local_ns[args.demo_name]
if not reloader.demo_tracked():
demo.launch(share=args.share)
reloader.track(demo)
elif reloader.queue_changed(demo):
print("Queue got added or removed. Restarting demo.")
reloader.running_demo.close()
demo.launch()
reloader.track(demo)
else:
reloader.swap_blocks(demo)
return reloader.running_demo.artifact

View File

@ -318,6 +318,7 @@
app.close();
app = await Client.connect(api_url, {
status_callback: handle_status,
with_null_state: true,
events: ["data", "log", "status", "render"]
});

View File

@ -58,9 +58,9 @@ test("gradio dev mode correctly reloads a stateful ChatInterface demo", async ({
import gradio as gr
def greet(msg, history):
return "Hello"
return f"You typed: {msg}"
demo = gr.ChatInterface(fn=greet, textbox=gr.Textbox(label="foo"))
demo = gr.ChatInterface(fn=greet, textbox=gr.Textbox(label="foo", placeholder="Type a message..."))
if __name__ == "__main__":
demo.launch()
@ -75,6 +75,17 @@ if __name__ == "__main__":
}
});
await expect(page.getByLabel("foo")).toBeVisible();
const textbox = page.getByPlaceholder("Type a message...");
const submit_button = page.getByRole("button", { name: "Submit" });
await textbox.fill("hello");
await submit_button.click();
await expect(textbox).toHaveValue("");
const response = page.locator(".bot p", {
hasText: "You typed: hello"
});
await expect(response).toBeVisible();
} finally {
if (_process) kill_process(_process);
}

View File

@ -104,7 +104,6 @@ exclude = [
"gradio/themes/",
"gradio/_frontend_code/",
"gradio/components/*_plot.py",
"gradio/ipython_ext.py",
"gradio/node/*.py",
"gradio/_frontend_code/*.py",
]