Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda