Issue
I'm new to Streamlit. I'm looking to format the numerical contents of st.datatable() to both currency and to 2dp.
I can achieve this using st.table() and formatting the data frame in pandas by
df_final['Total Quote Value'] = df_final['Total Quote Value'].map("£{:,.2f}".format)
However this approach generates a static non interactive table. If I take the above approach and apply to the st.dataframe() method, the sorting part, of the data table sorts on the lexicon and not the numerical as the data has been mapped to a string format. Any help is greatly appreciated. Thanks in advance.
Solution
You can use streamlit-aggrid in this situation.
Code
"""
Requirements:
pip install streamlit
pip install streamlit-aggrid
"""
import streamlit as st
import pandas as pd
from st_aggrid import GridOptionsBuilder, AgGrid
data = {
'price': [6.204, 6.255, 7.558, 1200.755]
}
df = pd.DataFrame(data)
st.write('### Streamlit dataframe')
st.dataframe(df)
# AgGrid
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_column("price", type=["numericColumn", "numberColumnFilter", "customCurrencyFormat"], custom_currency_symbol="£")
gridOptions = gb.build()
st.write('### Streamlit AgGrid')
AgGrid(
df,
gridOptions=gridOptions,
height=200
)
Output
Press to sort
Access menu for filtering
Answered By - ferdy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.