0

i work on program that can make auto report, i want to show a list on html table but i got stuck how to make it work like i want, im using flask as framework here. i have a python code like this:

a = 5
b = 2
result = []
subtraction = a - b
result.append("the value is "+str(subtraction)+" point")

then the result sended to index.html, on my html the code is like this :

<table><tr>
<td>No</td>
<td>description</td>
<td><span id="result"></span></td>
</tr></table>
<script>
var result = '{{result}}';
document.getElementById("result").innerHTML = result;
</script>

i expect to get result value like the value is 3 pointbut i getobject HTML SpanELement, i already check the console.log, i got &#34;the value is 3 point&#34; as resultvalue. how can i make it work the way i want ? sorry if my question is not clear, I will edit again. Thank you in advance

4
  • Have you tried alert({{result}}) to see what you get? if it is json consider using JSON.parse({{result}}); Commented Sep 7, 2020 at 19:20
  • What are you using to send result to the template? Flask? Commented Sep 7, 2020 at 19:25
  • yes, im using flask Commented Sep 7, 2020 at 19:31
  • i tried using alert and JSON.parse, both make the table column didnt show anything @A.Meshu Commented Sep 7, 2020 at 19:33

1 Answer 1

1

Not sure what templating system you are using. Here is one for Flask:

{% for item in result %}
  <td><span>{{ item }}</span></td>
{% endfor %}

This is way more efficient than using javascript to build the document. And this will loop through the list.

BONUS: F-strings in Python 3 will change your life:

result.append(f"the value is {subtraction} point")
Sign up to request clarification or add additional context in comments.

5 Comments

@GAEfann sorry i wrote my code wrongly, ill edit it first
i tried it multiple time but it won't work in my case
You're going to have to share more code and the error messages. This is the correct way to do this task.
aaah my bad, i got typo, your solution is work, thanks so much @GAEfan
i combine it with your first solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.