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

866
Visualizações
Plotly Graphs - Conflict between Javascript and Jinja

I've tried plotting graphs in my Flask web app using Plotly which didn't work for some reason so I started to simplify the issue to find the error.

It seems like there is an issue with the Jinja Syntax {{ myJSONfile | safe }} in javascript.

Aslong as I pass an empty string "" to the variable the graph renders but obviously without datapoints.

(Inspect Element Console gives an Unexpected token '{' Error referring to the first opening bracket of my Jinja variable)

According to this post I should have written the the syntax inside the javascript block correctly in order to pass a JSON-File to the javascript variable and I am out of ideas right now.

I'd appreciate if somebody has further ideas and can help me out here :)

Code example:

   {% extends "layout.html" %} {% block content %}

<div class=" row p-4 ">
    <div class="card m-auto " style="width: 90%; " data-aos="fade-left ">
        <div class="card-body ">
            <div id="chart1 "></div>
        </div>
    </div>
</div>


<script src="https://cdn.plot.ly/plotly-latest.min.js "></script>
<script>
    var graphs1 = {
        {
            graph1JSON | safe
        }
    };
    Plotly.plot("chart1 ", graphs1, {});
</script>

I've since tested out another test code from a tutorial github repository to eliminate the possibility that it may have been some typing errors in my code I wasn't able to identify. But those copy paste examples gave me the same issue.

I've tried different things suggested in some posts I've found here on Stackoverflow

  • checked again if my data was saved to the JSON variable in app.py correctly (which it does)
  • using quotes like: var graph1 = '{{ graph1JSON | safe }}'
  • using block code syntax: var graph1 = {%block code %} {{ graph1JSON | safe }} {%endblock code %}
  • Several combinations of above syntax

During debug I clearly see that the JSON file is created successfully and holds the desired data, Flask/Jinja just doesnt want to communicate with Javascript

Here's an additional Screenshot of how the syntax highlighting looks in my VS Code. (Other than in this particular case my Flask app is running fine, being able to render pages dynamically etc.)

enter image description here

(Syntax not recognized in the javascript part)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Please do not pull apart the jinja instruction. Jinja looks for double curly brackets in the template, which follow one another directly with no whitespace in between.

var graphs1 = {{ graph1JSON | safe }};
Plotly.plot('chart1', graphs1, {});
over 4 years ago · Santiago Trujillo Relatório

0

The problem here lies with VSCode, especially with Syntax Highlighting and Formatting Extensions. There is some conflict between some of your enabled extensions which - on save - format the javascript code in above way.

  1. Disable all enabled formatting extensions in VSCode
  2. I only activated the following:
  • Python
  • Python Extension Pack by Jayamanne
  • Better Jinja by Samuel Colvin

This way on save the syntax doesn't get shredded anymore making the JavaScript recognize your JSON-File correctly.

over 4 years ago · Santiago Trujillo 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