Column names with spaces in js part

Hi, I am quite new to streamlit-aggrid. I am trying to reformat the value of a column containing spaces in its name. In my code I am looping over the columns to apply the format (since different columns are formatted in a different way). Here is a reproducible example that shows my problem:

    import pandas as pd
    from st_aggrid import AgGrid, GridOptionsBuilder

    data = {'Share of x': [0.2, 0.1, 0.9, 0.6], 'Share': [0.3, 0.4, 0.1, 0.8], 'Share_in_%': [0.2, 0.6, 0.7, 0.8]}
    df = pd.DataFrame(data)

    gb = GridOptionsBuilder.from_dataframe(df)

    for column in df:
        value_format = f"(data.{column}*100).toFixed(1)+'%'"
        gb.configure_column(column, valueFormatter=value_format)

    grid_options = gb.build()

    AgGrid(df, gridOptions=grid_options)

This works but only for columns not containing spaces (column Share) and special characters (like in column Share_in_%). Any help in how to tweak the code in order to make it work for columns with spaces is highly appreciated.

Could you post a minimal reproducible code so we can explore.

@ferdy Thanks for the comment. I added an example.

There is JsCode and allow_unsafe_jscode=True.

import pandas as pd
from st_aggrid import AgGrid, GridOptionsBuilder, JsCode

data = {'Share of x': [0.2, 0.1, 0.9, 0.6], 'Share': [0.3, 0.4, 0.1, 0.8],
        'Share_in_%': [0.2, 0.6, 0.7, 0.8]}
df = pd.DataFrame(data)

gb = GridOptionsBuilder.from_dataframe(df)

col_formatter = JsCode(
    """
    function(params) {
        return (params.value * 100).toFixed(1) + '%';
    }
    """)

for column in df:
    gb.configure_column(column, valueFormatter=col_formatter)

grid_options = gb.build()

AgGrid(df, gridOptions=grid_options, allow_unsafe_jscode=True)

Output

image

1 Like