-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from agoenergy/167_download_button
Add excel download button
- Loading branch information
Showing
6 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Utility functions to download a pd.DataFrame as Excel file with streamlit.""" | ||
from io import BytesIO | ||
|
||
import pandas as pd | ||
import streamlit as st | ||
|
||
st.cache_data() | ||
|
||
|
||
def prepare_df_as_excel_stream(df: pd.DataFrame) -> bytes: | ||
"""Convert a Dataframe to excel bytes stream.""" | ||
output = BytesIO() | ||
writer = pd.ExcelWriter(output, engine="xlsxwriter") | ||
df.to_excel(writer, index=True, sheet_name="Data") | ||
writer.close() | ||
return output.getvalue() | ||
|
||
|
||
def prepare_and_download_df_as_excel(df: pd.DataFrame, filename: str): | ||
""" | ||
Prepare a bytes stream and add streamlit download button. | ||
https://stackoverflow.com/a/70120061 | ||
Parameters | ||
---------- | ||
df : pd:dataFrame | ||
filename : str | ||
filename will be appended with ".xlsx" | ||
Returns | ||
------- | ||
None | ||
""" | ||
excel_stream = prepare_df_as_excel_stream(df) | ||
|
||
st.download_button( | ||
label="Download Table as Excel", | ||
data=excel_stream, | ||
file_name=f"{filename}.xlsx", | ||
help="Click to download this Table as Excel File.", | ||
) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ streamlit>=1.29 | |
openpyxl~=3.1 | ||
plotly~=5.16 | ||
streamlit-antd-components | ||
XlsxWriter |