Python Flask - GUI for client

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

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).

回答1:

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/



回答2:

You cannot use tkinter in a web app to run code client-side. It is simply not possible.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!