Skip to main content

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

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-staticdirs-1.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

flask_staticdirs-1.0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file flask-staticdirs-1.0.1.tar.gz.

File metadata

  • Download URL: flask-staticdirs-1.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for flask-staticdirs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 956d61701fc50191240cef6ede167f5c1c35071f78578d2325537e52f2c4c6b9
MD5 622752bced4292508af896ddbee34ec8
BLAKE2b-256 67f103f888975245cffef5706a4a9ab689c460ce0b9c34e506f02199c2bdaeed

See more details on using hashes here.

File details

Details for the file flask_staticdirs-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_staticdirs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 971042225cf37f3f66bd4bfe1a1f9d05e355b79e6c12ad81c98e6fef365df649
MD5 a51673896fe3b22cba522f75816c4eff
BLAKE2b-256 d09ba20fb7207b9440e0947df09fba172ab3d9f162049dfaf60694857f88b457

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page