FajarHidaa commited on
Commit
68499b5
·
verified ·
1 Parent(s): 1851e86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -9,17 +9,14 @@ def read_google_sheet(csv_url):
9
  # Baca CSV dari URL
10
  df = pd.read_csv(csv_url)
11
 
12
- # Bersihkan DataFrame:
13
- # 1. Hapus baris yang seluruhnya kosong
14
  df = df.dropna(how="all")
15
- # 2. Hapus kolom yang seluruhnya kosong
16
  df = df.dropna(axis=1, how="all")
17
- # 3. Hapus kolom yang tidak memiliki nama (misal, nama diawali dengan "Unnamed") dan seluruh nilainya null
18
- df = df[[col for col in df.columns if not (col.startswith("Unnamed") and df[col].isnull().all())]]
19
-
20
- # (Opsional) Ubah nama kolom yang tidak informatif menjadi nama generik
21
- df.columns = [f"Column_{i}" if "Unnamed" in col else col for i, col in enumerate(df.columns)]
22
-
23
  # Konversi DataFrame ke JSON agar lebih mudah diproses oleh AI
24
  json_output = df.to_json(orient="records", force_ascii=False, indent=2)
25
  return json_output
@@ -37,24 +34,24 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Google Sheet Viewer") as demo:
37
  3. Pilih format **CSV** dan publish
38
  4. Salin URL CSV yang dihasilkan
39
  """)
40
-
41
  # Input URL menggunakan Textbox
42
  url_input = gr.Textbox(
43
  label="URL CSV Google Sheet",
44
  placeholder="Contoh: https://docs.google.com/spreadsheets/d/.../export?format=csv&gid=0",
45
  interactive=True
46
  )
47
-
48
  # Tombol submit
49
  submit_button = gr.Button("Tampilkan Data", variant="primary", size="lg")
50
-
51
  # Output JSON sebagai teks
52
  output = gr.Textbox(
53
  label="JSON Output (AI Friendly)",
54
  lines=10,
55
  interactive=False
56
  )
57
-
58
  # Handler untuk tombol submit
59
  submit_button.click(
60
  fn=read_google_sheet,
 
9
  # Baca CSV dari URL
10
  df = pd.read_csv(csv_url)
11
 
12
+ # Hapus baris dan kolom yang seluruhnya kosong
 
13
  df = df.dropna(how="all")
 
14
  df = df.dropna(axis=1, how="all")
15
+
16
+ # Hapus kolom tanpa nama yang memiliki nilai null di semua baris
17
+ df = df.loc[:, ~df.columns.str.contains("Unnamed")] # Hapus kolom "Unnamed: x"
18
+ df = df.dropna(axis=1, how="all") # Hapus kembali jika masih ada yang null
19
+
 
20
  # Konversi DataFrame ke JSON agar lebih mudah diproses oleh AI
21
  json_output = df.to_json(orient="records", force_ascii=False, indent=2)
22
  return json_output
 
34
  3. Pilih format **CSV** dan publish
35
  4. Salin URL CSV yang dihasilkan
36
  """)
37
+
38
  # Input URL menggunakan Textbox
39
  url_input = gr.Textbox(
40
  label="URL CSV Google Sheet",
41
  placeholder="Contoh: https://docs.google.com/spreadsheets/d/.../export?format=csv&gid=0",
42
  interactive=True
43
  )
44
+
45
  # Tombol submit
46
  submit_button = gr.Button("Tampilkan Data", variant="primary", size="lg")
47
+
48
  # Output JSON sebagai teks
49
  output = gr.Textbox(
50
  label="JSON Output (AI Friendly)",
51
  lines=10,
52
  interactive=False
53
  )
54
+
55
  # Handler untuk tombol submit
56
  submit_button.click(
57
  fn=read_google_sheet,