Skip to content

Commit d023b2e

Browse files
Adds docs for gr.api() which were previously missing from the website (#11173)
* changes * add changeset * add changeset * js --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent b618571 commit d023b2e

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed

.changeset/eleven-rabbits-obey.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"gradio": patch
3+
"website": patch
4+
---
5+
6+
feat:Adds docs for `gr.api()` which were previously missing from the website

gradio/events.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -932,13 +932,20 @@ def api(
932932
input = gr.Textbox()
933933
button = gr.Button("Submit")
934934
output = gr.Textbox()
935-
gr.on(
936-
triggers=[button.click, input.submit],
937-
fn=lambda x: x,
938-
inputs=[input],
939-
outputs=[output]
940-
)
941-
demo.launch()
935+
def fn(a: int, b: int, c: list[str]) -> tuple[int, str]:
936+
return a + b, c[a:b]
937+
gr.api(fn, api_name="add_and_slice")
938+
_, url, _ = demo.launch()
939+
940+
from gradio_client import Client
941+
client = Client(url)
942+
result = client.predict(
943+
a=3,
944+
b=5,
945+
c=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
946+
api_name="/add_and_slice"
947+
)
948+
print(result)
942949
"""
943950
if fn == "decorator":
944951

guides/04_additional-features/14_view-api-page.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,40 @@ btn.click(add, [num1, num2], output, api_name=False)
3232

3333
Note: setting an `api_name=False` also means that downstream apps will not be able to load your Gradio app using `gr.load()` as this function uses the Gradio API under the hood.
3434

35+
**Adding API endpoints**
36+
37+
You can also add new API routes to your Gradio application that do not correspond to events in your UI.
38+
39+
For example, in this Gradio applicaiton, we add a new route that adds numbers and slices a list:
40+
41+
```py
42+
import gradio as gr
43+
with gr.Blocks() as demo:
44+
with gr.Row():
45+
input = gr.Textbox()
46+
button = gr.Button("Submit")
47+
output = gr.Textbox()
48+
def fn(a: int, b: int, c: list[str]) -> tuple[int, str]:
49+
return a + b, c[a:b]
50+
gr.api(fn, api_name="add_and_slice")
51+
52+
_, url, _ = demo.launch()
53+
```
54+
55+
This will create a new route `/add_and_slice` which will show up in the "view API" page. It can be programmatically called by the Python or JS Clients (discussed below) like this:
56+
57+
```py
58+
from gradio_client import Client
59+
60+
client = Client(url)
61+
result = client.predict(
62+
a=3,
63+
b=5,
64+
c=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
65+
api_name="/add_and_slice"
66+
)
67+
print(result)
68+
```
3569

3670
## The Clients
3771

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
<script lang="ts">
3+
import {get_object} from "../../process_json.ts";
4+
import ParamTable from "$lib/components/ParamTable.svelte";
5+
import ShortcutTable from "$lib/components/ShortcutTable.svelte";
6+
import DemosSection from "$lib/components/DemosSection.svelte";
7+
import FunctionsSection from "$lib/components/FunctionsSection.svelte";
8+
import GuidesSection from "$lib/components/GuidesSection.svelte";
9+
import CopyButton from "$lib/components/CopyButton.svelte";
10+
import { style_formatted_text } from "$lib/text";
11+
12+
let obj = get_object("api");
13+
</script>
14+
15+
<!--- Title -->
16+
# {obj.name}
17+
18+
<!--- Usage -->
19+
```python
20+
gradio.api(···)
21+
```
22+
23+
<!--- Description -->
24+
### Description
25+
## {@html style_formatted_text(obj.description)}
26+
27+
<!-- Example Usage -->
28+
29+
{#if obj.example}
30+
### Example Usage
31+
```python
32+
import gradio as gr
33+
with gr.Blocks() as demo:
34+
with gr.Row():
35+
input = gr.Textbox()
36+
button = gr.Button("Submit")
37+
output = gr.Textbox()
38+
def fn(a: int, b: int, c: list[str]) -> tuple[int, str]:
39+
return a + b, c[a:b]
40+
gr.api(fn, api_name="add_and_slice")
41+
_, url, _ = demo.launch()
42+
43+
from gradio_client import Client
44+
client = Client(url)
45+
result = client.predict(
46+
a=3,
47+
b=5,
48+
c=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
49+
api_name="/add_and_slice"
50+
)
51+
print(result)
52+
```
53+
{/if}
54+
55+
<!--- Initialization -->
56+
### Initialization
57+
<ParamTable parameters={obj.parameters} anchor_links={obj.name}/>
58+
59+
60+
{#if obj.demos && obj.demos.length > 0}
61+
<!--- Demos -->
62+
### Demos
63+
<DemosSection demos={obj.demos} />
64+
{/if}
65+
66+
{#if obj.fns && obj.fns.length > 0}
67+
<!--- Methods -->
68+
### Methods
69+
<FunctionsSection fns={obj.fns} event_listeners={false} />
70+
{/if}
71+
72+
{#if obj.guides && obj.guides.length > 0}
73+
<!--- Guides -->
74+
### Guides
75+
<GuidesSection guides={obj.guides}/>
76+
{/if}

0 commit comments

Comments
 (0)