Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,20 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
from io import BytesIO
|
| 5 |
-
import streamlit.components.v1 as components
|
| 6 |
|
| 7 |
# -------------------------------------------------
|
| 8 |
# Page Setup
|
| 9 |
# -------------------------------------------------
|
| 10 |
-
st.set_page_config(page_title="JSON Text
|
| 11 |
-
st.title("π JSON Text β Excel Converter
|
| 12 |
|
| 13 |
st.markdown(
|
| 14 |
-
"Paste JSON-like **text data**
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
# -------------------------------------------------
|
| 18 |
-
# Text Input
|
| 19 |
# -------------------------------------------------
|
| 20 |
if "json_text" not in st.session_state:
|
| 21 |
st.session_state.json_text = ""
|
|
@@ -32,21 +32,24 @@ text_input = st.text_area(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# -------------------------------------------------
|
| 35 |
-
# Line-by-line Parser
|
| 36 |
# -------------------------------------------------
|
| 37 |
def parse_text_to_rows(text):
|
| 38 |
rows = []
|
| 39 |
|
| 40 |
for line in text.splitlines():
|
| 41 |
line = line.strip()
|
|
|
|
| 42 |
if not line:
|
| 43 |
continue
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Match "KEY": {
|
| 47 |
obj_match = re.match(r'"([^"]+)"\s*:\s*\{', line)
|
| 48 |
if obj_match:
|
| 49 |
-
rows.append([obj_match.group(1),
|
| 50 |
continue
|
| 51 |
|
| 52 |
# Match "KEY": "VALUE"
|
|
@@ -55,7 +58,10 @@ def parse_text_to_rows(text):
|
|
| 55 |
rows.append([kv_match.group(1), kv_match.group(2)])
|
| 56 |
continue
|
| 57 |
|
| 58 |
-
return pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# -------------------------------------------------
|
| 61 |
# Button Action
|
|
@@ -74,13 +80,6 @@ if st.button("Generate Excel Data", type="primary"):
|
|
| 74 |
st.subheader("π Excel Preview")
|
| 75 |
st.dataframe(df, use_container_width=True)
|
| 76 |
|
| 77 |
-
# Copy to Clipboard (only two columns)
|
| 78 |
-
csv_text = df.to_csv(index=False, sep="\t") # tab-separated for Excel
|
| 79 |
-
copy_button_html = f"""
|
| 80 |
-
<button onclick="navigator.clipboard.writeText(`{csv_text}`)">π Copy Content</button>
|
| 81 |
-
"""
|
| 82 |
-
components.html(copy_button_html, height=50)
|
| 83 |
-
|
| 84 |
# Excel Export
|
| 85 |
buffer = BytesIO()
|
| 86 |
with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
from io import BytesIO
|
|
|
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
# Page Setup
|
| 8 |
# -------------------------------------------------
|
| 9 |
+
st.set_page_config(page_title="JSON Text to Excel", layout="wide")
|
| 10 |
+
st.title("π JSON Text β Excel Converter")
|
| 11 |
|
| 12 |
st.markdown(
|
| 13 |
+
"Paste JSON-like **text data** (not strict JSON). "
|
| 14 |
+
"The app parses it line-by-line and generates Excel output."
|
| 15 |
)
|
| 16 |
|
| 17 |
# -------------------------------------------------
|
| 18 |
+
# Text Input (HF Safe)
|
| 19 |
# -------------------------------------------------
|
| 20 |
if "json_text" not in st.session_state:
|
| 21 |
st.session_state.json_text = ""
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# -------------------------------------------------
|
| 35 |
+
# Line-by-line Parser (ROBUST)
|
| 36 |
# -------------------------------------------------
|
| 37 |
def parse_text_to_rows(text):
|
| 38 |
rows = []
|
| 39 |
|
| 40 |
for line in text.splitlines():
|
| 41 |
line = line.strip()
|
| 42 |
+
|
| 43 |
if not line:
|
| 44 |
continue
|
| 45 |
+
|
| 46 |
+
# Remove trailing commas
|
| 47 |
+
line = line.rstrip(",")
|
| 48 |
|
| 49 |
# Match "KEY": {
|
| 50 |
obj_match = re.match(r'"([^"]+)"\s*:\s*\{', line)
|
| 51 |
if obj_match:
|
| 52 |
+
rows.append([obj_match.group(1), None])
|
| 53 |
continue
|
| 54 |
|
| 55 |
# Match "KEY": "VALUE"
|
|
|
|
| 58 |
rows.append([kv_match.group(1), kv_match.group(2)])
|
| 59 |
continue
|
| 60 |
|
| 61 |
+
return pd.DataFrame(
|
| 62 |
+
rows,
|
| 63 |
+
columns=["Object Name/Key Name", "English_Values"]
|
| 64 |
+
)
|
| 65 |
|
| 66 |
# -------------------------------------------------
|
| 67 |
# Button Action
|
|
|
|
| 80 |
st.subheader("π Excel Preview")
|
| 81 |
st.dataframe(df, use_container_width=True)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Excel Export
|
| 84 |
buffer = BytesIO()
|
| 85 |
with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
|