📖 Scenario: You are building a simple Flask web app that needs to serve a JavaScript file to add interactivity on the client side.Think of it like setting up a small shop where you want to display a sign (JavaScript) outside the window (web page) so visitors can see it and interact.
🎯 Goal: Create a Flask app that serves a JavaScript file correctly so the browser can load and run it.
📋 What You'll Learn
Create a Flask app instance named app
Create a route / that returns an HTML page referencing a JavaScript file
Place the JavaScript file in the correct static folder
Serve the JavaScript file using Flask's static file serving
💡 Why This Matters
🌍 Real World
Web developers often need to serve JavaScript files to add interactivity to their web pages. Flask's static folder makes this easy and organized.
💼 Career
Knowing how to serve static files like JavaScript and CSS is essential for backend developers working with Flask or similar web frameworks.
Progress0 / 4 steps
1
Create the Flask app and HTML route
Create a Flask app instance called app and add a route for / that returns a simple HTML string with a <script> tag referencing /static/script.js.
Flask
Hint
Use Flask(__name__) to create the app. Use @app.route('/') to define the home page. Return an HTML string with a <script> tag linking to /static/script.js.
2
Create the JavaScript file in the static folder
Create a folder named static in your project root. Inside it, create a file named script.js with a single line: console.log('JavaScript loaded');
Flask
Hint
The static folder is where Flask looks for static files like JavaScript. Create script.js inside it with the exact console.log line.
3
Run the Flask app with debug mode
Add the code to run the Flask app with debug=True when the script is executed directly.
Flask
Hint
Use the standard Python check if __name__ == '__main__': and call app.run(debug=True) inside it.
4
Verify JavaScript is served correctly
Add a comment in the Python file explaining that Flask automatically serves files from the static folder at the /static/ URL path.
Flask
Hint
Just add a comment line explaining how Flask serves static files from the static folder.
Practice
(1/5)
1. In Flask, where should you place your JavaScript files so they can be served correctly to the browser?
easy
A. Inside the static folder
B. Inside the templates folder
C. In the root project directory
D. Inside the instance folder
Solution
Step 1: Understand Flask static file serving
Flask automatically serves files placed in the static folder to the browser.
Step 2: Identify correct folder for JavaScript files
JavaScript files must be inside the static folder to be accessible via URL.
Final Answer:
Inside the static folder -> Option A
Quick Check:
Static folder = JavaScript files served [OK]
Hint: Always put JS files in the static folder for Flask to serve them [OK]
Common Mistakes:
Placing JS files in templates folder
Putting JS files in root directory
Expecting instance folder to serve static files
2. Which of the following is the correct way to link a JavaScript file named app.js located in the static folder inside your Flask HTML template?
easy
A. <script src="/templates/app.js"></script>
B. <script src="/static/app.js"></script>
C. <script href="/static/app.js"></script>
D. <link src="/static/app.js">
Solution
Step 1: Use correct HTML tag for JavaScript
The <script> tag with the src attribute is used to link JS files.
Step 2: Use correct path to static folder
Flask serves static files under the /static/ URL prefix, so path should be /static/app.js.
Final Answer:
<script src="/static/app.js"></script> -> Option B
Quick Check:
Script tag + /static path = correct link [OK]
Hint: Use
Common Mistakes:
Using <link> instead of <script>
Using href instead of src in <script>
Linking to /templates folder
3. Given this Flask route and folder structure, what will happen when the browser requests /static/script.js?
But the browser shows the JavaScript code as plain text instead of running it. What is the problem?
medium
A. The response is missing the correct Content-Type header for JavaScript
B. The file path 'static/app.js' is incorrect
C. You cannot serve JavaScript files manually in Flask
D. The route path should be '/static/app.js' instead of '/js/app.js'
Solution
Step 1: Check how Flask serves static files
Flask sets the Content-Type header to 'application/javascript' automatically for static files.
Step 2: Identify missing Content-Type in manual response
Returning raw file content without setting Content-Type causes browser to treat it as plain text.
Final Answer:
The response is missing the correct Content-Type header for JavaScript -> Option A
Quick Check:
Set Content-Type to application/javascript for JS files [OK]
Hint: Always set Content-Type header when serving JS manually [OK]
Common Mistakes:
Not setting Content-Type header
Assuming Flask auto-sets headers in manual routes
Using wrong file path without checking
5. You want to serve multiple JavaScript files from a custom folder named assets/js instead of the default static folder. Which Flask code snippet correctly serves these files at URL path /js/<filename>?
hard
A. @app.route('/js/<filename>')
def serve_js(filename):
return open(f'assets/js/{filename}').read()
B. from flask import send_file
@app.route('/js/<filename>')
def serve_js(filename):
return send_file(f'assets/js/{filename}')
C. from flask import send_from_directory
@app.route('/js/<filename>')
def serve_js(filename):
return send_from_directory('assets/js', filename)
D. @app.route('/js/<filename>')
def serve_js(filename):
return redirect(f'/static/{filename}')
Solution
Step 1: Use send_from_directory to serve files from custom folder
Flask's send_from_directory safely serves files from any folder with correct headers.
Step 2: Map URL path to custom folder
The route /js/<filename> calls send_from_directory('assets/js', filename) to serve files from assets/js.
Final Answer:
Use send_from_directory with assets/js folder -> Option C