Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from urllib.parse import urlparse, urljoin
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
import subprocess
|
7 |
-
import autopep8
|
8 |
|
9 |
# HTML and JavaScript for the "Copy Code" button
|
10 |
copy_button_html = """
|
@@ -175,23 +174,36 @@ async def fetch_space_file_content(space_url, file_path):
|
|
175 |
except Exception as e:
|
176 |
return f"Error: {e}"
|
177 |
|
178 |
-
# Function to check and
|
179 |
-
def
|
180 |
-
"""Checks
|
181 |
try:
|
182 |
-
#
|
183 |
result = subprocess.run(
|
184 |
["flake8", "--stdin-display-name", "app.py", "-"],
|
185 |
input=code.encode(),
|
186 |
capture_output=True,
|
|
|
187 |
)
|
188 |
|
189 |
if result.returncode == 0:
|
190 |
return "No errors found in the code.", code
|
191 |
|
192 |
-
# If there are errors,
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
except Exception as e:
|
196 |
return f"Error: {e}", code
|
197 |
|
@@ -295,7 +307,7 @@ with gr.Blocks() as demo:
|
|
295 |
|
296 |
# Обработчик для кнопки Correcter
|
297 |
def correct_code_handler(code):
|
298 |
-
result, corrected_code =
|
299 |
return result, corrected_code
|
300 |
|
301 |
correcter_button.click(
|
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
import subprocess
|
|
|
7 |
|
8 |
# HTML and JavaScript for the "Copy Code" button
|
9 |
copy_button_html = """
|
|
|
174 |
except Exception as e:
|
175 |
return f"Error: {e}"
|
176 |
|
177 |
+
# Function to check code for errors and provide detailed feedback
|
178 |
+
def check_code_for_errors(code):
|
179 |
+
"""Checks the code for errors using flake8 and provides detailed feedback."""
|
180 |
try:
|
181 |
+
# Run flake8 to check for errors
|
182 |
result = subprocess.run(
|
183 |
["flake8", "--stdin-display-name", "app.py", "-"],
|
184 |
input=code.encode(),
|
185 |
capture_output=True,
|
186 |
+
text=True,
|
187 |
)
|
188 |
|
189 |
if result.returncode == 0:
|
190 |
return "No errors found in the code.", code
|
191 |
|
192 |
+
# If there are errors, parse the output and provide detailed feedback
|
193 |
+
errors = result.stderr if result.stderr else result.stdout
|
194 |
+
error_messages = []
|
195 |
+
for line in errors.splitlines():
|
196 |
+
if ":" in line:
|
197 |
+
parts = line.split(":")
|
198 |
+
if len(parts) >= 4:
|
199 |
+
file_name, line_number, column, message = parts[0], parts[1], parts[2], ":".join(parts[3:])
|
200 |
+
error_messages.append(f"Line {line_number}, Column {column}: {message.strip()}")
|
201 |
+
|
202 |
+
if not error_messages:
|
203 |
+
return "Errors found, but could not parse details.", code
|
204 |
+
|
205 |
+
detailed_feedback = "Errors found in the code:\n" + "\n".join(error_messages)
|
206 |
+
return detailed_feedback, code
|
207 |
except Exception as e:
|
208 |
return f"Error: {e}", code
|
209 |
|
|
|
307 |
|
308 |
# Обработчик для кнопки Correcter
|
309 |
def correct_code_handler(code):
|
310 |
+
result, corrected_code = check_code_for_errors(code)
|
311 |
return result, corrected_code
|
312 |
|
313 |
correcter_button.click(
|