我正在使用Flask开发Web应用程序。我在py中编写了函数loginGmail,并尝试通过html中按钮的onclick事件对其进行调用。但是,该功能会在网站加载后自动调用。
main.py
def loginGmail():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
loginGmail()通过以下位置传递给home.html:
return render_template('home.html', things_to_sort=things_to_sort, form=form, loginGmailFunc=loginGmail)
在html一侧:
<button id="login" class="btn btn-outline-success my-2 my-sm-0" type="button" value="clickme" onclick="() => this.{{loginGmailFunc()}}">Login</button>