Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Flask-HTTPAuth when serving a static folder

I’m using Flask to serve a static folder:

from flask import Flask, send_from_directory
from flask_httpauth import HTTPBasicAuth

app = Flask(__name__,
            static_url_path='',
            static_folder='html_files')
...

@app.route('/')
@auth.login_required
def send_html_files():
    return send_from_directory('html_files', 'main.html')

I used the first example in Flask-HTTPAuth docs in order to add basic authentication to my website. Just a regular username and password is enough for me.

The problem is that the authentication dialog is not showing when the user go directly to http://localhost:5000/a/b/c (it works on http://localhost:5000/)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is the proper way of doing this? On the other hand, what is the quick and dirty way?

>Solution :

@app.route('/') matches your root path only.

Try something like this to match every path:

@app.route('/<path:filename>')
@auth.login_required
def send_html_files(filename):  
    return send_from_directory('html_files', filename)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading