Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lukasmasuch
Copy link
Collaborator

@lukasmasuch lukasmasuch commented Jul 5, 2025

Describe your changes

Allow setting the toast duration to short (4s default), long (10s) or infinite (until dismissed) or int (number of seconds)

GitHub Issue Link (if applicable)

Testing Plan

  • Added unit and e2e tests.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Contributor

snyk-io bot commented Jul 5, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@lukasmasuch lukasmasuch changed the title Allow setting duration for st.toast [WIP] Allow setting duration for st.toast Jul 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 5, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-11872/streamlit-1.48.0-py3-none-any.whl
🕹️ Preview app pr-11872.streamlit.app (☁️ Deploy here if not accessible)

@lukasmasuch lukasmasuch changed the title [WIP] Allow setting duration for st.toast Allow setting duration for st.toast Aug 7, 2025
@lukasmasuch lukasmasuch added security-assessment-completed Security assessment has been completed for PR change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Aug 7, 2025
@lukasmasuch lukasmasuch requested a review from Copilot August 7, 2025 23:01
@lukasmasuch lukasmasuch marked this pull request as ready for review August 7, 2025 23:01
@lukasmasuch lukasmasuch requested a review from a team as a code owner August 7, 2025 23:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds the ability to configure toast duration in Streamlit, allowing users to control how long toast notifications remain visible. This addresses a longstanding feature request (#7047) by introducing flexible duration settings.

Key changes:

  • Added duration parameter to st.toast() API with support for "short" (4s), "long" (10s), "infinite", or custom integer values
  • Updated protobuf definition to include optional duration field
  • Modified frontend components to respect custom duration settings

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
proto/streamlit/proto/Toast.proto Added optional duration field to Toast protobuf message
lib/streamlit/elements/toast.py Added duration parameter to toast function with validation logic
lib/tests/streamlit/toast_test.py Added comprehensive unit tests for duration variants
frontend/lib/src/components/elements/Toast/Toast.tsx Updated to use duration from protobuf and apply to autoHideDuration
frontend/lib/src/components/core/Block/ElementNodeRenderer.tsx Simplified props by passing entire element instead of individual fields
frontend/app/src/components/EventContainer/EventContainer.tsx Updated comment to clarify default duration behavior
e2e_playwright/st_toast.py Added test app with duration examples
e2e_playwright/st_toast_test.py Added e2e test for duration functionality

elif duration == "infinite":
toast_proto.duration = 0
else:
toast_proto.duration = duration
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

The code doesn't validate that integer duration values are non-negative. Negative values could cause unexpected behavior in the frontend.

Suggested change
toast_proto.duration = duration
if isinstance(duration, int):
if duration < 0:
raise StreamlitAPIException(
"Toast duration must be a non-negative integer, got %r." % duration
)
toast_proto.duration = duration
else:
raise StreamlitAPIException(
"Invalid value for duration: %r. Must be 'short', 'long', 'infinite', or a non-negative integer."
% duration
)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: Why are we not checking this? It makes sense to me to have something like this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added an exception for wrong duration values 👍 But I think this is one of the cases where it's a bit hard to draw a line since it's quite unlikely that a user/AI would be confused here and put in a negative integer. And we are far from protecting against all invalid values in our API via exceptions. And even if we had that as a goal, it could end up in a large number of extremely specific exceptions. This could be something we would need to pay for if we ever get to the exception handling refactoring or internationalisation. At the moment, I'm treating it as: add an exception whenever a user might be confused by the possible values / usage and be more lax on other parameters.

@lukasmasuch lukasmasuch enabled auto-merge (squash) August 8, 2025 20:16
@lukasmasuch lukasmasuch merged commit f156565 into develop Aug 8, 2025
36 checks passed
@lukasmasuch lukasmasuch deleted the feature/toast-timer branch August 8, 2025 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

st.toast timer

3 participants