Bird
0
0

You want to serve a static HTML file named index.html from your Flask app on Raspberry Pi when users visit /. Which code snippet correctly does this?

hard🚀 Application Q9 of 15
Raspberry Pi - Web Server and API
You want to serve a static HTML file named index.html from your Flask app on Raspberry Pi when users visit /. Which code snippet correctly does this?
Afrom flask import Flask, send_from_directory app = Flask(__name__) @app.route('/') def home(): return send_from_directory('static', 'index.html')
Bfrom flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'index.html'
Cfrom flask import Flask app = Flask(__name__) @app.route('/') def home(): return open('static/index.html').read()
Dfrom flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html')
Step-by-Step Solution
Solution:
  1. Step 1: Understand Flask static file serving

    Flask uses the templates folder and render_template() to serve HTML files properly.
  2. Step 2: Identify correct method

    Using render_template('index.html') looks for index.html in the templates folder and returns it as HTML response.
  3. Final Answer:

    Use render_template('index.html') to serve HTML files -> Option D
  4. Quick Check:

    Serve HTML with render_template() [OK]
Quick Trick: Put HTML in templates folder and use render_template() [OK]
Common Mistakes:
MISTAKES
  • Reading file manually and returning string
  • Returning filename string instead of content
  • Using send_from_directory for templates folder

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes