Hiding an AgGrid column

I would like to set certain columns of my dataframe to be invisible in the AgGrid. I still need to enbale user to pass the hidden data using select box to st.map - I just don’t want the user to see all the lat and lon coordinates on display. Is there any simple solution to this problem? Thanks for any good ideas!

Hi @PeterPetersen, I think you have a switch (hide =true) when configuring the grid columns.

Cheers

Hi @Shawn_Pereira, thanks, there is a switch in the front end. The thing is, that I want to hide these columns altogether before they become visible. It is just the display of column I am trying to hide, so I cannot just drop the column from my dataframe.
Cheers

Maybe create a df_hidden to be displayed in aggrid? I mean there can be df_full with all columns and df_hidden for display purposes.

Hi @TomJohn, thanks, if I do that, I cannot use the data from lat, lan columns for the callback to st.map

Yup, I see now. Does this work?

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

df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})

grid_options = {
    "columnDefs": [
        {
            "headerName": "col1",
            "field": "col1",
            "editable": True,
        },
        {
            "headerName": "col2",
            "field": "col2",
            "hide": True,
        },
    ],
}

grid_return = AgGrid(df, grid_options)
new_df = grid_return["data"]

st.write(new_df)
1 Like

In principle this looks great - many thanks! Is it possible to pass a list of column header names to be hidden instead of redefinig all headers?

Not sure exactly, but I should definitely link the source of my knowledge :slight_smile: Usage — streamlit-aggrid 0.2.3 documentation It may help us in further investigation.

I can achieve desired effect using GridOptionsBuilder :slight_smile:

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

df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})

# create a grid options builder and set the column definitions
gob = GridOptionsBuilder.from_dataframe(df)
grid_options = gob.build()

column_defs = grid_options["columnDefs"]
columns_to_hide = ["col2"]

# update the column definitions to hide the specified columns
for col in column_defs:
    if col["headerName"] in columns_to_hide:
        col["hide"] = True

grid_return = AgGrid(df, grid_options)
new_df = grid_return["data"]
st.write(new_df)
2 Likes

Hi @PeterPetersen , I was referring to the hide parameter as part of defining the aggrid columns (not the df columns). You may have 10 columns in your df, but might only want to show columns #1,3,5 in your aggrid output. Your can do that when defining them in your aggrid code; and because you defined them as hidden columns, you can still reference their content in the code, if you want.

Cheers

1 Like

This is excellent - many thanks!

1 Like