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 = """ Iframe Viewer {% if iframe_url %} {% else %}
Error: The 'IFRAME_URL' secret is not set in the Space settings. Please add it under Settings -> Repository secrets.
{% endif %} """ @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)