From 4066d301958f1b0c8da1ac59728f9e93334ddf2d Mon Sep 17 00:00:00 2001 From: Chuck <33324927+ChuckBuilds@users.noreply.github.com> Date: Sat, 31 May 2025 11:38:40 -0500 Subject: [PATCH] first add of webui --- requirements.txt | 3 +-- src/config_manager.py | 14 ++++++++++ web_interface.py | 62 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 web_interface.py diff --git a/requirements.txt b/requirements.txt index db0f4b29..ffacb9d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,5 +14,4 @@ icalevents python-socketio python-engineio websockets -websocket-client -requests \ No newline at end of file +websocket-client \ No newline at end of file diff --git a/src/config_manager.py b/src/config_manager.py index 4173d235..cf785c8b 100644 --- a/src/config_manager.py +++ b/src/config_manager.py @@ -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(): diff --git a/web_interface.py b/web_interface.py new file mode 100644 index 00000000..95fd77ff --- /dev/null +++ b/web_interface.py @@ -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 = """ + + + + + 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) \ No newline at end of file