0

I am using python-docx to save a word file and I have set a specific path but since path aren't the same for every user I would like to have a dialogue when the user clicks on download so he can choose exactly in which local repository to save the file.

What I have now:

@app.route('/download_file')
def save_doc():
    filename = 'test.docx'
    filepath = os.path.join(r'C:\Users\Joe\Desktop', filename)
    document.save(filepath)
    return 'meh'
accdias
  • 5,160
  • 3
  • 19
  • 31
Philx94
  • 1,036
  • 3
  • 20
  • 39

1 Answers1

0

Implementing the logic you described requires work on the front-end. Let's simplify the problem by assuming that the user manually types in the download target directory. (In practice, it would make more sense for there to be a pop-up window allowing the user to browse the directory on a file explorer.)

<form action="/download" method="post">
    <input type="text" value="" name="dir">
    <input type="submit" value="Download">
</form>

Then, in Flask, you might specify the following:

from flask import Flask, request

@app.route('/download', methods=['GET', 'POST'])
def save_doc():
    if request.method=='POST':
        download_dir = request.form['dir'] # Access user input from backend
        filename = 'test.docx'
        filepath = os.path.join(download_dir, filename)
        document.save(filepath)
        return 'Download complete'
    else:
        pass # Whatever implementation you want for 'GET' method

This is incomplete code that I wrote without knowledge of the structure of your project, and so may not work if it is simply copied and pasted into your source code. The implementation is also quite basic, so consider it a baseline model to help you get started on baking the user interactive dialogue system.

I'd be happy to answer any questions you might have.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
  • Thank you I'll look into this. So I understand that there is no such thing as triggering the user's OS 'path finder' ? – Philx94 Feb 07 '20 at 02:58
  • @Philx94 I think [this](https://stackoverflow.com/questions/35260416/folder-choose-in-html/35260979) might be what you are looking for. Does this answer your question? – Jake Tae Feb 07 '20 at 03:04