-1

I am just trying out Flask. I am trying to serve a different folder other than the one where the flask server started. This is what I have come up with.

alt_foler = '/folder/withindex/'
app = Flask('test')

@app.route('/')
@app.route('/<path:path>')
def redirect(path):
     return alt_folder + path

But when I give 127.0.0.1/index.html, I just see

'/folder/withindex/index.html' instead of actually serving the index.html file located at the given location. How to set this correctly?
user1429322
  • 1,266
  • 2
  • 24
  • 38

2 Answers2

1

While you should use an HTTP server like Nginx or Apache to serve static files, if you insist on using Flask, you can use send_from_directory.

@app.route('/<path:path>')
def redirect(path):
    return send_from_directory(alt_folder, path)
dirn
  • 19,454
  • 5
  • 69
  • 74
0

I figured this out.

Actually the redirect function should return the contents of the file instead of the file.

def redirect(path):
    return open(alt_folder + path, 'r').read()
user1429322
  • 1,266
  • 2
  • 24
  • 38