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.
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:
{
"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
.
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
myDict = {'dsa':[]}
Then in this list, add subsequent dictionaries
myDict['dsa'].append({"date":myDate, "present":present,"absent":absent})
Then just return json.dumps(myDict)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.