In Flask, a virtual directory refers to mapping certain URL paths to specific folders or directories. This can be achieved using Flask’s send_from_directory
method, which allows the app to serve files from specific directories, not just the default static directory.
Code Explanation
In the example code provided, we define two virtual directory mappings:
- The
pictures
directory is mapped to the URL path/pictures/<path:filename>
. - The
source_images
directory is mapped to the URL path/source_images/<path:filename>
.
At the same time, we set the Flask application’s root path, static file path, and template path.
Code Details:
Flask(__name__, root_path='/absolute/path/to', ...)
sets the root path of the Flask app.static_folder='/absolute/path/to/static'
specifies the static file directory. By default, Flask serves static files from a folder namedstatic
located at the root of the project.template_folder='/absolute/path/to/templates'
specifies the template directory. By default, Flask looks for HTML templates in a folder namedtemplates
in the root of the project.
View Functions:
custom_static_pictures(filename)
handles requests to/pictures/<filename>
, serving files from thepictures
directory.custom_static_source_images(filename)
handles requests to/source_images/<filename>
, serving files from thesource_images
directory.
Flask Application Directory Structure Example:
Assuming the project root is /absolute/path/to
, the directory structure may look like this:
/absolute/path/to/
├── app.py # Flask application main file
├── static/ # Default static files directory
│ ├── css/
│ ├── js/
│ └── images/
├── templates/ # Default templates directory
│ ├── index.html
│ └── layout.html
├── pictures/ # Custom pictures directory
│ ├── img1.jpg
│ ├── img2.png
└── source_images/ # Custom source images directory
├── imgA.jpg
├── imgB.png
or
/absolute/path/to/
├── static/ # 静态文件目录
│ ├── css/
│ ├── js/
│ └── images/
├── templates/ # 模板文件目录
│ ├── index.html
│ └── layout.html
├── pictures/ # 自定义图片目录
│ ├── img1.jpg
│ ├── img2.png
└── source_images/ # 自定义源图片目录
├── imgA.jpg
├── imgB.png
/absolute_2/path/to/
├── flask_app/ # Flask 应用所在的子目录
│ └── app.py # Flask 应用文件
Access Paths:
/pictures/img1.jpg
will fetch the file from/absolute/path/to/pictures/img1.jpg
./source_images/imgA.jpg
will fetch the file from/absolute/path/to/source_images/imgA.jpg
.
Virtual Directory Configuration Purpose:
- The custom
pictures
andsource_images
virtual directories allow you to serve files independently from the defaultstatic
directory, making it more flexible for managing file paths. - The
send_from_directory
method allows Flask to dynamically serve files from specified directories based on the URL, rather than just serving content from the static folder.
Directory Structure Diagram:
/absolute/path/to/
├── app.py
├── static/ # Static files directory
│ ├── css/
│ ├── js/
│ └── images/
├── templates/ # Templates directory
│ ├── index.html
│ └── layout.html
├── pictures/ # Custom pictures directory
│ ├── img1.jpg
│ ├── img2.png
└── source_images/ # Custom source images directory
├── imgA.jpg
├── imgB.png
Summary:
By setting up virtual directories, Flask becomes more flexible in managing and serving static resources from different directories, not just the default static
directory. This approach is useful in more complex resource management scenarios, especially when you need to organize static resources by functionality or type.
Full Code:
from flask import Flask, send_from_directory
# Specify the root path, static folder, and template folder for the Flask app
app = Flask(__name__,
root_path='/absolute/path/to', # Root path
static_folder='/absolute/path/to/static', # Static files directory
template_folder='/absolute/path/to/templates' # Templates directory
)
# Custom image directory mapping for "pictures"
@app.route("/pictures/<path:filename>")
def custom_static_pictures(filename):
return send_from_directory('pictures', filename)
# Add another custom image directory mapping for "source_images"
@app.route("/source_images/<path:filename>")
def custom_static_source_images(filename):
return send_from_directory('source_images', filename)
@app.route('/')
def home():
return 'Hello, Flask!'
if __name__ == '__main__':
app.run(debug=True)