File size: 2,087 Bytes
d2cc112
2e4ea36
d2cc112
 
 
 
 
2e4ea36
 
 
d2cc112
 
 
 
 
 
 
 
 
 
 
 
 
 
2e4ea36
 
 
 
 
 
 
 
 
d2cc112
 
 
 
 
 
 
 
 
 
 
2e4ea36
 
 
 
 
 
d2cc112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e4ea36
 
d2cc112
2e4ea36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
}

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/preview', methods=['POST'])
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

@app.route('/download', methods=['POST'])
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')