0

I have made a for loop in python that will iterate depedent on how many records I have. So in every loop I want to add a new row with that specific record (i) in the table in the html code. So is it possible to do that in Python and do you have any good example code I can learn from?

My loop looks like this below and in every (i) I want to add both html pieces I have added below (one of them is constant same code and the other one will change one value inside of the html code). In the end there should be 10 x html pieces with different values inside the html code. for i in range(10):

<table class="x_text_block" width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation" style="word-break: break-word;">
   <tbody>
        <tr>
            <td style="padding-bottom: 15px; padding-left: 10px; padding-right: 10px; padding-top: 15px;">
                <div style="font-family: sans-serif;">
                    <div class="x_txtTinyMce-wrapper" style="font-size: 12px; color: #555; line-height: 1.2; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;">
                        <p style="margin: 0; font-size: 14px;"><span style="font-size: 18px; color: #808080;">I WANT TO CHANGE THIS VALUE EACH FOR LOOP</span></p>
                    </div>
                </div>
            </td>
        </tr>
   </tbody>
</table>

<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="">
   <tbody>
      <tr>
    <td class="x_divider_inner" style="font-size: 1px; line-height: 1px; border-top: 1px solid #e1ede5;"><span> </span></td>
     </tr>
  </tbody>
</table>
3
  • Yes it is possible. In order for others to help you, you must post the code you have written. stackoverflow.com/help/minimal-reproducible-example Commented May 31, 2022 at 21:15
  • Where are the "records I have" coming from? Reading a file?, database? What format are they in? Commented May 31, 2022 at 21:21
  • @CaptainCaveman The format can be Int, String and I want to add the values each loop. See my code I have added. The data that I take is in pandas format. Commented Jun 1, 2022 at 6:15

1 Answer 1

1

you can use jinja.

For example, consider it data = {key1: [1,2,3,4], key2: [4,5,6]}.

in the HTML file you can have:

<table id="example-good" class="table" style="width:100%">
<thead>
  <tr>
      <th>Messages</th>
  </tr>
</thead>
<tbody>
  {% for key, value in data.items() %}
  <tr>
  {% for i in value %}

      <td>{{i}}</td>
  </tr>
  {% endfor %}
  {% endfor %}
</tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

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.