Files
stand/stand.py

108 lines
2.1 KiB
Python

import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
import hacks
from restapi import restapi
from utils.st_utils import get_session_id, rerun, hide_UI_elements
from utils.df_utils import to_excel_binary
st.set_page_config(
page_title="stand",
page_icon="icon.png",
layout="wide"
)
hide_UI_elements()
restapi.sid = get_session_id() # rest api needs current session ID to trigger the next rerun
changed = restapi.changed
df = restapi.data
# encode the column names into the key, thus trigger a hard reload (like F5) when the columns change
key = "stand:" + "+".join(str(col) for col in df.columns)
print(">>> start of streamlit run")
showing_downloads = st.session_state.get("showing_downloads", False)
col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
with col1:
if st.button("Downloads") and not showing_downloads:
st.session_state.showing_downloads = True
with col2:
with open("output.h5", "rb") as f:
st.download_button("Download hdf5", f, file_name="output.h5")
with col3:
xlsx = to_excel_binary(df)
st.download_button("Download xlsx", xlsx, file_name="output.xlsx")
with col4:
csv = df.to_csv()
st.download_button("Download csv", csv, file_name="output.csv")
# st.stop()
else:
st.session_state.showing_downloads = False
gob = GridOptionsBuilder.from_dataframe(df)
gob.configure_auto_height(True)
go = gob.build()
#st.write(go)
response = AgGrid(
df[::-1],
go,
filter=True,
editable=True,
sortable=True,
resizable=True,
fit_columns_on_grid_load=True,
reload_data=changed,
key=key
)
new_df = response.get("data")#, df)
new_df = new_df[::-1]
if not new_df.equals(df) and not changed:
restapi.data = new_df
# print("old:")
# print(df)
# print("new:")
# print(new_df)
# print(">>> force rerun")
# rerun()
#st.dataframe(df.astype(str))
#st.dataframe(new_df.astype(str))
if not new_df.empty:
# new_df.to_excel("output.xlsx")
new_df.to_hdf("output.h5", key="stand")
print(">>> end of streamlit run")