text
stringlengths
0
2k
heading1
stringlengths
4
79
source_page_url
stringclasses
182 values
source_page_title
stringclasses
182 values
direct_uri = urlunparse(urlparse(str(redirect_uri))._replace(scheme='https')) return await oauth.google.authorize_redirect(request, redirect_uri) @app.route('/auth') async def auth(request: Request): try: access_token = await oauth.google.authorize_access_token(request) except OAuthError: return RedirectResponse(url='/') request.session['user'] = dict(access_token)["userinfo"] return RedirectResponse(url='/') with gr.Blocks() as login_demo: gr.Button("Login", link="/login") app = gr.mount_gradio_app(app, login_demo, path="/login-demo") def greet(request: gr.Request): return f"Welcome to Gradio, {request.username}" with gr.Blocks() as main_demo: m = gr.Markdown("Welcome to Gradio!") gr.Button("Logout", link="/logout") main_demo.load(greet, None, m) app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user) if __name__ == '__main__': uvicorn.run(app) ``` There are actually two separate Gradio apps in this example! One that simply displays a log in button (this demo is accessible to any user), while the other main demo is only accessible to users that are logged in. You can try this example out on [this Space](https://huggingface.co/spaces/gradio/oauth-example).
Authentication
https://gradio.app/guides/sharing-your-app
Additional Features - Sharing Your App Guide
Gradio apps can function as MCP (Model Context Protocol) servers, allowing LLMs to use your app's functions as tools. By simply setting `mcp_server=True` in the `.launch()` method, Gradio automatically converts your app's functions into MCP tools that can be called by MCP clients like Claude Desktop, Cursor, or Cline. The server exposes tools based on your function names, docstrings, and type hints, and can handle file uploads, authentication headers, and progress updates. You can also create MCP-only functions using `gr.api` and expose resources and prompts using decorators. For a comprehensive guide on building MCP servers with Gradio, see [Building an MCP Server with Gradio](https://www.gradio.app/guides/building-mcp-server-with-gradio).
MCP Servers
https://gradio.app/guides/sharing-your-app
Additional Features - Sharing Your App Guide
When publishing your app publicly, and making it available via API or via MCP server, you might want to set rate limits to prevent users from abusing your app. You can identify users using their IP address (using the `gr.Request` object [as discussed above](accessing-the-network-request-directly)) or, if they are logged in via Hugging Face OAuth, using their username. To see a complete example of how to set rate limits, please see [this Gradio app](https://github.com/gradio-app/gradio/blob/main/demo/rate_limit/run.py).
Rate Limits
https://gradio.app/guides/sharing-your-app
Additional Features - Sharing Your App Guide
By default, Gradio collects certain analytics to help us better understand the usage of the `gradio` library. This includes the following information: * What environment the Gradio app is running on (e.g. Colab Notebook, Hugging Face Spaces) * What input/output components are being used in the Gradio app * Whether the Gradio app is utilizing certain advanced features, such as `auth` or `show_error` * The IP address which is used solely to measure the number of unique developers using Gradio * The version of Gradio that is running No information is collected from _users_ of your Gradio app. If you'd like to disable analytics altogether, you can do so by setting the `analytics_enabled` parameter to `False` in `gr.Blocks`, `gr.Interface`, or `gr.ChatInterface`. Or, you can set the GRADIO_ANALYTICS_ENABLED environment variable to `"False"` to apply this to all Gradio apps created across your system. *Note*: this reflects the analytics policy as of `gradio>=4.32.0`.
Analytics
https://gradio.app/guides/sharing-your-app
Additional Features - Sharing Your App Guide
[Progressive Web Apps (PWAs)](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps) are web applications that are regular web pages or websites, but can appear to the user like installable platform-specific applications. Gradio apps can be easily served as PWAs by setting the `pwa=True` parameter in the `launch()` method. Here's an example: ```python import gradio as gr def greet(name): return "Hello " + name + "!" demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox") demo.launch(pwa=True) Launch your app as a PWA ``` This will generate a PWA that can be installed on your device. Here's how it looks: ![Installing PWA](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/install-pwa.gif) When you specify `favicon_path` in the `launch()` method, the icon will be used as the app's icon. Here's an example: ```python demo.launch(pwa=True, favicon_path="./hf-logo.svg") Use a custom icon for your PWA ``` ![Custom PWA Icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/pwa-favicon.png)
Progressive Web App (PWA)
https://gradio.app/guides/sharing-your-app
Additional Features - Sharing Your App Guide
You can initialize the `I18n` class with multiple language dictionaries to add custom translations: ```python import gradio as gr Create an I18n instance with translations for multiple languages i18n = gr.I18n( en={"greeting": "Hello, welcome to my app!", "submit": "Submit"}, es={"greeting": "¡Hola, bienvenido a mi aplicación!", "submit": "Enviar"}, fr={"greeting": "Bonjour, bienvenue dans mon application!", "submit": "Soumettre"} ) with gr.Blocks() as demo: Use the i18n method to translate the greeting gr.Markdown(i18n("greeting")) with gr.Row(): input_text = gr.Textbox(label="Input") output_text = gr.Textbox(label="Output") submit_btn = gr.Button(i18n("submit")) Pass the i18n instance to the launch method demo.launch(i18n=i18n) ```
Setting Up Translations
https://gradio.app/guides/internationalization
Additional Features - Internationalization Guide
When you use the `i18n` instance with a translation key, Gradio will show the corresponding translation to users based on their browser's language settings or the language they've selected in your app. If a translation isn't available for the user's locale, the system will fall back to English (if available) or display the key itself.
How It Works
https://gradio.app/guides/internationalization
Additional Features - Internationalization Guide
Locale codes should follow the BCP 47 format (e.g., 'en', 'en-US', 'zh-CN'). The `I18n` class will warn you if you use an invalid locale code.
Valid Locale Codes
https://gradio.app/guides/internationalization
Additional Features - Internationalization Guide
The following component properties typically support internationalization: - `description` - `info` - `title` - `placeholder` - `value` - `label` Note that support may vary depending on the component, and some properties might have exceptions where internationalization is not applicable. You can check this by referring to the typehint for the parameter and if it contains `I18nData`, then it supports internationalization.
Supported Component Properties
https://gradio.app/guides/internationalization
Additional Features - Internationalization Guide
Client side functions are ideal for updating component properties (like visibility, placeholders, interactive state, or styling). Here's a basic example: ```py import gradio as gr with gr.Blocks() as demo: with gr.Row() as row: btn = gr.Button("Hide this row") This function runs in the browser without a server roundtrip btn.click( lambda: gr.Row(visible=False), None, row, js=True ) demo.launch() ```
When to Use Client Side Functions
https://gradio.app/guides/client-side-functions
Additional Features - Client Side Functions Guide
Client side functions have some important restrictions: * They can only update component properties (not values) * They cannot take any inputs Here are some functions that will work with `js=True`: ```py Simple property updates lambda: gr.Textbox(lines=4) Multiple component updates lambda: [gr.Textbox(lines=4), gr.Button(interactive=False)] Using gr.update() for property changes lambda: gr.update(visible=True, interactive=False) ``` We are working to increase the space of functions that can be transpiled to JavaScript so that they can be run in the browser. [Follow the Groovy library for more info](https://github.com/abidlabs/groovy-transpiler).
Limitations
https://gradio.app/guides/client-side-functions
Additional Features - Client Side Functions Guide
Here's a more complete example showing how client side functions can improve the user experience: $code_todo_list_js
Complete Example
https://gradio.app/guides/client-side-functions
Additional Features - Client Side Functions Guide
When you set `js=True`, Gradio: 1. Transpiles your Python function to JavaScript 2. Runs the function directly in the browser 3. Still sends the request to the server (for consistency and to handle any side effects) This provides immediate visual feedback while ensuring your application state remains consistent.
Behind the Scenes
https://gradio.app/guides/client-side-functions
Additional Features - Client Side Functions Guide