Aditya DN commited on
Commit
34e75f3
·
verified ·
1 Parent(s): faa2ea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -1,30 +1,40 @@
1
  import gradio as gr
2
  import base64
 
 
 
3
 
4
  def buffer_to_base64(binary_data: bytes):
 
5
  # Encode bytes ke Base64 string
6
- b64_str = base64.b64encode(binary_data).decode("utf-8")
7
- return b64_str
 
 
 
 
 
 
 
 
 
8
 
9
  with gr.Blocks() as demo:
10
- gr.Markdown("# ESP32 Buffer to Base64 API")
11
  with gr.Row():
12
  inp = gr.File(label="Upload Raw Buffer", type="binary")
13
- out = gr.Textbox(label="Base64 Output", lines=6)
 
 
14
  with gr.Row():
15
  btn_convert = gr.Button("Convert")
16
- btn_copy = gr.Button("Copy to Clipboard")
17
 
18
- # Tombol Convert
19
- btn_convert.click(buffer_to_base64, inputs=inp, outputs=out)
20
 
21
- # Tombol Copy pakai JS bawaan browser
22
- btn_copy.click(
23
- None,
24
- [],
25
- [],
26
- js="navigator.clipboard.writeText(document.querySelector('textarea').value)"
27
- )
28
 
29
  if __name__ == "__main__":
30
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  import base64
3
+ import pyperclip
4
+
5
+ b64_state = None # simpan sementara di server
6
 
7
  def buffer_to_base64(binary_data: bytes):
8
+ global b64_state
9
  # Encode bytes ke Base64 string
10
+ b64_state = base64.b64encode(binary_data).decode("utf-8")
11
+ return b64_state, f"[Buka hasil di tab baru](data:text/plain;base64,{b64_state})"
12
+
13
+ def copy_to_server():
14
+ if b64_state:
15
+ try:
16
+ pyperclip.copy(b64_state)
17
+ return "Base64 berhasil disalin ke clipboard **server**."
18
+ except Exception as e:
19
+ return f"Gagal menyalin ke clipboard server: {e}"
20
+ return "Belum ada data yang dikonversi."
21
 
22
  with gr.Blocks() as demo:
23
+ gr.Markdown("# ESP32 Buffer to Base64 API (No JS)")
24
  with gr.Row():
25
  inp = gr.File(label="Upload Raw Buffer", type="binary")
26
+ with gr.Row():
27
+ out = gr.Textbox(label="Base64 Output", lines=6, interactive=True)
28
+ link_out = gr.Markdown("") # link buka tab baru
29
  with gr.Row():
30
  btn_convert = gr.Button("Convert")
31
+ btn_copy = gr.Button("Copy (Server Clipboard)")
32
 
33
+ # Tombol Convert → tampilkan Base64 & link tab baru
34
+ btn_convert.click(buffer_to_base64, inputs=inp, outputs=[out, link_out])
35
 
36
+ # Tombol Copy (server side)
37
+ btn_copy.click(copy_to_server, outputs=out)
 
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
  demo.launch(server_name="0.0.0.0", server_port=7860)