Spaces:
No application file
No application file
import streamlit as st | |
import pandas as pd | |
# Function to export data based on format choice | |
def export_data(data, format_choice): | |
if format_choice == 'CSV': | |
data.to_csv('exported_data.csv', index=False) | |
elif format_choice == 'JSON': | |
data.to_json('exported_data.json', orient='records') | |
elif format_choice == 'Excel': | |
data.to_excel('exported_data.xlsx', index=False) | |
else: | |
st.error("Unsupported format choice!") | |
# Streamlit app | |
def main(): | |
st.title("Research Data Export") | |
# File upload section | |
st.header("Upload your data") | |
uploaded_file = st.file_uploader("Choose a file", type=["csv", "json", "xlsx"]) | |
if uploaded_file is not None: | |
file_extension = uploaded_file.name.split(".")[-1] | |
try: | |
if file_extension == 'csv': | |
data = pd.read_csv(uploaded_file) | |
elif file_extension == 'json': | |
data = pd.read_json(uploaded_file) | |
elif file_extension in ['xlsx', 'xls']: | |
data = pd.read_excel(uploaded_file) | |
else: | |
st.error("Unsupported file format!") | |
return | |
st.success("Data loaded successfully!") | |
# Display loaded data | |
st.subheader("Loaded Data") | |
st.dataframe(data) | |
# Format export section | |
st.header("Export your data") | |
export_format = st.selectbox("Select export format", ["CSV", "JSON", "Excel"]) | |
if st.button("Export"): | |
export_data(data, export_format) | |
st.success(f"Data exported successfully as {export_format}") | |
except Exception as e: | |
st.error(f"Error: {e}") | |
if __name__ == "__main__": | |
main() | |