Artificial-superintelligence commited on
Commit
4d30d4b
·
verified ·
1 Parent(s): 2784c23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -28
app.py CHANGED
@@ -1,40 +1,37 @@
1
- from flask import Flask, request, jsonify
2
- import sys
3
  import io
4
- import contextlib
5
 
6
  app = Flask(__name__)
7
 
8
- # Function to execute Python code safely
9
- def execute_python_code(code):
10
- # Capturing the output of Python execution
 
 
 
 
 
 
 
 
11
  output = io.StringIO()
12
  sys.stdout = output
13
  sys.stderr = output
14
-
15
  try:
16
- # Executing the code
17
  exec(code)
 
18
  except Exception as e:
19
- output.write(f"Error: {e}")
20
-
21
- # Return the captured output
22
- return output.getvalue()
23
-
24
- @app.route('/')
25
- def index():
26
- return app.send_static_file('index.html')
27
 
28
- @app.route('/execute', methods=['POST'])
29
- def execute():
30
- data = request.get_json()
31
- code = data.get('code', '')
32
-
33
- # Execute the Python code and capture the result
34
- result = execute_python_code(code)
35
-
36
- # Send the result back to the frontend
37
- return jsonify({'result': result})
38
 
39
- if __name__ == '__main__':
40
- app.run(debug=True)
 
 
1
+ from flask import Flask, request, jsonify, render_template
 
2
  import io
3
+ import sys
4
 
5
  app = Flask(__name__)
6
 
7
+ @app.route("/")
8
+ def index():
9
+ # Render the terminal-like interface
10
+ return render_template("index.html")
11
+
12
+ @app.route("/execute", methods=["POST"])
13
+ def execute_code():
14
+ # Get the Python code sent from the frontend
15
+ code = request.json.get("code", "")
16
+
17
+ # Redirect standard output to capture execution output
18
  output = io.StringIO()
19
  sys.stdout = output
20
  sys.stderr = output
21
+
22
  try:
23
+ # Execute the provided code
24
  exec(code)
25
+ result = output.getvalue()
26
  except Exception as e:
27
+ result = f"Error: {e}"
28
+ finally:
29
+ # Reset standard output
30
+ sys.stdout = sys.__stdout__
31
+ sys.stderr = sys.__stderr__
 
 
 
32
 
33
+ return jsonify({"result": result})
 
 
 
 
 
 
 
 
 
34
 
35
+ if __name__ == "__main__":
36
+ # Run the app on host 0.0.0.0 and port 7860 for compatibility with Hugging Face Spaces
37
+ app.run(host="0.0.0.0", port=7860)