Search code examples
pythonhttpurlflaskstatic-files

Flask : define a route to explore files and folders like SimpleHTTPSever does


I have done a simple Flask application which goal is to generate some output XML files.

I have all my applicative routes properly defined, ajax calls and so forth. The application works fine and I'm happy with it.

What I am missing is a way to easily serve and browse the result files and folders. This is what SimpleHTTPServer would do when run against the folder where the results files are stored.

I would like to achieve something similar via Flask (not to have a dedicated web server running on another port just for this mere purpose).

I know that Flask is based on BaseHTTPServer just like SimpleHTTPServer is, and I'm afraid that the file system browsing capability is part of the SimpleHTTPServer layer.

Is it possible ? If not natively supported, is there a Flask plugin which could help ?


Solution

  • Flask AutoIndex does exactly what you're looking for:

    import os.path
    from flask import Flask
    from flask.ext.autoindex import AutoIndex
    
    app = Flask(__name__)
    AutoIndex(app, browse_root=os.path.curdir)
    
    if __name__ == '__main__':
        app.run()