Reputation: 25341
I want to run a client-side file dialog GUI so that the user can choose a file to process with python (example). My code, which fundamentally works fine, is here:
from flask import Flask, url_for, request
app = Flask(__name__)
@app.route('/data')
def gui_test():
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
filepath = tkFileDialog.askopenfilename()
with open(filepath,'rb') as tt:
lines = tt.readlines()
return 'You are reading ' + filepath + '<p>Top 10 lines for proof of concept<p>'+'<br>'.join(lines[0:10])
if __name__ == '__main__':
app.run()
Everything works fine, except that the GUI opens on the machine running this REST code, and not on in the client's user account.
The user should go with the browser to http://127.0.0.1:5000/data and then should see the file dialog:
Details: Windows Server 2012 R2, Flask 0.10.1, Python 2.7
If I'm taking the wrong approach, I'd appreciate suggestions for other directions. There may be something flawed in the idea, because of browsers not sharing the full path (javascript example).
Upvotes: 2
Views: 3379
Reputation: 1686
You can't send GUI widgets to browser, only HTML, CSS and Javascript. See official documentation for file uploads in flask http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
Upvotes: 4
Reputation: 386210
You cannot use tkinter in a web app to run code client-side. It is simply not possible.
Upvotes: 1