我正在使用Python上的flask在Web服务器上制作一个闹钟。我希望能够取消警报以及设置其他警报。但是,当尝试使用sched函数时,设置警报时会遇到此错误:
(print_job() missing 1 required positional argument)
@app.route('/scheduler')
def print_job(name):
print(name)
s = sched.scheduler(time.time, time.sleep)
e1 = s.enter(3, 1, print_job, ("Notification #1 ",))
e2 = s.enter(1, 2, print_job, ("Notification #2",))
s.run(blocking=False)
alarm_time = request.args.get("alarm")
if alarm_time:
# convert alarm_time to a delay
delay = time_conversions.hhmm_to_seconds(alarm_time) - time_conversions.hhmm_to_seconds(time_conversions.current_time_hhmm())
print(delay)
#s.enter(int(delay), 1, print, ["Testy test",])
return'<head><meta http-equiv="refresh" content="30;url=\'/\'"></head> \
<form action="/" method="get"> \
<input type="datetime-local" name="alarm"> \
<input type="submit"> \
</form>'
问题是您的print_job
函数需要一个参数(name
),并且没有接收到一个参数。
[According to the flask documentation for urls with variables,您需要向@app.route()
装饰器中添加变量声明:
@app.route('/scheduler/<name>')
def print_job(name):
# ...
但是,由于您的函数似乎未使用name
变量,因此您可以将其删除:
@app.route('/scheduler')
def print_job():
# ...
print_job() missing 1 required positional argument
这是对您的路由的引用,该路由中没有参数,而函数定义需要一个参数。克服此错误所需要做的就是在烧瓶路径中添加一个参数。
@api.route('/scheduler/<name>')
def print_job(name):
print(name)
或者,您只需从函数定义中删除参数,就可以了。编码愉快!