# app.py from flask import Flask, send_from_directory, jsonify, request import os import json import errno app = Flask(__name__) # Directory to store settings SETTINGS_DIR = "settings" # Ensure the settings directory exists and has the correct permissions try: if not os.path.exists(SETTINGS_DIR): os.makedirs(SETTINGS_DIR, mode=0o775) os.chmod(SETTINGS_DIR, 0o775) except OSError as e: print(f"Error setting up settings directory: {e}") # Real-world simulation parameters (unscaled) simulation_params = { "sun": { "mass": 1.989e30, # Real mass in kg "position": [0, 0, 0], "orbital_velocity": 0, # Sun is stationary }, "earth": { "mass": 5.972e24, # Real mass in kg "position": [149.6e6, 0, 0], # 1 AU in km "orbital_velocity": 29.8, # Real orbital velocity in km/s }, "mars": { "mass": 6.417e23, # Real mass in kg "position": [227.9e6, 0, 0], # 1.52 AU in km "orbital_velocity": 24.1, # Real orbital velocity in km/s }, "fluid_speed": 0.1, "fluid_friction": 0.9, "fluid_deflection": 0.1, } # Serve the frontend @app.route('/') def serve_index(): return send_from_directory('static', 'index.html') # Serve static files (CSS, JS) @app.route('/static/') def serve_static(path): return send_from_directory('static', path) # API to get simulation parameters @app.route('/api/params', methods=['GET']) def get_params(): return jsonify(simulation_params) # API to update simulation parameters @app.route('/api/params', methods=['POST']) def update_params(): global simulation_params data = request.get_json() simulation_params.update(data) return jsonify({"status": "success", "params": simulation_params}) # API to save settings to a JSON file @app.route('/api/save', methods=['POST']) def save_settings(): try: filename = os.path.join(SETTINGS_DIR, "settings.json") with open(filename, 'w') as f: json.dump(simulation_params, f, indent=4) return jsonify({"status": "success", "message": "Settings saved successfully"}) except PermissionError as e: return jsonify({"status": "error", "message": "Permission denied: Unable to save settings. Please check directory permissions."}), 500 except Exception as e: return jsonify({"status": "error", "message": f"Error saving settings: {str(e)}"}), 500 # API to load settings from a JSON file @app.route('/api/load', methods=['GET']) def load_settings(): global simulation_params try: filename = os.path.join(SETTINGS_DIR, "settings.json") if os.path.exists(filename): with open(filename, 'r') as f: simulation_params = json.load(f) return jsonify({"status": "success", "params": simulation_params}) else: return jsonify({"status": "error", "message": "No saved settings found"}), 404 except PermissionError as e: return jsonify({"status": "error", "message": "Permission denied: Unable to load settings. Please check directory permissions."}), 500 except Exception as e: return jsonify({"status": "error", "message": f"Error loading settings: {str(e)}"}), 500 if __name__ == '__main__': port = int(os.environ.get('PORT', 7860)) # Default port for Hugging Face Spaces app.run(host='0.0.0.0', port=port)