Flask-AutoIndex

0.6.6 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart initializes a Flask application and configures Flask-AutoIndex to serve files from a 'my_files' directory at the `/files` URL path. It explicitly sets `add_url_rules=False` to allow for manual routing, preventing potential conflicts with other Flask routes. Run the script and navigate to `http://127.0.0.1:5000/files` to see the auto-indexed content.

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)

view raw JSON →