akhaliq HF Staff commited on
Commit
80f8f81
·
verified ·
1 Parent(s): 9e87b26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  import time
 
 
3
 
4
  # Simulated file storage (in-memory dictionary)
5
  files = {"untitled.py": ""} # file_name: content
@@ -15,8 +17,22 @@ def load_file(file, current_file, open_files):
15
  files[filename] = code
16
  if filename not in open_files:
17
  open_files.append(filename)
18
- return code, filename, list(files.keys()), open_files, code
19
- return files.get(current_file, ""), current_file, list(files.keys()), open_files, files.get(current_file, "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Save current file content
22
  def save_file(code, current_file):
@@ -68,7 +84,7 @@ def add_new_file(open_files, files):
68
  new_file = f"untitled_{len(files)}.py"
69
  files[new_file] = ""
70
  open_files.append(new_file)
71
- return "", new_file, list(files.keys()), open_files, files[new_file]
72
 
73
  # Close tab
74
  def close_tab(open_files, current_file, files):
@@ -83,13 +99,13 @@ def download_code(code, current_file):
83
  return code, current_file
84
 
85
  # Process command palette
86
- def process_palette(cmd, theme, files, current_file):
87
  if cmd == "theme dark":
88
  return "dark", "Theme set to dark"
89
  elif cmd == "theme light":
90
  return "light", "Theme set to light"
91
  elif cmd == "save":
92
- files[current_file] = code_editor.value
93
  return theme, f"Saved {current_file}"
94
  return theme, f"Command not found: {cmd}"
95
 
@@ -99,7 +115,7 @@ with gr.Blocks(
99
  css="""
100
  .gradio-container { background-color: #1e1e1e; color: #d4d4d4; font-family: 'Consolas', monospace; height: 100vh; margin: 0; }
101
  .activity-bar { background-color: #333; padding: 10px; width: 50px; min-width: 50px; }
102
- .sidebar { background-color: #252526; padding: 10px; width: 200px; min-width: 200px; }
103
  .editor { background-color: #1e1e1e; border: none; }
104
  .terminal { background-color: #0e0e0e; color: #b5cea8; font-size: 12px; }
105
  .status-bar { background-color: #007acc; color: white; padding: 5px; }
@@ -108,7 +124,7 @@ with gr.Blocks(
108
  .tab.active { background-color: #007acc; }
109
  button { background-color: #007acc; color: white; border: none; padding: 5px; margin: 2px; }
110
  button:hover { background-color: #005f99; }
111
- .file-list { max-height: 300px; overflow-y: auto; }
112
  .command-palette { position: absolute; top: 20%; left: 30%; width: 40%; background: #252526; z-index: 1000; padding: 10px; }
113
  .light .gradio-container { background-color: #ffffff; color: #000000; }
114
  .light .editor { background-color: #ffffff; color: #000000; }
@@ -137,7 +153,13 @@ with gr.Blocks(
137
  with gr.Column(scale=1, visible=False, elem_classes="sidebar") as sidebar:
138
  gr.Markdown("### Explorer")
139
  file_upload = gr.File(label="Upload File")
140
- file_list = gr.Listbox(label="Files", choices=list(files.keys()), interactive=True, elem_classes="file-list")
 
 
 
 
 
 
141
 
142
  # Editor Pane
143
  with gr.Column(scale=4):
@@ -153,7 +175,7 @@ with gr.Blocks(
153
  terminal_output = gr.Textbox(label="Terminal", lines=10, interactive=False)
154
  terminal_input = gr.Textbox(label=">", placeholder="Type command and press Enter")
155
  with gr.Column(scale=1):
156
- suggestions_list = gr.Listbox(label="Suggestions", choices=[], interactive=True)
157
 
158
  # Status Bar
159
  with gr.Row(elem_classes="status-bar"):
@@ -233,7 +255,13 @@ with gr.Blocks(
233
  file_upload.change(
234
  load_file,
235
  inputs=[file_upload, current_file, file_tabs.choices],
236
- outputs=[code_editor, current_file, file_list, file_tabs, code_editor]
 
 
 
 
 
 
237
  )
238
 
239
  save_btn.click(
@@ -272,7 +300,7 @@ with gr.Blocks(
272
  outputs=[suggestions_list]
273
  )
274
 
275
- suggestions_list.select(
276
  insert_suggestion,
277
  inputs=[code_editor, suggestions_list],
278
  outputs=[code_editor]
@@ -287,7 +315,7 @@ with gr.Blocks(
287
  new_file_btn.click(
288
  add_new_file,
289
  inputs=[file_tabs.choices, files],
290
- outputs=[code_editor, current_file, file_list, file_tabs, code_editor]
291
  )
292
 
293
  close_tab_btn.click(
@@ -300,19 +328,9 @@ with gr.Blocks(
300
  outputs=[file_tabs]
301
  )
302
 
303
- file_list.select(
304
- lambda selected, open_files, files: (files[selected], selected, open_files + [selected] if selected not in open_files else open_files),
305
- inputs=[file_list, file_tabs.choices, files],
306
- outputs=[code_editor, current_file, file_tabs.choices]
307
- ).then(
308
- lambda open_files, current_file: gr.update(choices=open_files, value=current_file),
309
- inputs=[file_tabs.choices, current_file],
310
- outputs=[file_tabs]
311
- )
312
-
313
  command_palette.submit(
314
  process_palette,
315
- inputs=[command_palette, theme_state, files, current_file],
316
  outputs=[theme_state, status_info]
317
  ).then(lambda: gr.update(visible=False), outputs=[command_palette])
318
 
 
1
  import gradio as gr
2
  import time
3
+ import os
4
+ from pathlib import Path
5
 
6
  # Simulated file storage (in-memory dictionary)
7
  files = {"untitled.py": ""} # file_name: content
 
17
  files[filename] = code
18
  if filename not in open_files:
19
  open_files.append(filename)
20
+ return code, filename, open_files, code
21
+ return files.get(current_file, ""), current_file, open_files, files.get(current_file, "")
22
+
23
+ # Load file from FileExplorer
24
+ def load_from_explorer(selected_files, files, open_files):
25
+ if isinstance(selected_files, str): # Single file selected
26
+ selected_files = [selected_files]
27
+ if selected_files and selected_files[0]:
28
+ filename = os.path.basename(selected_files[0])
29
+ with open(selected_files[0], "r") as f:
30
+ code = f.read()
31
+ files[filename] = code
32
+ if filename not in open_files:
33
+ open_files.append(filename)
34
+ return code, filename, open_files, code
35
+ return "", list(files.keys())[0], open_files, ""
36
 
37
  # Save current file content
38
  def save_file(code, current_file):
 
84
  new_file = f"untitled_{len(files)}.py"
85
  files[new_file] = ""
86
  open_files.append(new_file)
87
+ return "", new_file, open_files, files[new_file]
88
 
89
  # Close tab
90
  def close_tab(open_files, current_file, files):
 
99
  return code, current_file
100
 
101
  # Process command palette
102
+ def process_palette(cmd, theme, files, current_file, code):
103
  if cmd == "theme dark":
104
  return "dark", "Theme set to dark"
105
  elif cmd == "theme light":
106
  return "light", "Theme set to light"
107
  elif cmd == "save":
108
+ files[current_file] = code
109
  return theme, f"Saved {current_file}"
110
  return theme, f"Command not found: {cmd}"
111
 
 
115
  css="""
116
  .gradio-container { background-color: #1e1e1e; color: #d4d4d4; font-family: 'Consolas', monospace; height: 100vh; margin: 0; }
117
  .activity-bar { background-color: #333; padding: 10px; width: 50px; min-width: 50px; }
118
+ .sidebar { background-color: #252526; padding: 10px; width: 250px; min-width: 250px; }
119
  .editor { background-color: #1e1e1e; border: none; }
120
  .terminal { background-color: #0e0e0e; color: #b5cea8; font-size: 12px; }
121
  .status-bar { background-color: #007acc; color: white; padding: 5px; }
 
124
  .tab.active { background-color: #007acc; }
125
  button { background-color: #007acc; color: white; border: none; padding: 5px; margin: 2px; }
126
  button:hover { background-color: #005f99; }
127
+ .file-explorer { max-height: 300px; overflow-y: auto; }
128
  .command-palette { position: absolute; top: 20%; left: 30%; width: 40%; background: #252526; z-index: 1000; padding: 10px; }
129
  .light .gradio-container { background-color: #ffffff; color: #000000; }
130
  .light .editor { background-color: #ffffff; color: #000000; }
 
153
  with gr.Column(scale=1, visible=False, elem_classes="sidebar") as sidebar:
154
  gr.Markdown("### Explorer")
155
  file_upload = gr.File(label="Upload File")
156
+ file_explorer = gr.FileExplorer(
157
+ label="Files",
158
+ glob="*.py",
159
+ file_count="single",
160
+ root_dir=os.getcwd(),
161
+ elem_classes="file-explorer"
162
+ )
163
 
164
  # Editor Pane
165
  with gr.Column(scale=4):
 
175
  terminal_output = gr.Textbox(label="Terminal", lines=10, interactive=False)
176
  terminal_input = gr.Textbox(label=">", placeholder="Type command and press Enter")
177
  with gr.Column(scale=1):
178
+ suggestions_list = gr.Radio(choices=[], label="Suggestions", type="value")
179
 
180
  # Status Bar
181
  with gr.Row(elem_classes="status-bar"):
 
255
  file_upload.change(
256
  load_file,
257
  inputs=[file_upload, current_file, file_tabs.choices],
258
+ outputs=[code_editor, current_file, file_tabs, code_editor]
259
+ )
260
+
261
+ file_explorer.change(
262
+ load_from_explorer,
263
+ inputs=[file_explorer, files, file_tabs.choices],
264
+ outputs=[code_editor, current_file, file_tabs, code_editor]
265
  )
266
 
267
  save_btn.click(
 
300
  outputs=[suggestions_list]
301
  )
302
 
303
+ suggestions_list.change(
304
  insert_suggestion,
305
  inputs=[code_editor, suggestions_list],
306
  outputs=[code_editor]
 
315
  new_file_btn.click(
316
  add_new_file,
317
  inputs=[file_tabs.choices, files],
318
+ outputs=[code_editor, current_file, file_list, code_editor]
319
  )
320
 
321
  close_tab_btn.click(
 
328
  outputs=[file_tabs]
329
  )
330
 
 
 
 
 
 
 
 
 
 
 
331
  command_palette.submit(
332
  process_palette,
333
+ inputs=[command_palette, theme_state, files, current_file, code_editor],
334
  outputs=[theme_state, status_info]
335
  ).then(lambda: gr.update(visible=False), outputs=[command_palette])
336