Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
from io import BytesIO
|
|
|
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
# Page Setup
|
|
@@ -11,7 +12,8 @@ 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 |
# -------------------------------------------------
|
|
@@ -39,7 +41,6 @@ def parse_text_to_rows(text):
|
|
| 39 |
|
| 40 |
for line in text.splitlines():
|
| 41 |
line = line.strip()
|
| 42 |
-
|
| 43 |
if not line:
|
| 44 |
continue
|
| 45 |
|
|
@@ -60,7 +61,7 @@ def parse_text_to_rows(text):
|
|
| 60 |
|
| 61 |
return pd.DataFrame(
|
| 62 |
rows,
|
| 63 |
-
columns=["Object Name/Key Name", "
|
| 64 |
)
|
| 65 |
|
| 66 |
# -------------------------------------------------
|
|
@@ -80,7 +81,19 @@ if st.button("Generate Excel Data", type="primary"):
|
|
| 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:
|
| 86 |
df.to_excel(writer, index=False, sheet_name="Labels")
|
|
|
|
| 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
|
|
|
|
| 12 |
|
| 13 |
st.markdown(
|
| 14 |
"Paste JSON-like **text data** (not strict JSON). "
|
| 15 |
+
"The app parses it line-by-line and generates Excel output. "
|
| 16 |
+
"You can also copy the two columns to clipboard."
|
| 17 |
)
|
| 18 |
|
| 19 |
# -------------------------------------------------
|
|
|
|
| 41 |
|
| 42 |
for line in text.splitlines():
|
| 43 |
line = line.strip()
|
|
|
|
| 44 |
if not line:
|
| 45 |
continue
|
| 46 |
|
|
|
|
| 61 |
|
| 62 |
return pd.DataFrame(
|
| 63 |
rows,
|
| 64 |
+
columns=["Object Name/Key Name", "Values"]
|
| 65 |
)
|
| 66 |
|
| 67 |
# -------------------------------------------------
|
|
|
|
| 81 |
st.subheader("π Excel Preview")
|
| 82 |
st.dataframe(df, use_container_width=True)
|
| 83 |
|
| 84 |
+
# -------------------------
|
| 85 |
+
# Copy to Clipboard Button
|
| 86 |
+
# -------------------------
|
| 87 |
+
# Convert only the two columns to tab-separated text for Excel
|
| 88 |
+
csv_text = df.to_csv(index=False, sep="\t", header=True)
|
| 89 |
+
copy_button_html = f"""
|
| 90 |
+
<button onclick="navigator.clipboard.writeText(`{csv_text}`)">π Copy Two Columns</button>
|
| 91 |
+
"""
|
| 92 |
+
components.html(copy_button_html, height=50)
|
| 93 |
+
|
| 94 |
+
# -------------------------
|
| 95 |
# Excel Export
|
| 96 |
+
# -------------------------
|
| 97 |
buffer = BytesIO()
|
| 98 |
with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
|
| 99 |
df.to_excel(writer, index=False, sheet_name="Labels")
|