Skip to main content

flask-staticdirs

The built-in Flask functionality for serving static files from a directory does not serve index files. flask-staticdirs implements a Flask blueprint that does.

Features:

  • Redirects paths matching exsting directories so they end with '/'
  • Serves the first index file found in the path under a static folder
  • Serves paths matching exsiting files in a static folder
  • Allows conflicting routes to be superseded with an existing file or directory.

Usage

Instead of using Flask's static_folder, and static_url_path parameters, like this:

from flask import Flask

app = Flask(__name__, static_folder="public", static_url_path="/")
if __name__ == '__main__':
    app.run(host="localhost", port=3000, debug=True)

Use the blueprint returned from flask_staticdirs.staticdirs():

from flask import Flask
from flask_staticdirs import staticdirs

app = Flask(__name__)
app.register_blueprint(staticdirs("public"), url_prefix="/")
if __name__ == '__main__':
    app.run(host="localhost", port=3000, debug=True)

Note: url_prefix="/" is unnecessary since "/" is the default value, but the example matches static_url_path for clarity.

Superseding conflicting routes

Flask matches routes to incoming paths based on an algorithm that sorts routes. Because flask_staticdirs route is '/<path:path', just about any route will take precedence.

This is not normally a problem unless you want to mix programatic responses with the paths that serve static files.

For example, if you would like to serve static files before files found in a database

app.register_blueprint(staticdirs("public", files=[ "docs/*" ]))
@app.route("/docs/<doc>")
def mysubdir_route(doc):
    return doc_from_db(doc) or f'<p>Could not find {doc}.</p>'

I realize this is not really a common use case 😊, but it does illustrate the feature.

Serving other index files

If you want to serve indexes for directories with files other than 'index.html' you can list them in the index parameter:

app.register_blueprint(staticdirs("public", index=[ "index.html", "index.htm" ]))

API

staticdirs(static_folder='static', index='index.html', files=[])

Creates a Flask blueprint to serve files and directories (with support for index files) from at static folder.

  • param static_folder: The folder to serve files from. Defaults to 'static'.
  • param index: File or list of files (first found) to serve for a directory. Defaults to 'index.html'.
  • param files: An array of filename globs. Files and directories that match will be routed specifically to avoid conflict with other routes.
  • returns: A flask blueprint. Use with app.register_blueprint()

Release files for flask-staticdirs 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-staticdirs 1.0.1
File Size Uploaded
flask-staticdirs-1.0.1.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-staticdirs 1.0.1
File Interpreter ABI Platform
flask_staticdirs-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / flask-staticdirs-1.0.1.tar.gz

Download URL flask-staticdirs-1.0.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
956d61701fc50191240cef6ede167f5c1c35071f78578d2325537e52f2c4c6b9
BLAKE2b-256 checksum
How to use checksums
67f103f888975245cffef5706a4a9ab689c460ce0b9c34e506f02199c2bdaeed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.6

Release files / flask_staticdirs-1.0.1-py3-none-any.whl

Download URL flask_staticdirs-1.0.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
971042225cf37f3f66bd4bfe1a1f9d05e355b79e6c12ad81c98e6fef365df649
BLAKE2b-256 checksum
How to use checksums
d09ba20fb7207b9440e0947df09fba172ab3d9f162049dfaf60694857f88b457
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page