PablocFonseca / streamlit-aggrid

Implementation of Ag-Grid component for Streamlit
https://pypi.org/project/streamlit-aggrid/
MIT License
1.06k stars 202 forks source link

Periods in columns names make all entries in that column blank #31

Closed bwest2397 closed 2 years ago

bwest2397 commented 3 years ago

There's a bug that occurs when a period is present in the name of a column entered into AgGrid, all values in that column are displayed as empty. It can be visually worked around by replacing any periods in column names with the "one dot leader" character ("․", U+2024, or "\u2024" in Python), but that's an imperfect solution as the column names need to be restored in the returned dataframe.

Demo code snippet:

import streamlit as st
import pandas as pd
from st_aggrid import AgGrid

st.write('### No periods in column names')
df_no_dots = pd.DataFrame({'field_1':['a','b','c','d'], 'field_2':[1,2,3,4]})
AgGrid(df_no_dots)

st.write('---\n### One columns name with period, one without')
df_half_dots = pd.DataFrame({'field.1':['a','b','c','d'], 'field_2':[1,2,3,4]})
AgGrid(df_half_dots)

st.write('---\n### Both columns names with periods')
df_both_dots = pd.DataFrame({'field.1':['a','b','c','d'], 'field.2':[1,2,3,4]})
AgGrid(df_both_dots)

st.write("The same data, but with the table set as editable")
return_df = AgGrid(df_both_dots, editable=True)

st.write("The output of this displayed using st.dataframe demonstrates that the data is not being deleted, it just isn't being displayed")
st.dataframe(return_df['data'])

st.write('---\n### Workaround by replacing periods in column names with similar "one dot leader" character ("\u2024", U+2024)')
df_dots_workaround = df_both_dots.rename(columns=lambda col : col.replace('.','\u2024'))
AgGrid(df_dots_workaround)

Screenshots of the examples in the code snippet:

image

image

image

image

torlenor commented 2 years ago

I just wanted to post that we observed the same bug. As soon as there is a "." in the column name of a Pandas dataframe, the contents of that column are not shown in the resulting table.

Tested with Streamlit version: 1.2, 1.4 streamlit-aggrid version: 0.2.2.post4

P.S.: And thanks for that package, I love it to have AgGrid in Streamlit.

PablocFonseca commented 2 years ago

AgGrid documentation: https://www.ag-grid.com/javascript-data-grid/grid-properties/#reference-columns-suppressFieldDotNotation

suppressFieldDotNotation: If true, then dots in field names (e.g. address.firstline) are not treated as deep references. Allows you to use dots in your field name if you prefer.Default: false

So, you need to set this when your columns have dot's in their names. For convenience, I'm adding the following code to GridOptionsBuilder.from_dataframe() in V 0.2.3

if any('.' in col for col in dataframe.columns):
    gb.configure_grid_options(suppressFieldDotNotation = True)