簡體   English   中英

如何在python flask中設置JSON返回時間

[英]How to set JSON return time in python flask

在我的燒瓶應用程序中,前端和后端是分開的。 如何每天在設置的特定時間將JSON返回前端。

例如

@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

JSON文件中的預期數據:

{
  "dsa": [
    {
      "date": "12252014", 
      "present": 470,
      "absent": 30
    }, 
    {
      "date": "12262014", 
      "present": 490,
      "absent": 10
    }, 
    {
      "date": "12272014", 
      "present": 400,
      "absent": 100
    }
}

由於我意識到只要前端訪問路由/sda該函數就會每次返回jsonify,並在相同date JSON文件中復制該值。

如果我理解您的要求,那么最好的做法就是做這樣的事情。

創建一個字典,並添加一個帶有空列表的項目(dsa)作為值

myDict = {'dsa':[]}

然后在此列表中,添加后續詞典

myDict['dsa'].append({"date":myDate, "present":present,"absent":absent})

然后只需返回json.dumps(myDict)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM