Introducing Gradio 5.0

Read More
  1. Modals
  2. Warning

New to Gradio? Start here: Getting Started

See the Release History

To install Gradio from main, run the following command:

pip install https://gradio-builds.s3.amazonaws.com/ac9bf5ec9208b92579f36ee94a247ae3e676c02f/gradio-5.6.0-py3-none-any.whl

*Note: Setting share=True in launch() will not work.

Warning

gradio.Warning("A warning occured ⛔!", duration=5)

Description

This function allows you to pass custom warning messages to the user. You can do so simply by writing gr.Warning('message here') in your function, and when that line is executed the custom message will appear in a modal on the demo. The modal is yellow by default and has the heading: "Warning." Queue must be enabled for this behavior; otherwise, the warning will be printed to the console using the warnings library.

Example Usage

import gradio as gr
def hello_world():
    gr.Warning('This is a warning message.')
    return "hello world"
with gr.Blocks() as demo:
    md = gr.Markdown()
    demo.load(hello_world, inputs=None, outputs=[md])
demo.queue().launch()

Initialization

Parameters
message: str
default = "Warning issued."

The warning message to be displayed to the user. Can be HTML, which will be rendered in the modal.

duration: float | None
default = 10

The duration in seconds that the warning message should be displayed for. If None or 0, the message will be displayed indefinitely until the user closes it.

visible: bool
default = True

Whether the error message should be displayed in the UI.

title: str
default = "Warning"

The title to be displayed to the user at the top of the modal.

Demos

import gradio as gr

def failure():
    raise gr.Error("This should fail!")

def exception():
    raise ValueError("Something went wrong")

def success():
    return True

def warning_fn():
    gr.Warning("This is a warning!")

def info_fn():
    gr.Info("This is some info")

with gr.Blocks() as demo:
    gr.Markdown("Used in E2E tests of success event trigger. The then event covered in chatbot E2E tests."
                " Also testing that the status modals show up.")
    with gr.Row():
        result = gr.Textbox(label="Result")
        result_2 = gr.Textbox(label="Consecutive Event")
    with gr.Row():
        success_btn = gr.Button(value="Trigger Success")
        success_btn_2 = gr.Button(value="Trigger Consecutive Success")
        failure_btn = gr.Button(value="Trigger Failure")
        failure_exception = gr.Button(value="Trigger Failure With ValueError")
    with gr.Row():
        trigger_warning = gr.Button(value="Trigger Warning")
        trigger_info = gr.Button(value="Trigger Info")

        success_btn_2.click(success, None, None).success(lambda: "First Event Trigered", None, result).success(lambda: "Consecutive Event Triggered", None, result_2)
        success_btn.click(success, None, None).success(lambda: "Success event triggered", inputs=None, outputs=result)
        failure_btn.click(failure, None, None).success(lambda: "Should not be triggered", inputs=None, outputs=result)
        failure_exception.click(exception, None, None)
        trigger_warning.click(warning_fn, None, None)
        trigger_info.click(info_fn, None, None)

if __name__ == "__main__":
    demo.launch(show_error=True)