Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control Display of Error, Info, Warning #8489

Merged
merged 15 commits into from
Jun 11, 2024
Merged

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Jun 7, 2024

Description

Adds duration property to gr.Warning, gr.Info, and gr.Error.

Setting duration to 0 not display the toast in the UI. This is useful for giving useful error messages for clients. For example, the data validation done in the component classes can now raise gr.Error instead of TypeError or ValueError and clients will see the exact error message.

The duration parameter is specified in seconds. If it's None, the toast will be displayed indefinitely.

Closes: #6480

modal_control

import gradio as gr
from functools import partial

with gr.Blocks() as demo:
    with gr.Row():
        duration = gr.Number(label="Duration", info="Set to -1 for infinite", value=10, minimum=-1)
    with gr.Row():
        error = gr.Button("Error")
        info = gr.Button("Info")
        warning = gr.Button("Warning")

    def display_message(type, msg, duration):
        duration = None if duration < 0 else duration
        if type == "error":
            raise gr.Error(msg, duration=duration)
        elif type == "info":
            gr.Info(msg, duration=duration)
        elif type == "warning":
            gr.Warning(msg,  duration=duration)

    error.click(partial(display_message, "error", "ERROR 💥"),  [duration])
    info.click(partial(display_message, "info", "INFO ℹ️"), [duration])
    warning.click(partial(display_message, "warning", "WARNING ⚠️"), [duration])


demo.launch()

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 7, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/9688f4d10dc997f877a0feaf94fff10d98c6c9bf/gradio-4.36.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@9688f4d10dc997f877a0feaf94fff10d98c6c9bf#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/9688f4d10dc997f877a0feaf94fff10d98c6c9bf/gradio-client-1.1.1.tgz

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 7, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
@gradio/client minor
@gradio/statustracker minor
gradio minor
website minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Control Display of Error, Info, Warning

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

```

<!--- Description -->
### Description
## {@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.

## 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).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aliabd The link here is not being formatted as a link. How can I fix it?

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed a commit that fixed it. also styling clash.

what's happening with these issues is that i had overrided some default styling rules to design stuff (for example the side bar is a ul but we don't want bullet points there) and now we need them for the content.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@freddyaboulton freddyaboulton marked this pull request as ready for review June 7, 2024 18:12
@abidlabs
Copy link
Member

abidlabs commented Jun 7, 2024

Btw I think this sufficiently closes #6480

Provide a time parameter in seconds that would control the amount of time the message is displayed

This is complete

Have a param provides the ability to position the message on the screen. Maybe allow a combination of top, middle, bottom, left, right, center. And/or potentially provide a control name reference, and have the message pop near that control. Would be great for field validation messages.

I don't think we want to do this, especially the second part as we are planning on instead having a dedicated way to do frontend validation

def __init__(
self,
message: str = "Error raised.",
duration: int | None = 10,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but

Suggested change
duration: int | None = 10,
duration: int | float | None = 10,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just float is fine, int is a subset of float

@pngwn
Copy link
Member

pngwn commented Jun 7, 2024

With number values I'm used to 0 meaning unlimited, although in this case 0 meaning hidden does make sense I think.

@aliabid94
Copy link
Collaborator

With number values I'm used to 0 meaning unlimited, although in this case 0 meaning hidden does make sense I think.

I think it's just a bit confusing that 0 and None have such different meanings, when they're both "falsy". Wonder if another visible= kwarg would be more appropriate specifically for display.

@@ -318,6 +318,8 @@ export function handle_message(
status: {
queue,
message: data.output.error as string,
display: data.output.display as boolean,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is display passed from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally had a separate display parameter. Added it back as visible so this makes more sense now.

@@ -376,6 +381,7 @@ def log_message(
event_id: str,
log: str,
level: Literal["info", "warning"],
duration: int | None = 10000,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 10000 by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally had it being set in ms and forgot to change the default. Fixed now.

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few comments, otherwise LGTM!

@freddyaboulton
Copy link
Collaborator Author

Yes that's fair. Originally had a separate display kwarg but then thought that less parameters would be better. Will bring that back and clean up a bit to address the other comments. Thanks for the input all!

@freddyaboulton freddyaboulton force-pushed the control-error-display branch from cf22f48 to b9dd4a0 Compare June 11, 2024 23:10
@freddyaboulton freddyaboulton enabled auto-merge (squash) June 11, 2024 23:16
@freddyaboulton freddyaboulton merged commit c2a0d05 into main Jun 11, 2024
7 of 8 checks passed
@freddyaboulton freddyaboulton deleted the control-error-display branch June 11, 2024 23:28
@pngwn pngwn mentioned this pull request Jun 11, 2024
@pngwn pngwn mentioned this pull request Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add display time and screen location params to gr.info, gr.error, gr.warning
6 participants