mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-21 02:19:59 +08:00
Merge pull request #866 from gradio-app/790-in-out
add test for output to input
This commit is contained in:
commit
bb0b965998
47
demo/input-output/config.json
Normal file
47
demo/input-output/config.json
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"mode": "blocks",
|
||||
"components": [
|
||||
{
|
||||
"id": 1,
|
||||
"type": "textbox",
|
||||
"props": {
|
||||
"lines": 1,
|
||||
"placeholder": "None",
|
||||
"default": "",
|
||||
"name": "textbox",
|
||||
"label": "None",
|
||||
"css": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "button",
|
||||
"props": {
|
||||
"value": "Run",
|
||||
"name": "button",
|
||||
"label": "None",
|
||||
"css": {}
|
||||
}
|
||||
}
|
||||
],
|
||||
"theme": "default",
|
||||
"layout": {
|
||||
"id": 0,
|
||||
"children": [
|
||||
{
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"id": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
"dependencies": [
|
||||
{
|
||||
"targets": [2],
|
||||
"trigger": "click",
|
||||
"inputs": [1],
|
||||
"outputs": [1]
|
||||
}
|
||||
]
|
||||
}
|
16
demo/input-output/run.py
Normal file
16
demo/input-output/run.py
Normal file
@ -0,0 +1,16 @@
|
||||
import gradio as gr
|
||||
|
||||
|
||||
def image_mod(text):
|
||||
return text[::-1]
|
||||
|
||||
block = gr.Blocks()
|
||||
|
||||
with block:
|
||||
text = gr.Textbox()
|
||||
btn = gr.Button("Run")
|
||||
btn.click(image_mod, text, text)
|
||||
|
||||
print(block.get_config_file())
|
||||
if __name__ == "__main__":
|
||||
block.launch()
|
44
ui/packages/app/test/input-output.spec.ts
Normal file
44
ui/packages/app/test/input-output.spec.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { test, expect, Page } from "@playwright/test";
|
||||
|
||||
function mock_demo(page: Page, demo: string) {
|
||||
return page.route("http://localhost:7860/config", (route) => {
|
||||
return route.fulfill({
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*"
|
||||
},
|
||||
path: `../../../demo/${demo}/config.json`
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function mock_api(page: Page, body: Array<unknown>) {
|
||||
return page.route("http://localhost:7860/api/predict/", (route) => {
|
||||
const id = JSON.parse(route.request().postData()!).fn_index;
|
||||
return route.fulfill({
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: body[id]
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("a component acts as both input and output", async ({ page }) => {
|
||||
await mock_demo(page, "input-output");
|
||||
await mock_api(page, [["world hello"]]);
|
||||
await page.goto("http://localhost:3000");
|
||||
|
||||
const textbox = await page.locator(".input-text");
|
||||
const button = await page.locator("button");
|
||||
|
||||
await textbox.fill("hello world");
|
||||
|
||||
await Promise.all([
|
||||
button.click(),
|
||||
page.waitForResponse("http://localhost:7860/api/predict/")
|
||||
]);
|
||||
|
||||
await expect(await page.inputValue(".input-text")).toEqual("world hello");
|
||||
});
|
Loading…
Reference in New Issue
Block a user