Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# app.py
|
2 |
from flask import Flask, request, render_template
|
3 |
-
from parser import
|
4 |
import os
|
5 |
|
6 |
app = Flask(__name__)
|
@@ -8,23 +8,36 @@ app = Flask(__name__)
|
|
8 |
@app.route('/', methods=['GET', 'POST'])
|
9 |
def index():
|
10 |
if request.method == 'POST':
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
if __name__ == '__main__':
|
28 |
if not os.path.exists('uploads'):
|
29 |
os.makedirs('uploads')
|
30 |
-
app.run(
|
|
|
1 |
# app.py
|
2 |
from flask import Flask, request, render_template
|
3 |
+
from parser import parse_python_code
|
4 |
import os
|
5 |
|
6 |
app = Flask(__name__)
|
|
|
8 |
@app.route('/', methods=['GET', 'POST'])
|
9 |
def index():
|
10 |
if request.method == 'POST':
|
11 |
+
parts = None
|
12 |
+
filename = 'unnamed.py'
|
13 |
+
|
14 |
+
# Handle file upload
|
15 |
+
if 'file' in request.files and request.files['file'].filename:
|
16 |
+
file = request.files['file']
|
17 |
+
if not file.filename.endswith('.py'):
|
18 |
+
return 'Invalid file type. Please upload a Python file.', 400
|
19 |
+
filename = file.filename
|
20 |
+
file_path = os.path.join('uploads', filename)
|
21 |
+
file.save(file_path)
|
22 |
+
with open(file_path, 'r') as f:
|
23 |
+
code = f.read()
|
24 |
+
parts = parse_python_code(code)
|
25 |
+
|
26 |
+
# Handle pasted code
|
27 |
+
elif 'code' in request.form and request.form['code'].strip():
|
28 |
+
code = request.form['code']
|
29 |
+
filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
|
30 |
+
if not filename.endswith('.py'):
|
31 |
+
filename += '.py'
|
32 |
+
parts = parse_python_code(code)
|
33 |
+
|
34 |
+
if parts:
|
35 |
+
return render_template('results.html', parts=parts, filename=filename)
|
36 |
+
return 'No file or code provided', 400
|
37 |
+
|
38 |
+
return render_template('index.html')
|
39 |
|
40 |
if __name__ == '__main__':
|
41 |
if not os.path.exists('uploads'):
|
42 |
os.makedirs('uploads')
|
43 |
+
app.run(port=7860)
|