How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23?
I tried to set the number format to “%,.2f” as it is recognized by the defautl pandas DataFrame Styler, but this format results in erro when used in the Data Editor.
Steps to reproduce
Code snippet:
import pandas as pd
import streamlit as st
data = {
"name": ["Alice", "Bob"],
"value": [50000.5, 40000.4]
}
st.title("Example")
#load data into a DataFrame object:
df = pd.DataFrame(data)
st.data_editor(
df,
column_config={
"value": st.column_config.NumberColumn(format="%,.2f")
}
)
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
I expected the Data Editor to be compatible with the same formats as the default DataFrame Styler.
Also it would be great if one could choose which character to use as thousands and decimal separator.
Hi @willhuang, thanks for the quick response, I appreciate it.
Sad to know that implementing this feature is not something simple.
But I still have some doubts about this issue.
I find it curious that when the format parameter is not informed, the number column already shows the values with the thousand separator but without decimal places (try it in my code snippet to check). In this situation, isn’t sprintf.js used?
Wouldn’t it be possible to allow using this standard formatting but with 2 decimal places?
Ah yes you’re right. Huh for some reason, I was trying to check the sprintf format but you can just do something like this:
import pandas as pd
import streamlit as st
data = {
"name": ["Alice", "Bob"],
"value": [50000.5, 40000.4]
}
st.title("Example")
#load data into a DataFrame object:
df = pd.DataFrame(data)
st.data_editor(
df,
column_config={
# NOTICE THE STEP PARAMETER BEING USED HERE
"value": st.column_config.NumberColumn(step=".01")
}
)
Thanks @willhuang, your suggestion achieves the desired behavior and helped me a lot.
I think this solution is sufficient for the moment, but it is not yet ideal, because it implies an effective loss of data accuracy beyond the decimal points defined in the step and not just a visualization with less accuracy.
In my understanding, in an ideal solution the data could have arbitrary amounts of decimal places but only the visualization would show fewer decimal places, so that the data returned would have all decimal places.
Therefore, I think it would be very beneficial to talk to others in the Streamlit team about this issue and perhaps open an Issue to seek a solution to this problem.
Finally, could I use this reply to also ask if there is any way to change the character of the thousand and decimal separator? I’m from Brazil and here we use dot as thousand separator and comma as decimal separator
As far as I know and talking with Lukas Masuch, there is no way to change the character of the thousand and decimal separator as of right now. One solution would be to add a locale and that would be a feature enhancement. I suggest creating a feature enhancement with locale as an option in order to get it prioritized as we do look at github issues and prioritize the ones with high upvotes / interactions.