NOEOLOAD / app.py
MMOON's picture
Update app.py
99f73ac verified
import streamlit as st
import pandas as pd
import json
import io
import math
# 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 extract values based on CODE_NEO paths
def extract_values(json_data, code_neo_list):
extracted_values = []
for code in code_neo_list:
try:
# Navigate to "data -> modules -> food_8 -> questions"
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
keys = code.replace("data_modules_food_8_questions_", "").split("_")
for key in keys:
if isinstance(value, list): # Handle lists
value = value[0] if len(value) > 0 else None
if isinstance(value, dict):
value = value.get(key, None)
# Extract the 'answer' key if it's a dictionary
if isinstance(value, dict):
value = value.get("answer", None)
extracted_values.append(value)
except (KeyError, TypeError, IndexError):
extracted_values.append(None)
return extracted_values
# Function to safely update JSON with new values while preserving original data
def update_json_safe(json_data, code_neo_list, new_values):
for code, new_value in zip(code_neo_list, new_values):
try:
# Skip if new_value is NaN or None to preserve original value
if new_value is None or (isinstance(new_value, float) and math.isnan(new_value)):
continue
# Navigate to the "data -> modules -> food_8 -> questions" level
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
keys = code.replace("data_modules_food_8_questions_", "").split("_")
for key in keys[:-1]: # Traverse to the second-to-last key
if isinstance(value, list):
value = value[0] if len(value) > 0 else None
if isinstance(value, dict):
value = value.get(key, None)
# Update the 'answer' key if it exists
if isinstance(value, dict):
value["answer"] = new_value
except (KeyError, TypeError, IndexError):
pass
return json_data
# Streamlit App
st.title("JSON to Excel Mapping and Update")
# Mode Selection
mode = st.sidebar.radio("Select Mode", ["Extraction Mode", "Update Mode"])
if mode == "Extraction Mode":
st.header("Extraction Mode")
# Upload .ifs file
uploaded_json_file = st.file_uploader("Upload .ifs File", type=["ifs"])
# Upload Excel template
uploaded_excel_template = st.file_uploader("Upload Excel Template", type=["xls", "xlsx"])
if uploaded_json_file and uploaded_excel_template:
# Load JSON file
json_data, json_error = load_ifs_file(uploaded_json_file)
if json_error:
st.error(f"Error loading JSON: {json_error}")
else:
# Load Excel template
template_df = pd.read_excel(uploaded_excel_template, sheet_name="profil")
code_neo_list = template_df["CODE_NEO"].tolist()
# Extract data from JSON
extracted_values = extract_values(json_data, code_neo_list)
template_df["TEXTE_NEO"] = extracted_values
# Create an Excel file for download
output = io.BytesIO()
with pd.ExcelWriter(output, engine="openpyxl") as writer:
template_df.to_excel(writer, index=False, sheet_name="profil")
output.seek(0)
# Download button
st.success("Data extracted successfully!")
st.download_button(
label="Download Extracted Excel",
data=output,
file_name="extracted_data.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
elif mode == "Update Mode":
st.header("Update Mode")
# Upload original .ifs file
uploaded_json_file = st.file_uploader("Upload Original .ifs File", type=["ifs"])
# Upload updated Excel file
uploaded_updated_excel = st.file_uploader("Upload Updated Excel File", type=["xls", "xlsx"])
if uploaded_json_file and uploaded_updated_excel:
# Load JSON file
json_data, json_error = load_ifs_file(uploaded_json_file)
if json_error:
st.error(f"Error loading JSON: {json_error}")
else:
# Load updated Excel file
updated_df = pd.read_excel(uploaded_updated_excel, sheet_name="profil")
code_neo_list = updated_df["CODE_NEO"].tolist()
updated_values = updated_df["TEXTE_NEO"].tolist()
# Update JSON
updated_json_data = update_json_safe(json_data, code_neo_list, updated_values)
# Provide updated JSON for download
updated_json_str = json.dumps(updated_json_data, indent=4)
st.success("JSON updated successfully!")
st.download_button(
label="Download Updated .ifs File",
data=updated_json_str,
file_name="updated_report.ifs",
mime="application/json"
)