Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, send_file | |
| import io | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = Flask(__name__) | |
| # Default configuration to prevent missing data errors | |
| DEFAULTS = { | |
| 'theme': 'modern', | |
| 'title': '404', | |
| 'message': 'Oops! The page you are looking for has vanished into the void.', | |
| 'buttonText': 'Back to Safety', | |
| 'buttonLink': '/', | |
| 'bgColor': '#ffffff', | |
| 'textColor': '#1f2937', | |
| 'accentColor': '#ef4444', | |
| 'illustration': 'ghost', | |
| 'showGame': False | |
| } | |
| def index(): | |
| return render_template('index.html') | |
| def preview(): | |
| """ | |
| Returns the HTML for the preview iframe. | |
| """ | |
| try: | |
| # Get JSON data, defaulting to empty dict if None | |
| data = request.get_json(silent=True) or {} | |
| # Merge defaults with provided data (data overrides defaults) | |
| context = {**DEFAULTS, **data} | |
| return render_template('export_theme.html', **context) | |
| except Exception as e: | |
| logger.error(f"Preview error: {e}") | |
| return f"Internal Server Error: {str(e)}", 500 | |
| def download(): | |
| """ | |
| Returns the HTML as a downloadable file. | |
| """ | |
| try: | |
| data = request.get_json(silent=True) or {} | |
| context = {**DEFAULTS, **data} | |
| html_content = render_template('export_theme.html', **context) | |
| # Create a file-like object | |
| mem = io.BytesIO() | |
| mem.write(html_content.encode('utf-8')) | |
| mem.seek(0) | |
| return send_file( | |
| mem, | |
| as_attachment=True, | |
| download_name='404.html', | |
| mimetype='text/html' | |
| ) | |
| except Exception as e: | |
| logger.error(f"Download error: {e}") | |
| return f"Internal Server Error: {str(e)}", 500 | |
| if __name__ == '__main__': | |
| # Use port 7860 for Hugging Face Spaces compatibility | |
| app.run(debug=True, port=7860, host='0.0.0.0') | |