Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,17 +7,15 @@ app = Flask(__name__)
|
|
7 |
|
8 |
def reconstruct_code(parts):
|
9 |
"""Reconstruct the original code from parsed parts."""
|
10 |
-
# Sort parts by start line to ensure correct order
|
11 |
sorted_parts = sorted(parts, key=lambda p: p['location'][0])
|
12 |
-
|
13 |
-
reconstructed = ''.join(part['source'] for part in sorted_parts)
|
14 |
-
return reconstructed
|
15 |
|
16 |
@app.route('/', methods=['GET', 'POST'])
|
17 |
def index():
|
18 |
if request.method == 'POST':
|
19 |
parts = None
|
20 |
filename = 'unnamed.py'
|
|
|
21 |
|
22 |
# Handle file upload
|
23 |
if 'file' in request.files and request.files['file'].filename:
|
@@ -28,26 +26,34 @@ def index():
|
|
28 |
file_path = os.path.join('uploads', filename)
|
29 |
file.save(file_path)
|
30 |
with open(file_path, 'r') as f:
|
31 |
-
|
32 |
-
parts = parse_python_code(
|
33 |
|
34 |
# Handle pasted code
|
35 |
elif 'code' in request.form and request.form['code'].strip():
|
36 |
-
|
37 |
filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
|
38 |
if not filename.endswith('.py'):
|
39 |
filename += '.py'
|
40 |
-
parts = parse_python_code(
|
41 |
|
42 |
if parts:
|
43 |
indexed_parts = [{'index': i + 1, **part} for i, part in enumerate(parts)]
|
44 |
reconstructed_code = reconstruct_code(indexed_parts)
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return 'No file or code provided', 400
|
47 |
|
48 |
-
|
|
|
49 |
|
50 |
if __name__ == '__main__':
|
51 |
if not os.path.exists('uploads'):
|
52 |
os.makedirs('uploads')
|
53 |
-
app.run(host="0.0.0.0",
|
|
|
7 |
|
8 |
def reconstruct_code(parts):
|
9 |
"""Reconstruct the original code from parsed parts."""
|
|
|
10 |
sorted_parts = sorted(parts, key=lambda p: p['location'][0])
|
11 |
+
return ''.join(part['source'] for part in sorted_parts)
|
|
|
|
|
12 |
|
13 |
@app.route('/', methods=['GET', 'POST'])
|
14 |
def index():
|
15 |
if request.method == 'POST':
|
16 |
parts = None
|
17 |
filename = 'unnamed.py'
|
18 |
+
code_input = None
|
19 |
|
20 |
# Handle file upload
|
21 |
if 'file' in request.files and request.files['file'].filename:
|
|
|
26 |
file_path = os.path.join('uploads', filename)
|
27 |
file.save(file_path)
|
28 |
with open(file_path, 'r') as f:
|
29 |
+
code_input = f.read()
|
30 |
+
parts = parse_python_code(code_input)
|
31 |
|
32 |
# Handle pasted code
|
33 |
elif 'code' in request.form and request.form['code'].strip():
|
34 |
+
code_input = request.form['code']
|
35 |
filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
|
36 |
if not filename.endswith('.py'):
|
37 |
filename += '.py'
|
38 |
+
parts = parse_python_code(code_input)
|
39 |
|
40 |
if parts:
|
41 |
indexed_parts = [{'index': i + 1, **part} for i, part in enumerate(parts)]
|
42 |
reconstructed_code = reconstruct_code(indexed_parts)
|
43 |
+
# Return partial HTML for htmx to update the results section
|
44 |
+
return render_template(
|
45 |
+
'results_partial.html',
|
46 |
+
parts=indexed_parts,
|
47 |
+
filename=filename,
|
48 |
+
reconstructed_code=reconstructed_code,
|
49 |
+
code_input=code_input
|
50 |
+
)
|
51 |
return 'No file or code provided', 400
|
52 |
|
53 |
+
# Initial page load
|
54 |
+
return render_template('index.html', parts=None, filename=None, reconstructed_code=None, code_input=None)
|
55 |
|
56 |
if __name__ == '__main__':
|
57 |
if not os.path.exists('uploads'):
|
58 |
os.makedirs('uploads')
|
59 |
+
app.run(host="0.0.0.0",port=7860)
|