From cf5adc149bd9f9a04ef8b6b608b7a1093c45de75 Mon Sep 17 00:00:00 2001 From: rainmaker Date: Tue, 16 Jul 2024 23:33:22 +0200 Subject: [PATCH] Add gui.py --- gui.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 gui.py diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..51ec389 --- /dev/null +++ b/gui.py @@ -0,0 +1,53 @@ +from flask import Flask, render_template, request, jsonify, url_for +import subprocess +import json +import logging + +app = Flask(__name__) + +# Set up logging +logging.basicConfig(level=logging.INFO) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/favion.ico') +def favicon(): + return url_for('static', filename='favicon.ico') + +@app.route('/run_script', methods=['POST']) +def run_script(): + settings = request.json + logging.info(f"Received settings: {settings}") + + # Ensure Count is an integer + settings['Count'] = int(settings['Count']) + + with open('config.json', 'w') as json_file: + json.dump(settings, json_file) + logging.info("Settings written to config.json") + + try: + # Add a timeout to the subprocess to prevent it from hanging indefinitely + result = subprocess.run(['python3', 'main.py', 'config.json'], capture_output=True, text=True, timeout=15) + output = result.stdout + error = result.stderr + logging.info(f"Script output: {output}") + logging.error(f"Script error: {error}") + except subprocess.TimeoutExpired as e: + error_message = "The script execution timed out." + logging.error(error_message) + output = e.stdout if e.stdout else "" + error = e.stderr if e.stderr else error_message + return jsonify({"output": output, "error": error}) + except Exception as e: + logging.error(f"An error occurred: {str(e)}") + return jsonify({"error": str(e)}) + + return jsonify({"output": output, "error": error}) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=False) +