Skip to content

Commit

Permalink
Fix reload mode for jupyter notebook and stateful demos (#8599)
Browse files Browse the repository at this point in the history
* Remove deprecated method

* add changeset

* Fix bug

* Update test

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
freddyaboulton and gradio-pr-bot authored Jun 25, 2024
1 parent 65afffd commit ca125b7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/grumpy-houses-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---

fix:Fix reload mode for jupyter notebook and stateful demos
17 changes: 6 additions & 11 deletions gradio/ipython_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,27 @@ def track(self, demo: gr.Blocks):
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
1 change: 1 addition & 0 deletions js/app/src/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
});
Expand Down
15 changes: 13 additions & 2 deletions js/app/test/test_chatinterface.reload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down

0 comments on commit ca125b7

Please sign in to comment.