簡體   English   中英

如何為 streamlit-aggrid 設置最大列長度

[英]How to set a max column length for streamlit-aggrid

如果我有一個非常長的列條目占用了大部分表格(如下所示),我該如何設置表格選項以使其被截斷?


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

df = pd.DataFrame({'some short column': ['a', 'b', 'c'],
                   'some long column': ['all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy '
                                        'all column and no spacing makes coder a dull boy ', 'two', 'three'],
                   'some other column': ['one', 'two', 'three'],
                   'some other column 2': ['one', 'two', 'three']})

AgGrid(df)

在此處輸入圖像描述

編輯:

AgGrid(df, fit_columns_on_grid_load=True) 

適用於此示例,但我的實際 df 有 > 20 列,這導致列太擠在一起無法閱讀。 在此處輸入圖像描述

使用 fit_columns_on_grid_load 參數,默認為 False。

AgGrid(df, fit_columns_on_grid_load=True)

感謝這個組合! 不過,我不得不在單獨的配置行中添加fit_columns_on_grid ,因此:

gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(min_column_width=150)
interactive_table = AgGrid(df, gridOptions=gb.build(), fit_columns_on_grid_load=True)

結合@ferdy的回答。 如果我使用:

gb = GridOptionsBuilder.from_dataframe(df, min_column_width=30)
AgGrid(df, gridOptions=gb.build(), fit_columns_on_grid_load=True)

然后它可以安全地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM