text
stringlengths 0
2k
| heading1
stringlengths 4
79
| source_page_url
stringclasses 183
values | source_page_title
stringclasses 183
values |
|---|---|---|---|
**API endpoint names**
When you create a Gradio application, the API endpoint names are automatically generated based on the function names. You can change this by using the `api_name` parameter in `gr.Interface` or `gr.ChatInterface`. If you are using Gradio `Blocks`, you can name each event listener, like this:
```python
btn.click(add, [num1, num2], output, api_name="addition")
```
**Hiding API endpoints**
When building a complex Gradio app, you might want to hide certain API endpoints from appearing on the view API page, e.g. if they correspond to functions that simply update the UI. You can set the `show_api` parameter to `False` in any `Blocks` event listener to achieve this, e.g.
```python
btn.click(add, [num1, num2], output, show_api=False)
```
**Disabling API endpoints**
Hiding the API endpoint doesn't disable it. A user can still programmatically call the API endpoint if they know the name. If you want to disable an API endpoint altogether, set `api_name=False`, e.g.
```python
btn.click(add, [num1, num2], output, api_name=False)
```
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.
**Adding API endpoints**
You can also add new API routes to your Gradio application that do not correspond to events in your UI.
For example, in this Gradio application, we add a new route that adds numbers and slices a list:
```py
import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
input = gr.Textbox()
button = gr.Button("Submit")
output = gr.Textbox()
def fn(a: int, b: int, c: list[str]) -> tuple[int, str]:
return a + b, c[a:b]
gr.api(fn, api_name="add_and_slice")
_, url, _ = demo.launch()
```
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:
```py
from grad
|
Configuring the API Page
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
``
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:
```py
from gradio_client import Client
client = Client(url)
result = client.predict(
a=3,
b=5,
c=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
api_name="/add_and_slice"
)
print(result)
```
|
Configuring the API Page
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
This API page not only lists all of the endpoints that can be used to query the Gradio app, but also shows the usage of both [the Gradio Python client](https://gradio.app/guides/getting-started-with-the-python-client/), and [the Gradio JavaScript client](https://gradio.app/guides/getting-started-with-the-js-client/).
For each endpoint, Gradio automatically generates a complete code snippet with the parameters and their types, as well as example inputs, allowing you to immediately test an endpoint. Here's an example showing an image file input and `str` output:

|
The Clients
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
Instead of reading through the view API page, you can also use Gradio's built-in API recorder to generate the relevant code snippet. Simply click on the "API Recorder" button, use your Gradio app via the UI as you would normally, and then the API Recorder will generate the code using the Clients to recreate your all of your interactions programmatically.

|
The API Recorder 🪄
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
The API page also includes instructions on how to use the Gradio app as an Model Context Protocol (MCP) server, which is a standardized way to expose functions as tools so that they can be used by LLMs.

For the MCP sever, each tool, its description, and its parameters are listed, along with instructions on how to integrate with popular MCP Clients. Read more about Gradio's [MCP integration here](https://www.gradio.app/guides/building-mcp-server-with-gradio).
|
MCP Server
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
You can access the complete OpenAPI (formerly Swagger) specification of your Gradio app's API at the endpoint `<your-gradio-app-url>/gradio_api/openapi.json`. The OpenAPI specification is a standardized, language-agnostic interface description for REST APIs that enables both humans and computers to discover and understand the capabilities of your service.
|
OpenAPI Specification
|
https://gradio.app/guides/view-api-page
|
Additional Features - View Api Page Guide
|
To add custom buttons to a component, pass a list of `gr.Button()` instances to the `buttons` parameter:
```python
import gradio as gr
refresh_btn = gr.Button("Refresh", variant="secondary", size="sm")
clear_btn = gr.Button("Clear", variant="secondary", size="sm")
textbox = gr.Textbox(
value="Sample text",
label="Text Input",
buttons=[refresh_btn, clear_btn]
)
```
You can also mix built-in buttons (as strings) with custom buttons:
```python
code = gr.Code(
value="print('Hello')",
language="python",
buttons=["copy", "download", refresh_btn, export_btn]
)
```
|
Basic Usage
|
https://gradio.app/guides/custom-buttons
|
Additional Features - Custom Buttons Guide
|
Custom buttons work just like regular `gr.Button` components. You can connect them to Python functions or JavaScript functions using the `.click()` method:
Python Functions
```python
def refresh_data():
import random
return f"Refreshed: {random.randint(1000, 9999)}"
refresh_btn.click(refresh_data, outputs=textbox)
```
JavaScript Functions
```python
clear_btn.click(
None,
inputs=[],
outputs=textbox,
js="() => ''"
)
```
Combined Python and JavaScript
You can use the same button for both Python and JavaScript logic:
```python
alert_btn.click(
None,
inputs=textbox,
outputs=[],
js="(text) => { alert('Text: ' + text); return []; }"
)
```
|
Connecting Button Events
|
https://gradio.app/guides/custom-buttons
|
Additional Features - Custom Buttons Guide
|
Here's a complete example showing custom buttons with both Python and JavaScript functions:
$code_textbox_custom_buttons
|
Complete Example
|
https://gradio.app/guides/custom-buttons
|
Additional Features - Custom Buttons Guide
|
- Custom buttons appear in the component's toolbar, typically in the top-right corner
- Only the `value` of the Button is used, other attributes like `icon` are not used.
- Buttons are rendered in the order they appear in the `buttons` list
- Built-in buttons (like "copy", "download") can be hidden by omitting them from the list
- Custom buttons work with component events in the same way as as regular buttons
|
Notes
|
https://gradio.app/guides/custom-buttons
|
Additional Features - Custom Buttons Guide
|
1. `GRADIO_SERVER_PORT`
- **Description**: Specifies the port on which the Gradio app will run.
- **Default**: `7860`
- **Example**:
```bash
export GRADIO_SERVER_PORT=8000
```
2. `GRADIO_SERVER_NAME`
- **Description**: Defines the host name for the Gradio server. To make Gradio accessible from any IP address, set this to `"0.0.0.0"`
- **Default**: `"127.0.0.1"`
- **Example**:
```bash
export GRADIO_SERVER_NAME="0.0.0.0"
```
3. `GRADIO_NUM_PORTS`
- **Description**: Defines the number of ports to try when starting the Gradio server.
- **Default**: `100`
- **Example**:
```bash
export GRADIO_NUM_PORTS=200
```
4. `GRADIO_ANALYTICS_ENABLED`
- **Description**: Whether Gradio should provide
- **Default**: `"True"`
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_ANALYTICS_ENABLED="True"
```
5. `GRADIO_DEBUG`
- **Description**: Enables or disables debug mode in Gradio. If debug mode is enabled, the main thread does not terminate allowing error messages to be printed in environments such as Google Colab.
- **Default**: `0`
- **Example**:
```sh
export GRADIO_DEBUG=1
```
6. `GRADIO_FLAGGING_MODE`
- **Description**: Controls whether users can flag inputs/outputs in the Gradio interface. See [the Guide on flagging](/guides/using-flagging) for more details.
- **Default**: `"manual"`
- **Options**: `"never"`, `"manual"`, `"auto"`
- **Example**:
```sh
export GRADIO_FLAGGING_MODE="never"
```
7. `GRADIO_TEMP_DIR`
- **Description**: Specifies the directory where temporary files created by Gradio are stored.
- **Default**: System default temporary directory
- **Example**:
```sh
export GRADIO_TEMP_DIR="/path/to/temp"
```
8. `GRADIO_ROOT_PATH`
- **Description**: Sets the root path for the Gradio application. Useful if running Gradio [behind a reverse proxy](/guides/running-gradio-on-your-web-server-with-nginx).
- **Default**: `""`
- **Example**:
```sh
export GRADIO_ROOT_PATH=
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
r the Gradio application. Useful if running Gradio [behind a reverse proxy](/guides/running-gradio-on-your-web-server-with-nginx).
- **Default**: `""`
- **Example**:
```sh
export GRADIO_ROOT_PATH="/myapp"
```
9. `GRADIO_SHARE`
- **Description**: Enables or disables sharing the Gradio app.
- **Default**: `"False"`
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_SHARE="True"
```
10. `GRADIO_ALLOWED_PATHS`
- **Description**: Sets a list of complete filepaths or parent directories that gradio is allowed to serve. Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app. Multiple items can be specified by separating items with commas.
- **Default**: `""`
- **Example**:
```sh
export GRADIO_ALLOWED_PATHS="/mnt/sda1,/mnt/sda2"
```
11. `GRADIO_BLOCKED_PATHS`
- **Description**: Sets a list of complete filepaths or parent directories that gradio is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default. Multiple items can be specified by separating items with commas.
- **Default**: `""`
- **Example**:
```sh
export GRADIO_BLOCKED_PATHS="/users/x/gradio_app/admin,/users/x/gradio_app/keys"
```
12. `FORWARDED_ALLOW_IPS`
- **Description**: This is not a Gradio-specific environment variable, but rather one used in server configurations, specifically `uvicorn` which is used by Gradio internally. This environment variable is useful when deploying applications behind a reverse proxy. It defines a list of IP addresses that are trusted to forward traffic to your application. When set, the application will trust the `X-Forwarded-For` header from these IP addresses to determine the original IP address of the user making the request. This means that if you use the `gr.Request` [objec
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
the application will trust the `X-Forwarded-For` header from these IP addresses to determine the original IP address of the user making the request. This means that if you use the `gr.Request` [object's](https://www.gradio.app/docs/gradio/request) `client.host` property, it will correctly get the user's IP address instead of the IP address of the reverse proxy server. Note that only trusted IP addresses (i.e. the IP addresses of your reverse proxy servers) should be added, as any server with these IP addresses can modify the `X-Forwarded-For` header and spoof the client's IP address.
- **Default**: `"127.0.0.1"`
- **Example**:
```sh
export FORWARDED_ALLOW_IPS="127.0.0.1,192.168.1.100"
```
13. `GRADIO_CACHE_EXAMPLES`
- **Description**: Whether or not to cache examples by default in `gr.Interface()`, `gr.ChatInterface()` or in `gr.Examples()` when no explicit argument is passed for the `cache_examples` parameter. You can set this environment variable to either the string "true" or "false".
- **Default**: `"false"`
- **Example**:
```sh
export GRADIO_CACHE_EXAMPLES="true"
```
14. `GRADIO_CACHE_MODE`
- **Description**: How to cache examples. Only applies if `cache_examples` is set to `True` either via enviornment variable or by an explicit parameter, AND no no explicit argument is passed for the `cache_mode` parameter in `gr.Interface()`, `gr.ChatInterface()` or in `gr.Examples()`. Can be set to either the strings "lazy" or "eager." If "lazy", examples are cached after their first use for all users of the app. If "eager", all examples are cached at app launch.
- **Default**: `"eager"`
- **Example**:
```sh
export GRADIO_CACHE_MODE="lazy"
```
15. `GRADIO_EXAMPLES_CACHE`
- **Description**: If you set `cache_examples=True` in `gr.Interface()`, `gr.ChatInterface()` or in `gr.Examples()`, Gradio will run your prediction function and save the results to disk. By default, this is in the `.gradio/cached_examples//` subdirectory within your
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
e()`, `gr.ChatInterface()` or in `gr.Examples()`, Gradio will run your prediction function and save the results to disk. By default, this is in the `.gradio/cached_examples//` subdirectory within your app's working directory. You can customize the location of cached example files created by Gradio by setting the environment variable `GRADIO_EXAMPLES_CACHE` to an absolute path or a path relative to your working directory.
- **Default**: `".gradio/cached_examples/"`
- **Example**:
```sh
export GRADIO_EXAMPLES_CACHE="custom_cached_examples/"
```
16. `GRADIO_SSR_MODE`
- **Description**: Controls whether server-side rendering (SSR) is enabled. When enabled, the initial HTML is rendered on the server rather than the client, which can improve initial page load performance and SEO.
- **Default**: `"False"` (except on Hugging Face Spaces, where this environment variable sets it to `True`)
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_SSR_MODE="True"
```
17. `GRADIO_NODE_SERVER_NAME`
- **Description**: Defines the host name for the Gradio node server. (Only applies if `ssr_mode` is set to `True`.)
- **Default**: `GRADIO_SERVER_NAME` if it is set, otherwise `"127.0.0.1"`
- **Example**:
```sh
export GRADIO_NODE_SERVER_NAME="0.0.0.0"
```
18. `GRADIO_NODE_NUM_PORTS`
- **Description**: Defines the number of ports to try when starting the Gradio node server. (Only applies if `ssr_mode` is set to `True`.)
- **Default**: `100`
- **Example**:
```sh
export GRADIO_NODE_NUM_PORTS=200
```
19. `GRADIO_RESET_EXAMPLES_CACHE`
- **Description**: If set to "True", Gradio will delete and recreate the examples cache directory when the app starts instead of reusing the cached example if they already exist.
- **Default**: `"False"`
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_RESET_EXAMPLES_CACHE="True"
```
20. `GRADIO_CHAT_FLAGGING_MODE`
- **Description**: Controls whether users can flag
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
e"`
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_RESET_EXAMPLES_CACHE="True"
```
20. `GRADIO_CHAT_FLAGGING_MODE`
- **Description**: Controls whether users can flag messages in `gr.ChatInterface` applications. Similar to `GRADIO_FLAGGING_MODE` but specifically for chat interfaces.
- **Default**: `"never"`
- **Options**: `"never"`, `"manual"`
- **Example**:
```sh
export GRADIO_CHAT_FLAGGING_MODE="manual"
```
21. `GRADIO_WATCH_DIRS`
- **Description**: Specifies directories to watch for file changes when running Gradio in development mode. When files in these directories change, the Gradio app will automatically reload. Multiple directories can be specified by separating them with commas. This is primarily used by the `gradio` CLI command for development workflows.
- **Default**: `""`
- **Example**:
```sh
export GRADIO_WATCH_DIRS="/path/to/src,/path/to/templates"
```
22. `GRADIO_VIBE_MODE`
- **Description**: Enables the Vibe editor mode, which provides an in-browser chat that can be used to write or edit your Gradio app using natural language. When enabled, anyone who can access the Gradio endpoint can modify files and run arbitrary code on the host machine. Use with extreme caution in production environments.
- **Default**: `""`
- **Options**: Any non-empty string enables the mode
- **Example**:
```sh
export GRADIO_VIBE_MODE="1"
```
23. `GRADIO_MCP_SERVER`
- **Description**: Enables the MCP (Model Context Protocol) server functionality in Gradio. When enabled, the Gradio app will be set up as an MCP server and documented functions will be added as MCP tools that can be used by LLMs. This allows LLMs to interact with your Gradio app's functionality through the MCP protocol.
- **Default**: `"False"`
- **Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_MCP_SERVER="True"
```
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
*Options**: `"True"`, `"False"`
- **Example**:
```sh
export GRADIO_MCP_SERVER="True"
```
|
Key Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
To set environment variables in your terminal, use the `export` command followed by the variable name and its value. For example:
```sh
export GRADIO_SERVER_PORT=8000
```
If you're using a `.env` file to manage your environment variables, you can add them like this:
```sh
GRADIO_SERVER_PORT=8000
GRADIO_SERVER_NAME="localhost"
```
Then, use a tool like `dotenv` to load these variables when running your application.
|
How to Set Environment Variables
|
https://gradio.app/guides/environment-variables
|
Additional Features - Environment Variables Guide
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.