[英]How to set JSON return time in python flask
In my flask application, the front-end and back-end are separated. 在我的烧瓶应用程序中,前端和后端是分开的。 How can I return a JSON to front-end everyday at a specific time that I set. 如何每天在设置的特定时间将JSON返回前端。
For example 例如
@api.route('/sda')
def Daily():
# Get all entries (all students from all courses) that match today's date in MMDDYYYY format
entries = Entry.query.filter_by(timestamp.strftime('%M%D%Y')=datetime.datetime.now().strftime('%M%D%Y')).all()
# Count how many students attend school today (Since in the entries array, there are duplicate student with same ID, and I only want to count it once per student)
# When the loop ends, it will return a jsonify with today's date and total number of students attend school today
Expected data in JSON file: JSON文件中的预期数据:
{
"dsa": [
{
"date": "12252014",
"present": 470,
"absent": 30
},
{
"date": "12262014",
"present": 490,
"absent": 10
},
{
"date": "12272014",
"present": 400,
"absent": 100
}
}
Since I realize that whenever front-end access the route /sda
the function will return jsonify every time, and duplicate the value in JSON file with same date
. 由于我意识到只要前端访问路由/sda
该函数就会每次返回jsonify,并在相同date
JSON文件中复制该值。
If I understand what you're asking, the best course of action would be to do something like this. 如果我理解您的要求,那么最好的做法就是做这样的事情。
Create a dictionary, and add an item (dsa) with an empty list as the value 创建一个字典,并添加一个带有空列表的项目(dsa)作为值
myDict = {'dsa':[]}
Then in this list, add subsequent dictionaries 然后在此列表中,添加后续词典
myDict['dsa'].append({"date":myDate, "present":present,"absent":absent})
Then just return json.dumps(myDict)
然后只需返回json.dumps(myDict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.