File size: 1,900 Bytes
8abe1df
86cb17d
8abe1df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from flask import Flask, render_template_string

app = Flask(__name__)

# HTML template using Jinja2 syntax to insert the URL safely
# Added basic styling to make the iframe fill the page
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
    <title>Iframe Viewer</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            width: 100%;
            overflow: hidden; /* Prevent scrollbars on the main page */
        }
        iframe {
            display: block; /* Removes bottom space under iframe */
            width: 100%;
            height: 100%;
            border: none; /* Remove default border */
        }
        .error {
            padding: 20px;
            color: red;
            font-family: sans-serif;
        }
    </style>
</head>
<body>
    {% if iframe_url %}
        <iframe src="{{ iframe_url }}">
            Your browser does not support iframes.
        </iframe>
    {% else %}
        <div class="error">
            Error: The 'IFRAME_URL' secret is not set in the Space settings.
            Please add it under Settings -> Repository secrets.
        </div>
    {% endif %}
</body>
</html>
"""

@app.route('/')
def display_iframe():
    # Read the URL from the environment variable 'IFRAME_URL'
    # os.getenv returns None if the variable isn't set
    url_from_env = os.getenv('IFRAME_URL')

    # Pass the URL to the template rendering function
    # Flask's render_template_string automatically handles basic HTML escaping
    # for security if used directly in text, but here it's okay for the src attribute.
    return render_template_string(HTML_TEMPLATE, iframe_url=url_from_env)

if __name__ == '__main__':
    # Hugging Face Spaces expect the app to run on port 7860
    # Binding to '0.0.0.0' makes it accessible from outside the container
    app.run(host='0.0.0.0', port=7860)