JaganathC commited on
Commit
2fddd9e
·
verified ·
1 Parent(s): 84a2eaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -30
app.py CHANGED
@@ -3,46 +3,81 @@ import json
3
  import pandas as pd
4
  from io import BytesIO
5
 
6
- st.set_page_config(layout="wide")
 
 
 
 
 
 
7
 
8
- st.title("JSON to Excel Converter")
9
 
10
- json_text = st.text_area("JSON Data", height=300)
 
 
 
 
 
 
 
11
 
12
- def process_json(data):
 
 
 
13
  rows = []
14
 
15
- def traverse(obj):
16
- for k, v in obj.items():
17
- if isinstance(v, dict):
18
- rows.append([k, None])
19
- traverse(v)
20
  else:
21
- rows.append([k, v])
22
 
23
- traverse(data)
24
- return pd.DataFrame(rows, columns=["Object Name/Key Name", "English_Values"])
 
 
 
25
 
26
- if st.button("Generate Excel Data"):
27
- try:
28
- cleaned = json_text.replace(",}", "}").replace(",]", "]")
29
- parsed = json.loads(cleaned)
 
 
 
 
 
 
 
 
 
 
30
 
31
- df = process_json(parsed)
 
32
 
33
- st.subheader("Excel Preview")
34
- st.dataframe(df, use_container_width=True)
35
 
36
- output = BytesIO()
37
- with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
38
- df.to_excel(writer, index=False, sheet_name="Labels")
 
 
 
 
 
39
 
40
- st.download_button(
41
- "Download Excel File",
42
- data=output.getvalue(),
43
- file_name="JSON_To_Excel.xlsx",
44
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
45
- )
46
 
47
- except Exception as e:
48
- st.error(f"Invalid JSON: {e}")
 
3
  import pandas as pd
4
  from io import BytesIO
5
 
6
+ # -------------------------------------------------
7
+ # Page Config
8
+ # -------------------------------------------------
9
+ st.set_page_config(
10
+ page_title="JSON to Excel Converter",
11
+ layout="wide"
12
+ )
13
 
14
+ st.title("📘 JSON to Excel Converter")
15
 
16
+ # -------------------------------------------------
17
+ # Input Area
18
+ # -------------------------------------------------
19
+ json_input = st.text_area(
20
+ "JSON Data",
21
+ height=350,
22
+ placeholder="Paste your JSON data here..."
23
+ )
24
 
25
+ # -------------------------------------------------
26
+ # JSON Processing Logic
27
+ # -------------------------------------------------
28
+ def flatten_json(data):
29
  rows = []
30
 
31
+ def walk(obj):
32
+ for key, value in obj.items():
33
+ if isinstance(value, dict):
34
+ rows.append([key, None])
35
+ walk(value)
36
  else:
37
+ rows.append([key, value])
38
 
39
+ walk(data)
40
+ return pd.DataFrame(
41
+ rows,
42
+ columns=["Object Name/Key Name", "English_Values"]
43
+ )
44
 
45
+ # -------------------------------------------------
46
+ # Button Action
47
+ # -------------------------------------------------
48
+ if st.button("Generate Excel Data", type="primary"):
49
+ if not json_input.strip():
50
+ st.warning("Please paste JSON data.")
51
+ else:
52
+ try:
53
+ # Fix trailing commas
54
+ cleaned_json = (
55
+ json_input
56
+ .replace(",}", "}")
57
+ .replace(",]", "]")
58
+ )
59
 
60
+ parsed_json = json.loads(cleaned_json)
61
+ df = flatten_json(parsed_json)
62
 
63
+ st.subheader("📊 Excel Preview")
64
+ st.dataframe(df, use_container_width=True)
65
 
66
+ # Create Excel file
67
+ excel_buffer = BytesIO()
68
+ with pd.ExcelWriter(excel_buffer, engine="xlsxwriter") as writer:
69
+ df.to_excel(
70
+ writer,
71
+ index=False,
72
+ sheet_name="Labels"
73
+ )
74
 
75
+ st.download_button(
76
+ label="📥 Download Excel File",
77
+ data=excel_buffer.getvalue(),
78
+ file_name="JSON_To_Excel.xlsx",
79
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
80
+ )
81
 
82
+ except Exception as e:
83
+ st.error(f"Invalid JSON format: {e}")