Flask-AutoIndex
Flask-AutoIndex is a Flask extension that automatically generates index pages for Flask applications, mimicking Apache's mod_autoindex. It provides a default visual style for directory listings but also allows for extensive customization of templates and icons. The current stable version is 0.6.6, released in March 2020. While functional, the project appears to be in maintenance mode, with limited recent activity on its GitHub repository.
Common errors
-
ModuleNotFoundError: No module named 'flask.ext'
cause Attempting to import Flask-AutoIndex using the old `flask.ext` namespace, which was removed in modern Flask versions (0.11+).fixChange the import statement to `from flask_autoindex import AutoIndex`. -
AssertionError: View function mapping is overwriting an existing endpoint function: autoindex
cause This error occurs when Flask-AutoIndex's automatic URL rule creation conflicts with a manually defined Flask route that uses the same endpoint name (often 'autoindex' by default) or URL path.fixWhen initializing `AutoIndex`, pass `add_url_rules=False` and then define your routes for `AutoIndex` manually using `@app.route` decorators and calling `idx.render_autoindex(path)` in the view function, as shown in the quickstart. -
Flask-AutoIndex is serving files from '/' (the root of my application) instead of my custom home page.
cause By default, if `AutoIndex` is initialized without specifying `add_url_rules=False` and `browse_root` points to a high-level directory (like `os.path.curdir`), it can take over the root URL (`/`).fixInitialize `AutoIndex` with `add_url_rules=False` and explicitly define your `AutoIndex` routes for a specific path (e.g., `/files`), allowing your root path (`/`) to be handled by a separate Flask view function.
Warnings
- breaking Versions of Flask-AutoIndex prior to 0.6.4 supported Python 2. Versions 0.6.4 and newer require Python 3.6 or higher. Attempting to use newer versions on Python 2 will result in syntax errors or import issues.
- gotcha By default, if `add_url_rules` is not explicitly set to `False` during initialization, Flask-AutoIndex may automatically register routes like `/` or `/some_path` which could conflict with your application's custom routes, leading to `AssertionError: View function mapping is overwriting an existing endpoint function`.
- gotcha The `flask.ext` import pattern (e.g., `from flask.ext.autoindex import AutoIndex`) is an outdated Flask mechanism. In modern Flask (0.11+), extensions are imported directly from their package name.
- gotcha The project shows limited recent development activity, with the last PyPI release in March 2020 and last GitHub commit in 2024 (but main functionality largely unchanged since 2020). While it remains functional, it may not receive updates for new Flask features or critical bug fixes promptly.
Install
-
pip install Flask-AutoIndex
Imports
- AutoIndex
from flask.ext.autoindex import AutoIndex
from flask_autoindex import AutoIndex
Quickstart
import os
from flask import Flask
from flask_autoindex import AutoIndex
app = Flask(__name__)
# Create a dummy directory and file for demonstration
demo_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'my_files')
os.makedirs(demo_dir, exist_ok=True)
with open(os.path.join(demo_dir, 'example.txt'), 'w') as f:
f.write('This is an example file managed by Flask-AutoIndex.')
# Initialize AutoIndex to serve 'my_files' directory
# We set add_url_rules=False to manually define the route, preventing conflicts.
idx = AutoIndex(app, browse_root=demo_dir, add_url_rules=False)
@app.route('/')
def home():
return "<h1>Welcome!</h1><p>Visit <a href='/files'>/files</a> for the auto-indexed directory.</p>"
# Manually define routes for AutoIndex to serve content
@app.route('/files')
@app.route('/files/<path:path>')
def autoindex(path='.'):
# Call render_autoindex with the path to display the directory content
return idx.render_autoindex(path)
if __name__ == '__main__':
app.run(debug=True)