Skip to content

Commit 847ab6a

Browse files
authored
Display dataframes in streamlit apps with aggrid (#66)
* ✨ Feat(streamlit_reportview.py): Display dataframes in streamlit apps with st-aggrid to add searching and filtering features. Fixes #62 * ⬇️ Fix: downgrade streamlit-aggrid version to pass CI tests * ➕ Fix: do not specify aggrid version to avoid conflicts with other packages * ⬆️ Try 1.0.4 aggrid version * 📌 Fix: Try 1.1.0 aggrid version and altair without a specific version * 📌 Do not specific versions for altair and aggrid * 🐛 Fix(streamlit_reportview.py): add aggird imports and fix issues in the code to loaf the dfs * 🐛 Fix(streamlit_reportview.py): add f string to print df_index and create this variable once * 🐛 Fix(streamlit_reportview.py): correct df_index creation
1 parent bb1220f commit 847ab6a

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ version = "0.1.0"
1313
[tool.poetry.dependencies]
1414
python = ">=3.9,<3.9.7 || >3.9.7,<4.0"
1515
streamlit = "1.39.0"
16+
streamlit-aggrid = "*"
17+
quarto-cli = "*"
1618
plotly = "5.15.0"
1719
pyvis = "^0.3.2"
1820
pandas = {extras = ["parquet"], version = "^2.2.3"}
@@ -21,13 +23,14 @@ xlrd = "^2.0.1"
2123
nbformat = "^5.10.4"
2224
nbclient = "^0.10.0"
2325
matplotlib = "^3.9.2"
24-
altair = "^5.4.1"
26+
altair = "*"
2527
itables = "^2.2.2"
2628
kaleido = "0.2.0"
2729
vl-convert-python = "^1.7.0"
2830
dataframe-image = "^0.2.6"
2931
strenum = { version = "^0.4.15", python = "<3.11" }
3032
pyyaml = "^6.0.2"
33+
3134
# optional doc depencencies, follow approach as described here:
3235
# https://github.com/python-poetry/poetry/issues/2567#issuecomment-646766059
3336
sphinx = {version="*", optional=true}
@@ -36,15 +39,12 @@ myst-nb = {version="*", optional=true}
3639
ipywidgets = {version="*", optional=true}
3740
sphinx-new-tab-link = {version = "!=0.2.2", optional=true}
3841
jupytext = {version="*", optional=true}
39-
# quarto
40-
quarto-cli = "*"
4142

4243
[tool.poetry.group.dev.dependencies]
4344
ipykernel = {version="^6.29.5", optional=true}
4445

4546
[tool.poetry.requires-plugins]
4647
poetry-dynamic-versioning = { version = ">=1.0.0,<2.0.0", extras = ["plugin"] }
47-
4848

4949
[tool.poetry-dynamic-versioning]
5050
enable = true

src/vuegen/streamlit_reportview.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,27 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
466466
read_function = read_function_mapping[file_extension]
467467
dataframe_content.append(f"""df = pd.{read_function.__name__}('{dataframe.file_path}')""")
468468

469-
# Display the dataframe
470-
dataframe_content.append("st.dataframe(df, use_container_width=True)")
471-
469+
# Displays a DataFrame using AgGrid with configurable options.
470+
dataframe_content.append("""
471+
# Displays a DataFrame using AgGrid with configurable options.
472+
grid_builder = GridOptionsBuilder.from_dataframe(df)
473+
grid_builder.configure_default_column(editable=True, groupable=True)
474+
grid_builder.configure_side_bar(filters_panel=True, columns_panel=True)
475+
grid_builder.configure_selection(selection_mode="multiple")
476+
grid_builder.configure_pagination(enabled=True, paginationAutoPageSize=False, paginationPageSize=20)
477+
grid_options = grid_builder.build()
478+
479+
AgGrid(df, gridOptions=grid_options)
480+
481+
# Button to download the df
482+
df_csv = df.to_csv(sep=',', header=True, index=False).encode('utf-8')
483+
st.download_button(
484+
label="Download dataframe as CSV",
485+
data=df_csv,
486+
file_name=f"dataframe_{df_index}.csv",
487+
mime='text/csv',
488+
key=f"download_button_{df_index}")
489+
df_index += 1""")
472490
except Exception as e:
473491
self.report.logger.error(f"Error generating content for DataFrame: {dataframe.title}. Error: {str(e)}")
474492
raise
@@ -707,7 +725,7 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
707725
r.PlotType.PLOTLY: ['import json', 'import requests'],
708726
r.PlotType.INTERACTIVE_NETWORK: ['import requests']
709727
},
710-
'dataframe': ['import pandas as pd'],
728+
'dataframe': ['import pandas as pd', 'from st_aggrid import AgGrid, GridOptionsBuilder'],
711729
'markdown': ['import requests'],
712730
'chatbot': ['import time', 'import json', 'import requests']
713731
}
@@ -720,12 +738,13 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
720738
plot_type = getattr(component, 'plot_type', None)
721739
if plot_type in components_imports['plot']:
722740
component_imports.extend(components_imports['plot'][plot_type])
723-
elif component_type == r.ComponentType.DATAFRAME:
724-
component_imports.extend(components_imports['dataframe'])
725741
elif component_type == r.ComponentType.MARKDOWN:
726742
component_imports.extend(components_imports['markdown'])
727743
elif component_type == r.ComponentType.CHATBOT:
728744
component_imports.extend(components_imports['chatbot'])
745+
elif component_type == r.ComponentType.DATAFRAME:
746+
component_imports.extend(components_imports['dataframe'])
747+
component_imports.append('df_index = 1')
729748

730749
# Return the list of import statements
731-
return component_imports
750+
return component_imports

0 commit comments

Comments
 (0)