How to print list in python flask? ( jinja2) (html)

Viewed 615

I have a list in python. Each element in a list will have a lengthy line. my code is printing all element in the list but, since every element are too lengthy its printing each element as whole continues paragraph

I need like: All the element should print one by one. since its too lengthy i can have Horizontal scroll bar. so that i can have one by one. Here's my code

index.html
<html>
<head>
    <meta charset="UTF-8">
    <title> checking</title>
</head>
<body>

      {%for i in Jinja_list %}

    {{ i }}
   
{%endfor%}
    </body>
</html>
app.py
@app.route('/',methods=['POST'])
def index():
       Jinja_list = ['lenthyyyyy line1',''lenthyyyyy line2',......]
       return render_template('index.html',Jinja_list=Jinja_list)

if __name__ == "__main__":
    app.run(debug=True, port=5042)

(i need like) output:(as soon as runs the app.py) (in the browser)

lenthyyyyy line1........
lenthyyyyy line2..........
lenthyyyyy line3......

###Since it lenghty how can i have horizontal scroll bar

2 Answers

First of all, you must pass a list to the view. After that you should use a for loop to iterate over the items and create elements like <tr> or <div> to show them.

I can see that you don't know how to create one of those elements. Let's say we want to put the data of each item in a <div>, you can do something like this.

{% for e in list %}
<div>
    {{e}}
</div>
{% endfor %}

if you want, you can also pass a list of some objects to send even more complex data.

In your template try to do this:

<html>
<head>
    <meta charset="UTF-8">
    <title> checking</title>
</head>
<body>

   {%for i in Jinja_list %}

      <p>{{ i }}</p>
   
   {%endfor%}
    </body>
</html>

Specifically, add the <p> tag where you want to print the value of i. This will make sure that each element from your list will get printed below the previous one (line by line).

Related