Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, render_template_string, escape
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# HTML template using Jinja2 syntax to insert the URL safely
|
7 |
+
# Added basic styling to make the iframe fill the page
|
8 |
+
HTML_TEMPLATE = """
|
9 |
+
<!DOCTYPE html>
|
10 |
+
<html>
|
11 |
+
<head>
|
12 |
+
<title>Iframe Viewer</title>
|
13 |
+
<style>
|
14 |
+
html, body {
|
15 |
+
margin: 0;
|
16 |
+
padding: 0;
|
17 |
+
height: 100%;
|
18 |
+
width: 100%;
|
19 |
+
overflow: hidden; /* Prevent scrollbars on the main page */
|
20 |
+
}
|
21 |
+
iframe {
|
22 |
+
display: block; /* Removes bottom space under iframe */
|
23 |
+
width: 100%;
|
24 |
+
height: 100%;
|
25 |
+
border: none; /* Remove default border */
|
26 |
+
}
|
27 |
+
.error {
|
28 |
+
padding: 20px;
|
29 |
+
color: red;
|
30 |
+
font-family: sans-serif;
|
31 |
+
}
|
32 |
+
</style>
|
33 |
+
</head>
|
34 |
+
<body>
|
35 |
+
{% if iframe_url %}
|
36 |
+
<iframe src="{{ iframe_url }}">
|
37 |
+
Your browser does not support iframes.
|
38 |
+
</iframe>
|
39 |
+
{% else %}
|
40 |
+
<div class="error">
|
41 |
+
Error: The 'IFRAME_URL' secret is not set in the Space settings.
|
42 |
+
Please add it under Settings -> Repository secrets.
|
43 |
+
</div>
|
44 |
+
{% endif %}
|
45 |
+
</body>
|
46 |
+
</html>
|
47 |
+
"""
|
48 |
+
|
49 |
+
@app.route('/')
|
50 |
+
def display_iframe():
|
51 |
+
# Read the URL from the environment variable 'IFRAME_URL'
|
52 |
+
# os.getenv returns None if the variable isn't set
|
53 |
+
url_from_env = os.getenv('IFRAME_URL')
|
54 |
+
|
55 |
+
# Pass the URL to the template rendering function
|
56 |
+
# Flask's render_template_string automatically handles basic HTML escaping
|
57 |
+
# for security if used directly in text, but here it's okay for the src attribute.
|
58 |
+
return render_template_string(HTML_TEMPLATE, iframe_url=url_from_env)
|
59 |
+
|
60 |
+
if __name__ == '__main__':
|
61 |
+
# Hugging Face Spaces expect the app to run on port 7860
|
62 |
+
# Binding to '0.0.0.0' makes it accessible from outside the container
|
63 |
+
app.run(host='0.0.0.0', port=7860)
|