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

asked May 9, 2011 at 15:57
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.

answered May 9, 2011 at 16:04
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.

answered May 9, 2011 at 16:03

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.

answered May 9, 2011 at 16:03

Comments

1

Use some template languages like jinja or mako

answered May 9, 2011 at 16:04

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

answered May 9, 2011 at 16:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.