streamlit icon indicating copy to clipboard operation
streamlit copied to clipboard

Improve our format parameter as python format

Open arraydude opened this issue 5 years ago • 7 comments

Problem

As a user: I want to format my number as I do with Python.

For example:

x = 1000000
print(f"{x:,}")
# 1,000,000

Additional context

https://discuss.streamlit.io/t/format-integer-with-comma-using-python-printf/2344


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

Views

arraydude avatar Apr 03 '20 19:04 arraydude

I am using a streamlit form to receive some numbers and other information. One problem I have with the number input widget is the lack of format parameters in order to display values as currencies with 'separators'. At the moment it is very hard to recognize a large input number correctly.

Are you open to solutions from the community? Do I have to make pull requests to sprintf-js that is used by streamlit at the moment, can I introduce another dependency eg. https://www.npmjs.com/package/python-format-js or do you prefer a minimalistic proposal from me?

Every advice would be much appreciated.

Thanks

alpakaxaxa avatar Oct 05 '21 22:10 alpakaxaxa

Even better a lambda function to format it as we like.

krishvk avatar Jun 11 '23 15:06 krishvk

Wanted to leave an update and some clarifying comments on this issue since the description is a bit vague.

There is limited format support provided today using the format arg for st.slider, st.number_input and st.column_config.NumberColumn. This is implemented basically using a subset of the functionality from https://github.com/alexei/sprintf.js

Note that st.number_input has some limitations from the other two - see #4897 - we'd like to fix this!

We took a look at this during the implementation of column_config and decided to stick with sprintf as our number formatting library, we didn't see another JavaScript-based option we were happy with (the one linked above from a couple years ago has very low usage).

The thing we'd most likely want to add here is specifically support for adding a thousands separators, particularly comma but maybe other options too. This seems a lot more feasible than trying to add general python syntax support or something more dynamic, given the limitations in JavaScript.

If anyone has other specific formatting asks or use cases, please let us know!

sfc-gh-jcarroll avatar Oct 02 '23 06:10 sfc-gh-jcarroll

Thank you for the update. My usecase is to add units to the numbers displayed in st.number_input. This already works with st.column_config.NumberColumn but not for st.number_input. Since you said you are using the same underlaying JS library, this might be easy fixable. What dou you think?

LukasK13 avatar Mar 03 '24 11:03 LukasK13

My usecase is to add units to the numbers displayed in st.number_input. This already works with st.column_config.NumberColumn but not for st.number_input. Since you said you are using the same underlaying JS library, this might be easy fixable. What dou you think?

We looked into this a while ago but it's unfortunately not that easy. The number input component we use doesn't easily allow adding non-number digits. So we'd either need to parse things ourself (which is not trivial) or add a new parameter to add a prefix or suffix (which is possible but not as easy as reusing our existing logic).

jrieke avatar Jul 10 '24 11:07 jrieke

+1 for an improved number formatting. I added a comment to the ongoing issue in the Javascript library used by Streamlit: https://github.com/alexei/sprintf.js/issues/124 - if thousand separator is useful to you, please consider adding a vote to the linked issue.

haschdl avatar Feb 27 '25 17:02 haschdl

Are there any other aspects besides the support for configuring the thousands separators that are important to support in our format parameter?

lukasmasuch avatar Dec 05 '25 10:12 lukasmasuch