0

I'm trying to implement a Flask view that renders a file from a directory files at the root. Ideally, the path would show up in the URL. However, files contains both files and directories. As different files are stored in different levels of the files hierarchy, I'm stuck.

Is there a way to implement a controller in Flask such that a PATH is accepted as an argument instead of explicit strings? I can sense this is probably impossible, but I'm not quite sure how else to proceed.

For example, I want something like

@app.route('/files/<filepath>')
def render_file(filepath):
    f = open('files/' + filepath, 'r')
    (...)

where files has some organization like this:

files
|________file1.txt
|________filedirL1
          |________filedirL2
                   |_________file32.txt   

But the route would not only recognize top-level files with routes like GET /files/file1.txt but also something like GET /files/filedirL1/filedirL2/file32.txt

Anonymous
  • 411
  • 1
  • 4
  • 14

1 Answers1

0

Just found that you can use the path converter, which works fine:

@app.route('/files/<path:path>')
def render_file(path):
    (...)
Anonymous
  • 411
  • 1
  • 4
  • 14