r/Python • • Apr 20 '14

HMTL and Python

I'm trying to use the setHtml function of PyQt to display some Html for a small app. I'd like to return the text from a function. Currently, on calling the function, the HTML displays in the textBrowser, but there is no right justify of the numbers in stack list.

Code:

def display(stack):
head = '''

<!DOCTYPE html>

<style type="text/css"> body { font-family: sans-serif; font-size: large } .container { position: absolute; bottom:0; margin: 0 5px 5px 5px; } .right{ display: block; float: right; text-align: right width: 500px; } </style>

<body>'''

tail = '</body>'

i = len(stack)
for num in stack:
	if i % 2 == 0:
		line = '<div class = "container"; style = background-color:#a0c4de;><b>' + str(i) +':</b>' +  \
						'<span class = "right">'+ str(num) +'</span>' + \
					'</div>'
	else:
		line = '<div class = "container"><b>' + str(i) +':</b>' + \
						'<span class = "right">' + str(num) +'</span>' + \
					'</div>'

	head += line
	i -= 1
		
return head + tail

I originally wanted to use Jinja 2 to accomplish this but wasn't able to get it to work.

Any advice is appreciated.

Edit: I don't think pyqt supports the css styles I'm trying to use: PyQt style docs

However, I'm still not able to get this to work even by using margin-right: 100px; in my .left properties. However, this works using JSFiddle: HTML test

Thoughts?

0 Upvotes

6 comments sorted by

View all comments

2

u/call_me_cookie Apr 20 '14

If you really want to build HTML without using a templating engine, DOMinate has a joyous API for building html docs:

https://github.com/Knio/dominate