用按钮调用python函数
问题内容:
我希望能够单击html中的按钮并调用python函数。我已经尝试过了,但它仅适用于文本。而且我在这里看到可以在按钮中使用函数名称,但是它不起作用,我也不知道为什么:/
而且我不希望在单击按钮后转到另一页,我希望停留在同一页上并只执行该函数中的代码。
我的py文件:
from flask import Flask
from flask import render_template
import tkinter as tk
from tkinter import filedialog
import sys
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('hello.html')
@app.route('/upload/')
def uploaduj():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
return file_path
我的html文件:
<!doctype html>
<title>Flaskr</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Flaskr</h1>
<div class=metanav>
<button action="/upload/">Klik</button>
</div>
我对python和flask真的很陌生,因此感谢您的帮助。
编辑: 我现在知道tkinter将无法在Web浏览器中工作
问题答案:
您需要一个HTML文件输入对话框。
<form action="/upload">
<input type="file" name="fileupload" value="fileupload" id="fileupload">
<label for="fileupload"> Select a file to upload</label>
<input type="submit" value="Klik">
</form>
如何在Flask中处理它,在文档中