Skip to content

Commit

Permalink
Add visible parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyaboulton committed Jun 11, 2024
1 parent 09b6515 commit b9dd4a0
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client/js/src/helpers/api_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export function handle_message(
status: {
queue,
message: data.output.error as string,
display: data.output.display as boolean,
visible: data.output.visible as boolean,
duration: data.output.duration as number,
stage: "error",
code: data.code,
Expand Down
3 changes: 2 additions & 1 deletion client/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export interface Status {
success?: boolean;
stage: "pending" | "error" | "complete" | "generating";
duration?: number;
display?: boolean;
visible?: boolean;
broken?: boolean;
size?: number;
position?: number;
Expand Down Expand Up @@ -364,6 +364,7 @@ export interface LogMessage extends Log {
endpoint: string;
fn_index: number;
duration: number | null;
visible: boolean;
}

export interface RenderMessage extends Render {
Expand Down
3 changes: 3 additions & 0 deletions client/js/src/utils/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export function submit(
level: data.level,
endpoint: _endpoint,
duration: data.duration,
visible: data.visible,
fn_index
});
} else if (type === "generating") {
Expand Down Expand Up @@ -476,6 +477,7 @@ export function submit(
level: data.level,
endpoint: _endpoint,
duration: data.duration,
visible: data.visible,
fn_index
});
} else if (type === "generating") {
Expand Down Expand Up @@ -633,6 +635,7 @@ export function submit(
level: data.level,
endpoint: _endpoint,
duration: data.duration,
visible: data.visible,
fn_index
});
return;
Expand Down
5 changes: 4 additions & 1 deletion gradio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ def __init__(
self,
message: str = "Error raised.",
duration: int | None = 10,
visible: bool = True,
):
"""
Parameters:
message: The error message to be displayed to the user.
duration: The duration in seconds to display the error message. If None, the error message will be displayed until the user closes it. If 0, the error message will not be displayed in the UI but will still be raised as an exception.
duration: The duration in seconds to display the error message. If None or 0, the error message will be displayed until the user closes it.
visible: Whether the error message should be displayed in the UI.
"""
self.message = message
self.duration = duration
self.visible = visible
super().__init__(self.message)

def __str__(self):
Expand Down
19 changes: 12 additions & 7 deletions gradio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ def log_message(
message: str,
level: Literal["info", "warning"] = "info",
duration: int | None = 10,
visible: bool = True,
):
from gradio.context import LocalContext

Expand All @@ -1244,20 +1245,21 @@ def log_message(
warnings.warn(message)
return
blocks._queue.log_message(
event_id=event_id, log=message, level=level, duration=duration
event_id=event_id, log=message, level=level, duration=duration, visible=visible
)


@document(documentation_group="modals")
def Warning( # noqa: N802
message: str = "Warning issued.", duration: int | None = 10
message: str = "Warning issued.", duration: int | None = 10, visible: bool = True
):
"""
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.
Demos: blocks_chained_events
Parameters:
message: The warning message to be displayed to the user.
duration: The duration in seconds that the warning message should be displayed for. If None, the message will be displayed indefinitely until the user closes it. If 0, the error message will not be displayed in the UI but will still be logged to the console.
duration: 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: Whether the error message should be displayed in the UI.
Example:
import gradio as gr
def hello_world():
Expand All @@ -1268,19 +1270,22 @@ def hello_world():
demo.load(hello_world, inputs=None, outputs=[md])
demo.queue().launch()
"""
log_message(message, level="warning", duration=duration)
log_message(message, level="warning", duration=duration, visible=visible)


@document(documentation_group="modals")
def Info( # noqa: N802
message: str = "Info issued.", duration: int | None = 10
message: str = "Info issued.",
duration: int | None = 10,
visible: bool = True,
):
"""
This function allows you to pass custom info messages to the user. You can do so simply by writing `gr.Info('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 gray by default and has the heading: "Info." Queue must be enabled for this behavior; otherwise, the message will be printed to the console.
Demos: blocks_chained_events
Parameters:
message: The info message to be displayed to the user.
duration: The duration in seconds that the info message should be displayed for. If None, the message will be displayed indefinitely until the user closes it. If 0, the error message will not be displayed in the UI but will still be logged to the console.
duration: The duration in seconds that the info message should be displayed for. If None or 0, the message will be displayed indefinitely until the user closes it.
visible: Whether the error message should be displayed in the UI.
Example:
import gradio as gr
def hello_world():
Expand All @@ -1291,4 +1296,4 @@ def hello_world():
demo.load(hello_world, inputs=None, outputs=[md])
demo.queue().launch()
"""
log_message(message, level="info", duration=duration)
log_message(message, level="info", duration=duration, visible=visible)
4 changes: 3 additions & 1 deletion gradio/queueing.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ def log_message(
event_id: str,
log: str,
level: Literal["info", "warning"],
duration: int | None = 10000,
duration: int | None = 10,
visible: bool = True,
):
events = [
evt for job in self.active_jobs if job is not None for evt in job
Expand All @@ -392,6 +393,7 @@ def log_message(
log=log,
level=level,
duration=duration,
visible=visible,
)
self.send_message(event, log_message)

Expand Down
1 change: 1 addition & 0 deletions gradio/server_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LogMessage(BaseMessage):
log: str
level: Literal["info", "warning"]
duration: Optional[int] = 10
visible: bool = True


class EstimationMessage(BaseMessage):
Expand Down
1 change: 1 addition & 0 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,4 +1429,5 @@ def error_payload(
content["error"] = str(error)
if isinstance(error, Error):
content["duration"] = error.duration
content["visible"] = error.visible
return content
2 changes: 1 addition & 1 deletion js/_website/src/lib/templates/gradio/05_modals/error.svx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ raise gradio.Error("An error occured 💥!", duration=5)
## {@html style_formatted_text(obj.description)}

## You can control for how long the error message is displayed with the `duration` parameter. If it's `None`, the message will be displayed forever until the user closes it. If it's a number, it will be shown for that many seconds.

## You can also hide the error modal from being shown in the UI by setting `visible=False`.
## Below is a demo of how different values of duration control the error, info, and warning messages. You can see the code [here](https://huggingface.co/spaces/freddyaboulton/gradio-error-duration/blob/244331cf53f6b5fa2fd406ece3bf55c6ccb9f5f2/app.py#L17).

![modal_control](https://github.com/gradio-app/gradio/assets/41651716/f0977bcd-eaec-4eca-a2fd-ede95fdb8fd2)
Expand Down
21 changes: 16 additions & 5 deletions js/app/src/Blocks.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@
message: string,
fn_index: number,
type: ToastMessage["type"],
duration: number | null = 10
duration: number | null = 10,
visible = true
): ToastMessage & { fn_index: number } {
return {
message,
fn_index,
type,
id: ++_error_id,
duration
duration,
visible
};
}
Expand Down Expand Up @@ -346,8 +348,11 @@
}
function handle_log(msg: LogMessage): void {
const { log, fn_index, level, duration } = msg;
messages = [new_message(log, fn_index, level, duration), ...messages];
const { log, fn_index, level, duration, visible } = msg;
messages = [
new_message(log, fn_index, level, duration, visible),
...messages
];
}
function handle_status_update(message: StatusMessage): void {
Expand Down Expand Up @@ -419,7 +424,13 @@
(_, b) => b
);
messages = [
new_message(_message, fn_index, "error", status.duration),
new_message(
_message,
fn_index,
"error",
status.duration,
status.visible
),
...messages
];
}
Expand Down
4 changes: 2 additions & 2 deletions js/statustracker/static/Toast.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</script>

<div class="toast-wrap">
{#each messages as { type, message, id, duration } (id)}
{#each messages as { type, message, id, duration, visible } (id)}
<div animate:flip={{ duration: 300 }} style:width="100%">
<ToastContent {type} {message} {duration} on:close {id} />
<ToastContent {type} {message} {duration} {visible} on:close {id} />
</div>
{/each}
</div>
Expand Down
6 changes: 4 additions & 2 deletions js/statustracker/static/ToastContent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
export let message = "";
export let type: ToastMessage["type"];
export let id: number;
export let duration: number | null = 10; // Add duration prop
export let duration: number | null = 10;
export let visible = true;
$: display = duration ? duration > 0 : true;
$: display = visible;
$: duration = duration || null;
const dispatch = createEventDispatcher();
Expand Down
1 change: 1 addition & 0 deletions js/statustracker/static/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface ToastMessage {
message: string;
id: number;
duration: number | null;
visible: boolean;
}

0 comments on commit b9dd4a0

Please sign in to comment.