Welcome to our comprehensive guide on using Blueprint Static Files in Flask! 🎯
In this lesson, we'll walk you through how to organize and serve static files like CSS, JavaScript, and images using Flask Blueprints. By the end of this tutorial, you'll be able to build more sophisticated applications with a better structure.
Static files are the non-changing resources, such as HTML, CSS, JavaScript, images, and fonts, that are necessary for your web application to function correctly but do not change based on user interactions.
Using Blueprints to manage static files makes it easier to keep your project organized and maintainable. Each Blueprint can have its own set of templates, static files, and routes, making it perfect for creating reusable and modular components.
Before diving into Blueprint static files, let's first make sure you have Flask installed. If you haven't done so already, install Flask using pip:
pip install flaskNow, let's create a new Flask project and set up a Blueprint for serving static files.
from flask import Flask, Blueprint
app = Flask(__name__)
static_files_blueprint = Blueprint('static_files', __name__, static_folder='static')
@static_files_blueprint.route('/css/style.css')
def serve_style_css():
return send_from_directory('static', 'style.css')
@static_files_blueprint.route('/js/script.js')
def serve_script_js():
return send_from_directory('static', 'script.js')
app.register_blueprint(static_files_blueprint)In the above code, we've created a new Blueprint called static_files and registered it with our Flask application. We've also defined two routes, one for serving a CSS file and another for a JavaScript file, both located within the static folder.
Let's see this Blueprint in action by creating an HTML template and adding some static files. Create a new folder named templates and a folder named static within the project directory.
Create the following files in the static folder:
/* style.css */
body {
background-color: #f0f8ff;
font-family: Arial, sans-serif;
}// script.js
console.log('Hello, World!');Create an index.html file within the templates folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Static Files Example</title>
<link rel="stylesheet" href="{{ url_for('static_files.serve_style_css') }}">
<script src="{{ url_for('static_files.serve_script_js') }}"></script>
</head>
<body>
<h1>Welcome to our Flask Blueprint Static Files Example!</h1>
</body>
</html>Now, let's update the app.py file to serve our HTML template:
from flask import render_template
@app.route('/')
def home():
return render_template('index.html')Run your application using the following command:
python app.pyNavigate to http://127.0.0.1:5000/ in your web browser, and you should see our static files in action.
Remember to use the url_for function to generate URLs for our static files within the templates. It ensures that the correct path is used even if we decide to change the Blueprint name or the folder location of our static files.
What is the purpose of using Blueprint for serving static files in Flask?
Congratulations! You've now learned how to use Blueprint for serving static files in Flask. As you continue to work with Flask, you'll find that Blueprints are an essential tool for building modular and maintainable web applications. Happy coding! 🚀