【问题标题】:How to set JSON return time in python flask如何在python烧瓶中设置JSON返回时间
【发布时间】:2015-02-24 14:42:10
【问题描述】:

在我的烧瓶应用程序中,前端和后端是分开的。如何每天在我设置的特定时间将 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,并将JSON文件中的值复制为相同的date

【问题讨论】:

  • 你到底想要什么?

标签: python json flask sqlalchemy


【解决方案1】:

如果我理解你的问题,最好的做法是做这样的事情。

创建一个字典,并添加一个以空列表为值的项目(dsa)

myDict = {'dsa':[]}

然后在这个列表中,添加后续的字典

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

然后返回json.dumps(myDict)

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2020-08-24
    • 2018-11-02
    • 2018-09-10
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多