42

I have a simple Flask route that I want to capture a path to a file. If I use <path> in the rule, it works for /get_dir/one but not /get_dir/one/two. How can I capture an arbitrary path, so that path='/one/two/etc will be passed to the view function?

@app.route('/get_dir/<path>')
def get_dir(path):
    return path
davidism
  • 121,510
  • 29
  • 395
  • 339
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187

1 Answers1

88

Use the path converter to capture arbitrary length paths: <path:path> will capture a path and pass it to the path argument. The default converter captures a single string but stops at slashes, which is why your first url matched but the second didn't.

If you also want to match the root directory (a leading slash and empty path), you can add another rule that sets a default value for the path argument.

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_dir(path):
    return path

There are other built-in converters such as int and float, and it's possible to write your own as well for more complex cases.

HelmuthB
  • 471
  • 3
  • 10
moodh
  • 2,661
  • 28
  • 42
  • 5
    For those that is not working, make sure you remove static_url_path, https://github.com/pallets/flask/issues/1633 – FrEaKmAn Feb 08 '18 at 10:53
  • I'm a little spooked that you have to add another rule for the root. Is it simply understood that variable parts cannot be the **empty string**? – Bob Stein Dec 06 '18 at 12:20
  • 1
    @BobStein I'm late to the party, but note that the source for [werkzeug.routing.PathConverter](https://github.com/pallets/werkzeug/blob/master/src/werkzeug/routing.py#L1153) defines the regex for matching paths as `[^/].*?`. The `[^/]` at the beginning means it requires at least one non-slash character, with any other characters (including slashes) optionally occurring after that. I'd expect that behavior, if only because an 'index page' is a pretty common concept and matching the empty string in a URL isn't a behavior I've ever seen (or wanted). – kungphu Jun 11 '19 at 03:46
  • Thanks a lot. 'path' is exactly what I needed. – Sohail Saha May 28 '21 at 09:39
  • 4
    To be clear, the first 'path' in `` is the converter and the second one is the value being passed in. So you would write `@app.route('/')` if named your parameter my_path. – djcunningham0 Oct 11 '21 at 05:29