Natkat1 commited on
Commit
1f0b8a2
·
verified ·
1 Parent(s): 7f99fb3

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +193 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import io
4
+ import sys
5
+ import traceback
6
+
7
+ def init_demo():
8
+ files = {
9
+ "main.py": 'print("Hello, Gradio Code Editor!")\nprint("This is a demo file.")',
10
+ "app.py": 'import gradio as gr\n\ndef greet(name):\n return f"Hello, {name}!"\n\nprint("App loaded")',
11
+ "utils.py": 'def utility_function():\n """A sample utility function."""\n return "Utility result"\n\nprint(utility_function())'
12
+ }
13
+ df_data = pd.DataFrame({"Filename": list(files.keys())})
14
+ return df_data, files, None
15
+
16
+ def select_file(evt: gr.SelectData, files_state):
17
+ if evt.index:
18
+ filename = evt.value["Filename"]
19
+ code = files_state.get(filename, "")
20
+ return code, filename
21
+ return "", None
22
+
23
+ def add_file(new_name, files_state, df):
24
+ if not new_name.strip():
25
+ raise gr.Error("Please enter a file name.")
26
+ if new_name in files_state:
27
+ raise gr.Error("File already exists.")
28
+ files_state[new_name] = "# New file content"
29
+ new_df = pd.DataFrame({"Filename": list(files_state.keys())})
30
+ return new_df, files_state
31
+
32
+ def delete_file(evt: gr.SelectData, files_state, current_file_state, df):
33
+ if evt.index:
34
+ filename = evt.value["Filename"]
35
+ del files_state[filename]
36
+ new_df = pd.DataFrame({"Filename": list(files_state.keys())})
37
+ if current_file_state == filename:
38
+ return new_df, files_state, None
39
+ return df, files_state, current_file_state
40
+
41
+ def save_file(code, current_file, files_state, df):
42
+ if not current_file or current_file not in files_state:
43
+ raise gr.Error("No file selected or invalid file.")
44
+ files_state[current_file] = code
45
+ return gr.update(value="Saved successfully!"), df, files_state
46
+
47
+ def run_code(code):
48
+ output = io.StringIO()
49
+ old_stdout = sys.stdout
50
+ old_stderr = sys.stderr
51
+ sys.stdout = output
52
+ sys.stderr = output
53
+ g = {"__builtins__": {}}
54
+ g.update(print=print, pd=pd, gr=gr, io=io, sys=sys) # Safe globals
55
+ try:
56
+ exec(code, g)
57
+ except Exception:
58
+ sys.stdout = old_stdout
59
+ sys.stderr = old_stderr
60
+ return traceback.format_exc()
61
+ finally:
62
+ sys.stdout = old_stdout
63
+ sys.stderr = old_stderr
64
+ return output.getvalue()
65
+
66
+ custom_theme = gr.themes.Monochrome(
67
+ primary_hue="blue",
68
+ secondary_hue="indigo",
69
+ neutral_hue="slate",
70
+ font=gr.themes.GoogleFont("JetBrains Mono"),
71
+ font_mono=gr.themes.GoogleFont("JetBrains Mono"),
72
+ ).set(
73
+ code_background_fill_dark="*neutral_900",
74
+ code_token_operator_color_dark="*blue_400",
75
+ code_token_constant_color_dark="*green_400",
76
+ code_token_function_color_dark="*blue_400",
77
+ code_token_keyword_color_dark="*purple_400",
78
+ )
79
+
80
+ css = """
81
+ #left-panel {
82
+ background-color: var(--neutral-900);
83
+ border-right: 1px solid var(--neutral-700);
84
+ overflow-y: auto;
85
+ max-height: 80vh;
86
+ }
87
+ #right-panel {
88
+ background-color: var(--neutral-925);
89
+ }
90
+ .monaco-editor .monaco-editor-background {
91
+ background-color: #1e1e1e !important;
92
+ }
93
+ """
94
+
95
+ with gr.Blocks(theme=custom_theme, css=css) as demo:
96
+ gr.Markdown("# Modern Code Editor")
97
+ gr.Markdown("[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
98
+
99
+ files_state = gr.State({})
100
+ current_file_state = gr.State(None)
101
+
102
+ with gr.Row():
103
+ with gr.Column(scale=1, min_width=280, elem_id="left-panel", elem_classes="panel"):
104
+ gr.Markdown("## 📁 File Explorer")
105
+ files_df = gr.Dataframe(
106
+ headers=["Filename"],
107
+ datatype=["str"],
108
+ interactive=True,
109
+ wrap=False,
110
+ show_row_numbers=False,
111
+ max_height=300,
112
+ )
113
+ gr.Markdown("### Actions")
114
+ new_file_name = gr.Textbox(label="New File Name", placeholder="e.g., new_script.py")
115
+ with gr.Row():
116
+ add_btn = gr.Button("➕ Add File", variant="secondary", scale=1)
117
+ delete_btn = gr.Button("🗑️ Delete Selected", variant="stop", scale=1)
118
+ save_btn = gr.Button("💾 Save Current File", variant="primary")
119
+ status_msg = gr.Markdown("Ready", visible=False)
120
+
121
+ with gr.Column(scale=4, elem_id="right-panel", elem_classes="panel"):
122
+ current_file_display = gr.Markdown("**No file selected**", elem_id="current-file")
123
+ editor = gr.Code(
124
+ value="",
125
+ language="python",
126
+ lines=25,
127
+ font_size="14px",
128
+ show_line_numbers=True,
129
+ autocomplete=True,
130
+ theme="vs-dark",
131
+ )
132
+ with gr.Row():
133
+ run_btn = gr.Button("▶️ Run Code", variant="primary", scale=0)
134
+ output = gr.Textbox(
135
+ label="Output",
136
+ lines=12,
137
+ max_lines=20,
138
+ interactive=False,
139
+ show_copy_button=True,
140
+ )
141
+
142
+ # Event handlers
143
+ demo.load(init_demo, outputs=[files_df, files_state, current_file_state])
144
+
145
+ files_df.select(
146
+ select_file,
147
+ inputs=[files_state],
148
+ outputs=[editor, current_file_display],
149
+ show_progress="minimal"
150
+ ).then(
151
+ lambda file: f"**Current: {file or 'None'}**",
152
+ current_file_state,
153
+ current_file_display
154
+ )
155
+
156
+ add_btn.click(
157
+ add_file,
158
+ inputs=[new_file_name, files_state, files_df],
159
+ outputs=[files_df, files_state],
160
+ )
161
+
162
+ delete_btn.click(
163
+ delete_file,
164
+ inputs=[files_state, current_file_state, files_df],
165
+ outputs=[files_df, files_state, current_file_state],
166
+ )
167
+
168
+ save_btn.click(
169
+ save_file,
170
+ inputs=[editor, current_file_state, files_state, files_df],
171
+ outputs=[status_msg, files_df, files_state],
172
+ ).then(
173
+ lambda: gr.update(visible=False),
174
+ None,
175
+ status_msg,
176
+ show_progress=False
177
+ )
178
+
179
+ run_btn.click(
180
+ run_code,
181
+ inputs=editor,
182
+ outputs=output,
183
+ show_progress="full",
184
+ )
185
+
186
+ demo.launch(
187
+ theme=custom_theme,
188
+ css=css,
189
+ footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}],
190
+ share=False,
191
+ show_error=True,
192
+ enable_queue=True,
193
+ )
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio>=6.0
2
+ requests
3
+ Pillow
4
+ pandas
5
+ numpy
6
+ openpyxl