broadfield-dev commited on
Commit
5859778
·
verified ·
1 Parent(s): b28c8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -5,12 +5,21 @@ import os
5
 
6
  app = Flask(__name__)
7
 
 
 
 
 
 
 
 
 
8
  @app.route('/', methods=['GET', 'POST'])
9
  def index():
10
  if request.method == 'POST':
11
  parts = None
12
  filename = 'unnamed.py'
13
 
 
14
  if 'file' in request.files and request.files['file'].filename:
15
  file = request.files['file']
16
  if not file.filename.endswith('.py'):
@@ -22,6 +31,7 @@ def index():
22
  code = f.read()
23
  parts = parse_python_code(code)
24
 
 
25
  elif 'code' in request.form and request.form['code'].strip():
26
  code = request.form['code']
27
  filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
@@ -31,7 +41,8 @@ def index():
31
 
32
  if parts:
33
  indexed_parts = [{'index': i + 1, **part} for i, part in enumerate(parts)]
34
- return render_template('results.html', parts=indexed_parts, filename=filename)
 
35
  return 'No file or code provided', 400
36
 
37
  return render_template('index.html')
@@ -39,4 +50,4 @@ def index():
39
  if __name__ == '__main__':
40
  if not os.path.exists('uploads'):
41
  os.makedirs('uploads')
42
- app.run(host="0.0.0.0",port=7860)
 
5
 
6
  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
+ # Concatenate the source strings
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:
24
  file = request.files['file']
25
  if not file.filename.endswith('.py'):
 
31
  code = f.read()
32
  parts = parse_python_code(code)
33
 
34
+ # Handle pasted code
35
  elif 'code' in request.form and request.form['code'].strip():
36
  code = request.form['code']
37
  filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
 
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
+ return render_template('results.html', parts=indexed_parts, filename=filename, reconstructed_code=reconstructed_code)
46
  return 'No file or code provided', 400
47
 
48
  return render_template('index.html')
 
50
  if __name__ == '__main__':
51
  if not os.path.exists('uploads'):
52
  os.makedirs('uploads')
53
+ app.run(port=7860)