Files
Brancheneinstufung2/app.py
2025-08-08 12:07:11 +00:00

53 lines
1.9 KiB
Python

# app.py
from flask import Flask, jsonify, request
import subprocess
import sys
import os
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
app = Flask(__name__)
SCRIPT_MAP = {
"run_duplicate_check": {
"script": "duplicate_checker.py",
"args": []
},
"run_reclassify_branches": {
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
"args": ["--mode", "reclassify_branches"]
},
"run_predict_technicians": {
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
"args": ["--mode", "predict_technicians"]
}
}
@app.route('/run-script', methods=['POST']) # <-- Der korrekte Endpunkt
def run_script():
data = request.get_json()
action = data.get('action')
if not action or action not in SCRIPT_MAP:
return jsonify({"status": "error", "message": "Ungültige oder fehlende Aktion."}), 400
script_config = SCRIPT_MAP[action]
script_name = script_config["script"]
script_args = script_config["args"]
if not os.path.exists(script_name):
logging.error(f"Skript '{script_name}' für Aktion '{action}' nicht gefunden.")
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
try:
logging.info(f"Starte Aktion '{action}' -> Skript: '{script_name}' mit Argumenten: {script_args}")
python_executable = sys.executable
command = [python_executable, script_name] + script_args
subprocess.Popen(command)
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet."}), 200
except Exception as e:
logging.error(f"Fehler beim Starten des Skripts für Aktion '{action}': {e}")
return jsonify({"status": "error", "message": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)