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

8

u/fancy_pantser Apr 20 '14 edited Apr 20 '14

There are many things wrong here.

  1. it's HTML, not HMTL

  2. it's generally bad to build HTML this way; use a templating language

  3. <style> goes inside the <head>, which you left out completely

  4. I think you have some other things that you removed, so I can't say for sure but if things with the right class are 500px wide, the container will expand to that size too. I think you meant for the container to be wider than that? Perhaps page-width?

  5. this part: if i % 2 == 0: isn't doing what you think it is because you set i only once (before the loop) this way: i = len(stack)

  6. use triple-quotes or \ for multi-line string literals, try to not mix the two

  7. in the jsfiddle you linked, both items are left-aligned, meaning it did not work

  8. why put the background color inline and not with the rest of the CSS styling?

  9. you can't overlap tags like: <b><div>blah blah</b></div>

edit: here is a fixed version in jsfiddle: http://jsfiddle.net/k8QSy/

2

u/yardshop Apr 20 '14

this part: if i % 2 == 0: isn't doing what you think

He does decrement i at the end of the loop.

2

u/brucesalem Apr 20 '14

Print head + tail to verify how the string is formatted. You may need to escape punctuation.

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

2

u/lykwydchykyn Apr 21 '14

As you've discovered, the textBrowser doesn't support full CSS. You can always use a webkit widget if you want full HTML support, though this adds some dependencies to your app.

1

u/yardshop Apr 20 '14

You're missing an ending semi-colon after "text-align: right".

Also your inline style tag in your first "line" declaration is not correct. You don't need the semi-colons there, and you need to put the style attribute in quotes. It should be something like this:

    line = '<div class = "container" style = "background-color:#a0c4de"><b>' # etc

The others are correct that you should use a library to generate this stuff for you, but if you are going to do it by hand, you should mock it up as a stand-alone HTML file first, get that working the way you want it, then bring the pieces back into your script and make sure the script generates the identical structure.