Search code examples
fileflaskunicodepythonanywhere

Flask in PythonAnywhere doesn't find file name with spanish characters


I'm trying to build a website that has one page for downloading some resources, and it just happened that my local flask version finds perfectly any file name (when using send_from_directory()), but once deployed at PythonAnywhere it just doesn't work for the filenames that have spanish accented characters such as á.

I guess it has something to do with unicode, but I couldn't find how to fix it (the logs at pythonanywhere don't seem to show anything, since flask simply delivers a "Not found" page to the user).

...and I'd really like to have those accents in the name of the files people download (they're anki decks, some of them intended for education purposes, and it just feels wrong to deliver bad ortography in the deck name).

My code looks like this:

@app.route('/anki/d/<file>')
def d_anki(file):
   if file == "verbscat":
       ankideck = "[Rusca] Temps Verbals Catalans.apkg"
   elif file == "irregular":
       ankideck = "[Rusca] Verbs Irregulars Anglès.apkg"
   # ...
   else:
       return f"The file {file} wasn't found."
   return send_from_directory("./static/anki/", ankideck, as_attachment=True, cache_timeout=0)

(then I link to this url in a button by <a href="/anki/d/irregular" ...>)


Solution

  • Oh I just realized I can choose a different name for the downloaded file by adding attachment_filename="Whatever I want to call it" to the parameters in send_from_directory.

    So I guess we can do with this workaround (having the original files with simple non-accented names and adding the proper name afterwards).

    if file == "irregular":
        ankideck = "irregular.apkg"
        name = "[Rusca] Verbs Irregulars Anglès.apkg"
    # ...
    return send_from_directory("./static/anki/", ankideck, as_attachment=True, attachment_filename=name cache_timeout=0)