Spaces:
Running
Running
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> | |
""" | |
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) |