0

I'm working with flask and handsontable. I'm interested in dynamically creating data and then passing it to a view for display with handsontable.

So far I have :

@app.route('/mv')
def my_view():

    pq = my_object()

    # put all of the data into a list
    for p in pq:
        print p._data
    data = [p._data for p in pq]

    # render the template with our data
    return render_template('my_view.html', page_title = 'nothing yet', data=data)

if __name__ == '__main__':
    app.run()

The print statement generates a dictionary of keys values for each object that looks like:

{ 'su': False, 'delete_at': 1440324275.2,  'id': 360,  'created_at': 1440266675.2, }
{ 'su': False, 'delete_at': 1440324275.202,  'id': 361,  'created_at': 1440266675.202, }
{ 'su': False, 'delete_at': 1440324275.203,  'id': 362,   'created_at': 1440266675.203, }

I want to convert the unix time stamps to human readable date times, before ( or after ? ) passing it to the view. What's the best way to do this?

I've come across Converting unix timestamp string to readable date in Python, which shows a possible way to do this with python. Is it better to do this with python or javascript within the view?

Community
  • 1
  • 1
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    I would suggest on the python side map your timestamps to iso strings using `datetime.fromtimestamp(unixtime).isoformat()` and on the client side you can use Javascript `new Date(isodatestring)`. – doog abides Aug 23 '15 at 00:15
  • 1
    `datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')` should work to convert it to human-readable. – Patrick Allen Aug 23 '15 at 03:45
  • 1
    It can work either way; it is really a design decision, that will be influenced by details in the requirements. If your server-side template can format it, then great because it is less work for mobile devices; if you need to display it in the browser's local time then you will want to format it in JavaScript. – dsh Aug 24 '15 at 21:09
  • Thanks guys, not sure if anyone would like to turn their comments into answers – user1592380 Aug 28 '15 at 00:55

0 Answers0