from flask import Flask, render_template_string, request, redirect, url_for
import json # Added import for json
from src.config_manager import ConfigManager
app = Flask(__name__)
config_manager = ConfigManager()
CONFIG_TEMPLATE = """
LED Matrix Config
LED Matrix Configuration
"""
@app.route('/')
def display_config_route():
try:
current_config = config_manager.load_config()
# Pretty print JSON for the textarea
config_json = json.dumps(current_config, indent=4)
return render_template_string(CONFIG_TEMPLATE, config_json=config_json)
except Exception as e:
return f"Error loading configuration: {str(e)}", 500
@app.route('/save', methods=['POST'])
def save_config_route():
try:
new_config_str = request.form['config_data']
new_config = json.loads(new_config_str) # Parse the JSON string from textarea
config_manager.save_config(new_config)
return redirect(url_for('display_config_route'))
except json.JSONDecodeError:
return "Error: Invalid JSON format submitted.", 400
except Exception as e:
return f"Error saving configuration: {str(e)}", 500
if __name__ == '__main__':
# Make sure to run with debug=True only for development
# In a production environment, use a proper WSGI server like Gunicorn
app.run(debug=True, host='0.0.0.0', port=5000)