Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
# Function to load the .ifs file
|
| 7 |
def load_ifs_file(file):
|
|
@@ -17,7 +18,6 @@ def extract_values(json_data, code_neo_list):
|
|
| 17 |
try:
|
| 18 |
# Navigate to "data -> modules -> food_8 -> questions"
|
| 19 |
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
|
| 20 |
-
# Extract keys after the fixed prefix
|
| 21 |
keys = code.replace("data_modules_food_8_questions_", "").split("_")
|
| 22 |
for key in keys:
|
| 23 |
if isinstance(value, list): # Handle lists
|
|
@@ -32,10 +32,14 @@ def extract_values(json_data, code_neo_list):
|
|
| 32 |
extracted_values.append(None)
|
| 33 |
return extracted_values
|
| 34 |
|
| 35 |
-
# Function to update JSON with new values
|
| 36 |
-
def
|
| 37 |
for code, new_value in zip(code_neo_list, new_values):
|
| 38 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Navigate to the "data -> modules -> food_8 -> questions" level
|
| 40 |
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
|
| 41 |
keys = code.replace("data_modules_food_8_questions_", "").split("_")
|
|
@@ -114,7 +118,7 @@ elif mode == "Update Mode":
|
|
| 114 |
updated_values = updated_df["TEXTE_NEO"].tolist()
|
| 115 |
|
| 116 |
# Update JSON
|
| 117 |
-
updated_json_data =
|
| 118 |
|
| 119 |
# Provide updated JSON for download
|
| 120 |
updated_json_str = json.dumps(updated_json_data, indent=4)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
| 4 |
import io
|
| 5 |
+
import math
|
| 6 |
|
| 7 |
# Function to load the .ifs file
|
| 8 |
def load_ifs_file(file):
|
|
|
|
| 18 |
try:
|
| 19 |
# Navigate to "data -> modules -> food_8 -> questions"
|
| 20 |
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
|
|
|
|
| 21 |
keys = code.replace("data_modules_food_8_questions_", "").split("_")
|
| 22 |
for key in keys:
|
| 23 |
if isinstance(value, list): # Handle lists
|
|
|
|
| 32 |
extracted_values.append(None)
|
| 33 |
return extracted_values
|
| 34 |
|
| 35 |
+
# Function to safely update JSON with new values while preserving original data
|
| 36 |
+
def update_json_safe(json_data, code_neo_list, new_values):
|
| 37 |
for code, new_value in zip(code_neo_list, new_values):
|
| 38 |
try:
|
| 39 |
+
# Skip if new_value is NaN or None to preserve original value
|
| 40 |
+
if new_value is None or (isinstance(new_value, float) and math.isnan(new_value)):
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
# Navigate to the "data -> modules -> food_8 -> questions" level
|
| 44 |
value = json_data.get("data", {}).get("modules", {}).get("food_8", {}).get("questions", {})
|
| 45 |
keys = code.replace("data_modules_food_8_questions_", "").split("_")
|
|
|
|
| 118 |
updated_values = updated_df["TEXTE_NEO"].tolist()
|
| 119 |
|
| 120 |
# Update JSON
|
| 121 |
+
updated_json_data = update_json_safe(json_data, code_neo_list, updated_values)
|
| 122 |
|
| 123 |
# Provide updated JSON for download
|
| 124 |
updated_json_str = json.dumps(updated_json_data, indent=4)
|