ThunderSpark
ThunderSpark

Reputation: 109

Python Flask: How to use '{}'.format() in a multiline string (""") especially a html one?

Hello currently I know it is possible to format string variables with the '{}'.format(x), but how exacly do you do this in in a multiine string (""" in this case an long html page string)? Because if I put these '{}.format()' in a multiline string like this one pretty sure it will just return as string.

Because here is the thing with this, I just need to the make html_string for a email body, but some variables like the download link have to come in here. Not to mention also a special scenario depending if a bool is there or not.


def generate_html_body(attachment_bool: bool,download_link: str):
    if attachment_bool is False:
        Letter_1 = "Helaas was deze te groot om via mail door te sturen."
        Letter_2 = "Klik hier om het bestand te downloaden."
    if attachment_bool is True:
        Letter_1 =  "U vindt het bestand terug in de bijlage."
        Letter_2 = "Is het bestand niet in de bijlage terug te vinden? Klik dan hier en download dan hier."



 html_string="""<html>
<head>
    <title>{{ title }}</title>
</head>



<body>
    <p>Uw GIPOD data is gereed,</p>
    <p> { Letter_1}</p>
    <a href="{}"> {{  Letter_2 }} </a>
    <p> U heeft 30 dagen de tijd om deze te downloaden. Bedankt om CLASSIFIED te gebruiken. </p>

    <table>
        <tr>
            <td>
                <p>
                    <![if !vml]><img width=500 height=500 src="" alt="logo" class="img-responsive"><![endif]>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p></p>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span></span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span style='font-size:10.0pt;line-height:105%;font-family:Roboto;color:#00273F'>
                        classiefie<o:p></o:p>
                    </span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <b>
                        <span>
                            <a href="classiefie"">
                                <span style='color:#0563C1'>classiefie"/span>
                            </a>
                        </span>
                    </b><span></span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span></span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <b>
                        <span lang=NL style='font-size:7.0pt;line-height:105%;font-family:Roboto;
  color:#5EBA00;'>
                            Gelieve rekening te houden met het milieu
                            voordat u dit document afdrukt<o:p></o:p>
                        </span>
                    </b>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span></span>
                </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    <span>
                        <![if !vml]><img img width=500 height=500 src="classiefie">
                    </span>
                </p>
            </td>
        </tr>
    </table>

</body>
</html>"""

Upvotes: 0

Views: 892

Answers (1)

ljdyer
ljdyer

Reputation: 2086

str.format() works just fine for multi-line strings and I have used it for HTML in Flask apps with no issues.

print("""
    Here's the number I want to show you: {number}
    """.format(number=42)
)

output:

    Here's the number I want to show you: 42

I personally prefer to use f-strings:

number = 42
print(f"""
    Here's the number I want to show you: {number}
    """
    )

output:

    Here's the number I want to show you: 42

Upvotes: 1

Related Questions