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

263
Visualizações
How to access Python dictionary in Javascript Script in Django

I am working in Django, where I am sending a Python Dictionary into an HTML file.

In that HTML File, I want to access the Python dictionary and append the keys and values of dictionary into the Javascript arrays. I am able to access Python dictionary in HTML, but can't do the same in Javascript.

This is how I am able to access the values of Dictionary in HTML,

{% for key, image in predictedLabel.items %}
      <tr>
        <th scope="row">{{ key }}</th>
        <td>{{ image }}</td>
      </tr>
  {%endfor%}

The Javascript script that I have is of Pie Chart, which is as given below,

      <script>
        var xArray = [];
        var yArray = []; 
        
        var xArray = ["Italy", "France", "Spain", "USA", "Argentina"];
        var yArray = [55, 49, 44, 24, 15];

        var layout = { title: "Distribution of Labels" };

        var data = [{ labels: xArray, values: yArray, hole: .5, type: "pie" }];

        Plotly.newPlot("myPlot", data, layout);
      </script>

I want to put the Python Dictionary Key into the xArray and the Values of Dictionary into the yArray. For that, I need to access the Python Dictionary in the Javascript script, but I do not know how to access the Python values in the Javascript script. Can anyone help me in this regard?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can pass the dictionary in JSON form to the client. This is commonly done like this:

import json

# ... send to client
json = json.dumps(dict_name)

Then in your template:

<script type="application/json">{{ json }}</script>

Then in your JavaScript you can simply do:

const data = JSON.parse(document.querySelector('script[type="application/json"]'));
// do something with 'data'
about 4 years ago · Juan Pablo Isaza Relatório
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