mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 13:02:59 +00:00
first add of webui
This commit is contained in:
@@ -15,4 +15,3 @@ python-socketio
|
||||
python-engineio
|
||||
websockets
|
||||
websocket-client
|
||||
requests
|
||||
@@ -38,6 +38,20 @@ class ConfigManager:
|
||||
print(f"Error loading configuration: {str(e)}")
|
||||
raise
|
||||
|
||||
def save_config(self, config_data: Dict[str, Any]) -> None:
|
||||
"""Save configuration to the main JSON file."""
|
||||
try:
|
||||
with open(self.config_path, 'w') as f:
|
||||
json.dump(config_data, f, indent=4)
|
||||
self.config = config_data # Update the in-memory config
|
||||
print(f"Configuration successfully saved to {os.path.abspath(self.config_path)}")
|
||||
except IOError as e:
|
||||
print(f"Error writing configuration to file {os.path.abspath(self.config_path)}: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"An unexpected error occurred while saving configuration: {str(e)}")
|
||||
raise
|
||||
|
||||
def _deep_merge(self, target: Dict, source: Dict) -> None:
|
||||
"""Deep merge source dict into target dict."""
|
||||
for key, value in source.items():
|
||||
|
||||
62
web_interface.py
Normal file
62
web_interface.py
Normal file
@@ -0,0 +1,62 @@
|
||||
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 = """
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>LED Matrix Config</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; margin: 20px; }
|
||||
label { display: block; margin-top: 10px; }
|
||||
input[type="text"], textarea { width: 100%; padding: 8px; margin-top: 5px; border-radius: 4px; border: 1px solid #ccc; box-sizing: border-box; }
|
||||
input[type="submit"] { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px;}
|
||||
input[type="submit"]:hover { background-color: #45a049; }
|
||||
.container { max-width: 600px; margin: auto; background: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
|
||||
h1 { text-align: center; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>LED Matrix Configuration</h1>
|
||||
<form method="post" action="{{ url_for('save_config_route') }}">
|
||||
<label for="config_data">Configuration (JSON):</label>
|
||||
<textarea name="config_data" rows="20" cols="80">{{ config_json }}</textarea><br>
|
||||
<input type="submit" value="Save Configuration">
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
@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)
|
||||
Reference in New Issue
Block a user