4

so in PHP it's possible to have an entire section of php source be filled with direct raw html:

    <?php
    function doThis(){
    ?>

    <html>
    <a>LOOL</a>
    </html>


    <?php
    }

    doThis();

    ?>

and calling doThis() will print out all the html code between the curly braces...is there a similar functionality in Python? or do I have to virtually print all the HTML individually using the print command? python's indentation seems to make it really inconvenient to write HTML on python code

0

5 Answers 5

4

I'm not certain I fully understand your question, but if you need to have long blocks of arbitrary text in Python the best way I've found is like so:

myHTML = """
<html>
<head>
<title>I am an HTML Page<title>
<head>
<body>
<div>Some content here.</div>
</body>
</html>
"""

The key is the triple Quotes. It allows you to put any other content between them, including line breaks, spacing, etc.

Sign up to request clarification or add additional context in comments.

Comments

4

Python is not a pseudo-template language like PHP, if you want to generate HTML use a template engine like Jinja2.

Comments

2

First, multiline strings:

"""\
<html>
<a>LOOL</a>
</html>"""

Second, if you're writing something significant, you should use a web framework and a template language for the page layout and static content.

Comments

1

Use some template languages like jinja or mako

Comments

0

Well, you could put the code into a file and make python print out the file.

OR

use string suppression to escape everything:

def printHTML():
    HTML = """ all this
is 
considered to be
    ONE
        GIANT
            STRING
"""
    print HTML

Hope this helps

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.