get_data_survey / src /streamlit_app.py
zeinhasan's picture
Update src/streamlit_app.py
1874f00 verified
import os
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit" # supaya tidak error permission
import streamlit as st
import mysql.connector
import pandas as pd
import io
DB_CONFIG = {
'host': os.environ.get('DB_HOST'),
'user': os.environ.get('DB_USER'),
'password': os.environ.get('DB_PASS'),
'database': os.environ.get('DB_NAME')
}
st.title("Download Data Survey Face Wash")
try:
conn = mysql.connector.connect(**DB_CONFIG)
df = pd.read_sql("SELECT * FROM Kansei", conn)
conn.close()
except Exception as e:
st.error(f"Gagal mengambil data dari database: {e}")
st.stop()
if df.empty:
st.write("Data survey belum tersedia.")
else:
st.write(f"Jumlah data: {len(df)}")
st.dataframe(df)
output = io.BytesIO()
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, index=False, sheet_name='Survey Data')
data_excel = output.getvalue()
st.download_button(
label="Download Data Survey (Excel)",
data=data_excel,
file_name='survey_data.xlsx',
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)