Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
python flask datetime object to vis.js array

I'm building a Flask app that will display a timeline. I've decided to use vis.js since it's flexible. I render my template HTML page and call an API endpoint using AJAX GET to retrieve the JSON object of items for the timeline. I want to render this JSON object in the timeline but it just doesn't want to render the chart.

Any help will be appreciated.

My seq of events

  1. Flask App renders HTML Template
  2. AJAX Call to API endpoint
  3. API endpoint has a function that compiles a list of dict's and returns it as json.dumps(_list)
  4. AJAX response function assigns it to vis.js data input

JSON Object returned in AJAX call:

[{"id": 1, "content": "My label 1", "start": "2022-05-26T14:18:27Z"}, 
{"id": 2, "content": "My label 2", "start": "2022-05-26T12:51:20Z"}, 
{"id": 3, "content": "My label 3", "start": "2022-05-26T12:51:05Z"}]

API Endpoint compiling function:

i = 0
_cursor = db.collection.find({}).sort([("timestamp", pymongo.DESCENDING)]) #timestamp is millisec epoch unix time
for item in _cursor:
   _temp_dict['id'] = int(i)
   _temp_dict['content'] = str(item['name'])
   _temp_dict['start'] = str(datetime.fromtimestamp(int(item['timestamp'])/1000).strftime('%Y-%m-%dT%H:%M:%SZ'))
   _temp_list.append(_temp_dict)
   _temp_dict = {}
return json.dumps(_temp_list)

My AJAX call and Timeline build:

$(document).ready(function() {
        $.fn.dataTable.ext.errMode = 'none';
        $.ajax({
          type: 'GET',
          url : '/api/datamine/timeline',
          dataType: "json",
          success:function(data){
            console.log("Data: " + data + "\nStatus: " + status);
            var result = JSON.stringify(data);
            var container = document.getElementById('visualization');
            var items = new vis.DataSet(result);
            var options = {};
            var timeline = new vis.Timeline(container, items, options);
          }
      });

The timeline will not build. My console.log where I print the JSON object looks like this.

Data: [object Object],[object Object],[object Object],[object Object],[object Object]
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!