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

Uploaded Source

Built Distribution

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

Uploaded Python 3

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