NOEOLOAD / appv1.py
MMOON's picture
Rename app.py to appv1.py
949a53c verified
import streamlit as st
import json
import io
# Function to load the .ifs file
def load_ifs_file(file):
try:
return json.load(file), None
except Exception as e:
return None, str(e)
# Function to save the updated content back to a file
def get_file_as_bytes(content):
return io.BytesIO(json.dumps(content, indent=4).encode("utf-8"))
# Streamlit App
st.title("IFS JSON Editor with Download Option")
# Upload file widget
uploaded_file = st.file_uploader("Upload a .ifs file", type=["ifs"])
if uploaded_file is not None:
# Load the uploaded file
ifs_data, error = load_ifs_file(uploaded_file)
if error:
st.error(f"Error loading file: {error}")
else:
# Editable fields with collapsible sections
st.subheader("Edit Specific Fields")
# Accessing specific paths in the JSON for editing
try:
# Extract paths for collapsible sections
employees_desc_en = ifs_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {}).get("numberOfEmployeesDescription_en", {}).get("answer", "")
employees_desc = ifs_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {}).get("numberOfEmployeesDescription", {}).get("answer", "")
# Editable fields inside collapsible sections
with st.expander("Edit 'numberOfEmployeesDescription_en_answer'"):
new_employees_desc_en = st.text_area("Number of Employees Description (EN)", employees_desc_en, height=150)
with st.expander("Edit 'numberOfEmployeesDescription_answer'"):
new_employees_desc = st.text_area("Number of Employees Description", employees_desc, height=150)
# Save changes to JSON
if st.button("Save Changes"):
# Update the JSON with new values
ifs_data["data"]["modules"]["food_8"]["questions"]["numberOfEmployeesDescription_en"]["answer"] = new_employees_desc_en
ifs_data["data"]["modules"]["food_8"]["questions"]["numberOfEmployeesDescription"]["answer"] = new_employees_desc
# Create a downloadable file
file_bytes = get_file_as_bytes(ifs_data)
st.success("Changes saved successfully!")
# Display the download button
st.download_button(
label="Download Edited File",
data=file_bytes,
file_name="edited_output.ifs",
mime="application/json",
)
except Exception as e:
st.error(f"Error accessing JSON fields: {e}")
)