Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,69 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def read_google_sheet(csv_url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
if not csv_url:
|
| 8 |
return "⚠️ Silakan masukkan URL CSV Google Sheet"
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
| 4 |
|
| 5 |
+
def import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
def read_google_sheet(csv_url):
|
| 9 |
+
try:
|
| 10 |
+
if not csv_url:
|
| 11 |
+
return "⚠️ Silakan masukkan URL CSV Google Sheet"
|
| 12 |
+
|
| 13 |
+
# Baca CSV dari URL
|
| 14 |
+
df = pd.read_csv(csv_url)
|
| 15 |
+
|
| 16 |
+
# Bersihkan DataFrame:
|
| 17 |
+
# Hapus baris dan kolom yang seluruhnya kosong
|
| 18 |
+
df = df.dropna(how="all")
|
| 19 |
+
df = df.dropna(axis=1, how="all")
|
| 20 |
+
|
| 21 |
+
# Ubah nama kolom yang tidak informatif (misalnya 'Unnamed: ...') menjadi nama generik
|
| 22 |
+
df.columns = [f"Column_{i}" if "Unnamed" in col else col for i, col in enumerate(df.columns)]
|
| 23 |
+
|
| 24 |
+
# Konversi DataFrame ke JSON agar lebih mudah diproses oleh AI
|
| 25 |
+
json_output = df.to_json(orient="records", force_ascii=False, indent=2)
|
| 26 |
+
return json_output
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"❌ Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Google Sheet Viewer") as demo:
|
| 32 |
+
with gr.Column():
|
| 33 |
+
gr.Markdown("""
|
| 34 |
+
# 📊 Google Sheet Viewer
|
| 35 |
+
**Cara Penggunaan:**
|
| 36 |
+
1. Buka Google Sheet anda
|
| 37 |
+
2. Pilih *File > Share > Publish to web*
|
| 38 |
+
3. Pilih format **CSV** dan publish
|
| 39 |
+
4. Salin URL CSV yang dihasilkan
|
| 40 |
+
""")
|
| 41 |
+
|
| 42 |
+
# Input URL menggunakan Textbox
|
| 43 |
+
url_input = gr.Textbox(
|
| 44 |
+
label="URL CSV Google Sheet",
|
| 45 |
+
placeholder="Contoh: https://docs.google.com/spreadsheets/d/.../export?format=csv&gid=0",
|
| 46 |
+
interactive=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Tombol submit
|
| 50 |
+
submit_button = gr.Button("Tampilkan Data", variant="primary", size="lg")
|
| 51 |
+
|
| 52 |
+
# Output JSON sebagai teks
|
| 53 |
+
output = gr.Textbox(
|
| 54 |
+
label="JSON Output (AI Friendly)",
|
| 55 |
+
lines=10,
|
| 56 |
+
interactive=False
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Handler untuk tombol submit
|
| 60 |
+
submit_button.click(
|
| 61 |
+
fn=read_google_sheet,
|
| 62 |
+
inputs=url_input,
|
| 63 |
+
outputs=output
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()(csv_url):
|
| 68 |
try:
|
| 69 |
if not csv_url:
|
| 70 |
return "⚠️ Silakan masukkan URL CSV Google Sheet"
|