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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix progress bar float input bug #7953

Merged
merged 2 commits into from Jan 17, 2024

Conversation

notiona
Copy link
Contributor

@notiona notiona commented Jan 13, 2024

Describe your changes

This PR solves the wrongly raised streamlit.errors.StreamlitAPIException when giving a value slightly greater than 1.0 for st.progress_bar. The issue is stated here.

Even after mitigating the issue directly, the PR also handles cases where given value for st.progress_bar is slightly below 0.0, which should also cause a similar bug.

Instead of simply rounding the given float value, I have decided to use math.isclose() function as it provides both relative tolerance and absolute tolerance capabilities. Also, performance-wise, by checking math.isclose() in the same if condition as the previous condition, we would have no excess computation for normal values due to short circuit evaluation.

Additionally if required, this helper function _check_float_between can be reused in other parts of streamlit where flexible float comparison is necessary. Thus the low and high bounds are configurable as keyword arguments.

GitHub Issue Link (if applicable)

Closes #5517

Testing Plan

  • I have made an additional unit test in lib/tests/streamlit/elements/progress_test.py checking for values that are off the bound of (low <= value <= high), yet still should be considered valid input.
  • I have also tested the case stated in Progress Bar Float Error聽#5517 locally (following the local setup guide in the contribution guides), and it works as intended.

image

Please feel free to give any ideas on improvements or suggestions. 馃槃


Contribution License Agreement

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

Copy link
Collaborator

@LukasMasuch LukasMasuch left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution 馃憤 LGTM

def _check_float_between(value: float, low: float = 0.0, high: float = 1.0) -> bool:
"""
Checks given value is 'between' the bounds of [low, high],
considering close values around bounds are acceptable input
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be great if you extended the comment slightly explaining why we want to consider close values around bounds, e.g. ... acceptable input, for example in order to account for values being slightly off due to floating point operations.. What do you think?

Copy link
Contributor Author

@notiona notiona Jan 17, 2024

Choose a reason for hiding this comment

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

@raethlein I agree. Thank you for your comment. I have extended the docstring in commit 98ff93b4b3af986ef4fde89b8689a22c81dc7a09.

Copy link
Collaborator

@raethlein raethlein left a comment

Choose a reason for hiding this comment

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

Thank you, looks great! I have just left a tiny nit.

Comment on lines 44 to 51
if (
(low <= value <= high)
or math.isclose(value, low, rel_tol=1e-9, abs_tol=1e-9)
or math.isclose(value, high, rel_tol=1e-9, abs_tol=1e-9)
):
return True
else:
return False
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: this could be cleaned up a tiny bit by just returning the conditional in the if rather than returning True or False based on it

    return (
        (low <= value <= high)
        or math.isclose(value, low, rel_tol=1e-9, abs_tol=1e-9)
        or math.isclose(value, high, rel_tol=1e-9, abs_tol=1e-9)
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vdonato I thought at first since the if condition is quite long it would hurt readability to simply return the expression, but now that I see your suggestion it looks cleaner. Thank you for the comment. I have fixed it in commit 98ff93b4b3af986ef4fde89b8689a22c81dc7a09.

Copy link
Collaborator

@vdonato vdonato left a comment

Choose a reason for hiding this comment

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

One tiny nit (and also seconding @raethlein's comment), but otherwise this LGTM. Thanks for the contribution @notiona!

@notiona
Copy link
Contributor Author

notiona commented Jan 17, 2024

Thank you for your valuable comments guys! I will address them and let you guys know when I am done.

@notiona
Copy link
Contributor Author

notiona commented Jan 17, 2024

I have addressed all your comments. Feel free to provide any other feedback. Thank you for the great work! @LukasMasuch @sfc-gh-lmasuch @raethlein @vdonato

@LukasMasuch
Copy link
Collaborator

Thanks for the update :)

@LukasMasuch LukasMasuch merged commit 16ad6de into streamlit:develop Jan 17, 2024
39 checks passed
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
* Fix progress bar float input bug

* add docstring, simplify fix st.progress bar bug fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Progress Bar Float Error
5 participants