How can I change the precision of the inputs in Streamlit’s DataEditor to 2 decimal places?
I’ve been experimenting with the DataEditor in Streamlit and noticed that it allows me to input numbers like 5,584.4949. However, since my data involves money, I want to enforce a specific format so that numbers display as 5,584.49 within the DataEditor.
I’ve tried several methods to enforce this precision, including using df.style.format
, df.round()
, and st.column_config.NumberColumn(format="%,.2f")
, but none of these approaches seem to work reliably.
Here’s a simplified version of my code:
import streamlit as st
import pandas as pd
from babel.numbers import format_number
def app():
st.title("Konten")
# Initial DataFrame
df = pd.DataFrame(
[
{"Konto": "Konto 1", "Kontostand": 0.0, "Kontokorrent": 0.0},
{"Konto": "Konto 2", "Kontostand": 0.0, "Kontokorrent": 0.0}
]
)
df = df.style.format(
{
"Kontostand": lambda x : format_number(x, locale='de_DE'),
"Kontokorrent": lambda x : format_number(x, locale='de_DE'),
},
thousands=' ',
decimal=',',
)
# Display the DataFrame in a Streamlit data editor
edited_df = st.data_editor(df, num_rows="dynamic")
# Round the numbers in the edited DataFrame to 2 decimal places
edited_df = edited_df.round({k: 2 for k in edited_df})
# Calculate the sum of all values in the columns "Kontostand" and "Kontokorrent"
available_money = edited_df["Kontostand"].sum() + edited_df["Kontokorrent"].sum()
# Format available_money in German number representation
formatted_money = format_number(available_money, locale='de_DE')
# Display the total available money
st.markdown(f"Zur Verfügung: **{formatted_money} €**")
I’m using Streamlit version 1.36.0 and Python 3.10.12. Any suggestions on how to achieve the desired precision would be greatly appreciated!
Demo:
Here I want a uniform formatting across all cells, that has the same precision of 2.
Debug info
Streamlit version: 1.36.0
Python version: 3.10.12