Skip to content

Commit

Permalink
Merge pull request #197 from agoenergy/167_download_button
Browse files Browse the repository at this point in the history
Add excel download button
  • Loading branch information
joAschauer authored Dec 14, 2023
2 parents 54a75dd + 67e99b5 commit 06596f8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
44 changes: 44 additions & 0 deletions app/excel_download.py
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
6 changes: 6 additions & 0 deletions app/layout_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas as pd
import streamlit as st

from app.excel_download import prepare_and_download_df_as_excel
from app.plot_functions import create_bar_chart_costs
from app.ptxboa_functions import change_index_names, config_number_columns

Expand Down Expand Up @@ -85,6 +86,11 @@ def display_costs(
with st.expander("**Data**"):
column_config = config_number_columns(df_res, format=f"%.1f {output_unit}")
st.dataframe(df_res, use_container_width=True, column_config=column_config)
fn = f"costs_per_{key}_{key_suffix}".strip("_")
if st.session_state["user_changes_df"] is not None:
fn = f"{fn}_{select_data}".lower().replace(" ", "_")
prepare_and_download_df_as_excel(df_res, filename=fn)

return None


Expand Down
27 changes: 18 additions & 9 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
import streamlit as st

from app.excel_download import prepare_and_download_df_as_excel
from ptxboa.api import PtxboaAPI


Expand Down Expand Up @@ -649,22 +650,14 @@ def display_and_edit_input_data(
st.session_state[f"{key}_number"] = 0
editor_key = f"{key}_{st.session_state[f'{key}_number']}"

with st.form(key=f"{key}_form"):
with st.form(key=f"{key}_form", border=False):
st.info(
(
"You can edit data directly in the table. When done, click the "
"**Apply Changes** button below to rerun calculations."
)
)

df = st.data_editor(
df,
use_container_width=True,
key=editor_key,
num_rows="fixed",
disabled=[index],
column_config=column_config,
)
st.form_submit_button(
"Apply Changes",
type="primary",
Expand All @@ -681,12 +674,28 @@ def display_and_edit_input_data(
"editor_key": editor_key,
},
)

df = st.data_editor(
df,
use_container_width=True,
key=editor_key,
num_rows="fixed",
disabled=[index],
column_config=column_config,
)

else:
st.dataframe(
df,
use_container_width=True,
column_config=column_config,
)

scenario = st.session_state["scenario"].lower()
scenario = scenario.replace(")", "").replace("(", "")
fn = f"{key}_{scenario}".replace(" ", "_")
prepare_and_download_df_as_excel(df, filename=fn)

return df


Expand Down
2 changes: 1 addition & 1 deletion app/tab_deep_dive_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def content_deep_dive_countries(
api,
data_type="full load hours",
scope=ddc,
key="input_data_editor_full_load_hours_ddc",
key=f"input_data_full_load_hours_{ddc.replace(' ', '_').lower()}",
)

with c_0_1:
Expand Down
12 changes: 6 additions & 6 deletions app/tab_input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def content_input_data(api: PtxboaAPI) -> None:
api,
data_type=data_selection,
scope="world",
key=f"input_data_editor_{data_selection}",
key=f"input_data_{data_selection}",
)
with c_0_1:
st.markdown("**Regional Distribution**")
Expand All @@ -82,21 +82,21 @@ def content_input_data(api: PtxboaAPI) -> None:
api,
data_type="electricity_generation",
scope=None,
key="input_data_editor_electricity_generation",
key="input_data_electricity_generation",
)
with st.expander("**Electrolysis and derivate production**"):
display_and_edit_input_data(
api,
data_type="conversion_processes",
scope=None,
key="input_data_editor_conversion_processes",
key="input_data_conversion_processes",
)
with st.expander("**Transportation (ships and pipelines)**"):
display_and_edit_input_data(
api,
data_type="transportation_processes",
scope=None,
key="input_data_editor_transportation_processes",
key="input_data_transportation_processes",
)
with st.expander(
"**Transportation (compression, liquefication and reconversion)**"
Expand All @@ -105,12 +105,12 @@ def content_input_data(api: PtxboaAPI) -> None:
api,
data_type="reconversion_processes",
scope=None,
key="input_data_editor_reconversion_processes",
key="input_data_reconversion_processes",
)
with st.expander("**Specific costs for materials and energy carriers**"):
display_and_edit_input_data(
api,
data_type="specific_costs",
scope=None,
key="input_data_editor_specific_costs",
key="input_data_specific_costs",
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ streamlit>=1.29
openpyxl~=3.1
plotly~=5.16
streamlit-antd-components
XlsxWriter

0 comments on commit 06596f8

Please sign in to comment.