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