Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding Data To Email Through Loop In Python

I am attempting to iterate through a map containing arrays to an HTML email template. I can print out the data in the array but, the data does not show up in the array after finishing the loop.

  html_data = """
    <!DOCTYPE html>

            <html lang="en">
        <head></head>
        <body>




        <br>
        <br>

        Date: {{date}}
        <br>

        <br>
        Hi,
        <br>

        <br>
        Today's Picks Are in:


        <br>

        <br>
        <div class="card-body">
        <table class="table table-sm">
            <thead>
                <tr>

                    <th align='left'>Stock</th>
                    <th>Pick</th>
                    <th>Detail</th>
                    <th>Change</th>

                </tr>
            </thead>
            <tbody>
        <td align='left'>{{ picks }}</td>

            {% for pick in picks %}
                <tr>
                
                    <td align='center'> {{pick.stock}} </td>
                    # <td align='center'> {{pick.prices}} </td>
                    # <td align='center'>{{pick.option_details}}</td>
                    # <td align='center'>{{pick.change}}</td>
                    </tr>
            {% endfor %}

            </tbody>
        </table>
        </div>
        <br>
        <br>




        <p>If you would like to be removed from this list, please contact customer service: <mail_to="devs@hscottindustriescom">HScottIndustries</a> </p>

"""

Picks is the mapped array

picks = {
‘stocks’: stocks,
‘prices’: stock_price,
‘option_details’: options_details,
‘change’: change
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The data from picks is added here.

msg = MIMEText(Environment().from_string(html_data).render(picks=picks, date=date_original), "html")

The output is this:

 <!DOCTYPE html>

            <html lang="en">
        <head></head>
        <body>




        <br>
        <br>

        Date: 07-05-2022
        <br>

        <br>
        Hi,
        <br>

        <br>
        Today's Picks Are in:


        <br>

        <br>
        <div class="card-body">
        <table class="table table-sm">
            <thead>
                <tr>

                    <th align='left'>Stock</th>
                    <th>Pick</th>
                    <th>Detail</th>
                    <th>Change</th>

                </tr>
            </thead>
            <tbody>
        <td align='left'>{'stocks': ['WPP', 'NEX', 'PTEN'], 'prices': ['45.21', '8.37', '13.39'], 'option_details': ['na', 'NEX Jul 15 2022 $7.50 Put', 'PTEN Jul 15 2022 $13.00 Put'], 'change': ['-10.19%', '-11.52%', '-11.53%']}</td>

            
                <tr>
                
                    <td align='center'>  </td>
                    # <td align='center'>  </td>
                    # <td align='center'></td>
                    # <td align='center'></td>
                    </tr>
            
                <tr>
                
                    <td align='center'>  </td>
                    # <td align='center'>  </td>
                    # <td align='center'></td>
                    # <td align='center'></td>
                    </tr>
            
                <tr>
                
                    <td align='center'>  </td>
                    # <td align='center'>  </td>
                    # <td align='center'></td>
                    # <td align='center'></td>
                    </tr>
            
                <tr>
                
                    <td align='center'>  </td>
                    # <td align='center'>  </td>
                    # <td align='center'></td>
                    # <td align='center'></td>
                    </tr>
            

            </tbody>
        </table>
        </div>
        <br>
        <br>




        <p>If you would like to be removed from this list, please contact customer service: <a href="https://hscottindustriescom">HScottIndustries</a> </p>



        <footer class="main-footer">
        <strong>Copyright &copy; 2022 <mail_to="devs@HScottIndustries.com">HScottIndustries LLC</a></strong>

How do I add the get the data to iterate correctly to the corresponding fields?

>Solution :

You are iterating over picks, which right now is a dictionary, meaning you will get dictionary keys in return. You can either format picks in a different way, or use an index. This format should work:

picks = [
  {
     'stocks': 'WPP',
     'prices': '45.21',
     'option_details': 'na',
     'change': '-10.19%'
  },
  {
     'stocks': 'NEX',
     'prices': ...
  },
  ...
]

Alternatively you can pass picks as zipped arrays in your view:

zipped_picks = zip(*picks.values())

Then access them with:

{% for pick in zipped_picks %}
   <div> {{pick.0}} <div>
   <div> {{pick.1}} <div>
   <div> {{pick.2}} <div>
   <div> {{pick.3}} <div>
{% endfor %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading