View difference between Paste ID: VSjYBrSF and 5NEHsp2L
SHOW: | | - or go back to the newest paste.
1
from HTMLParser import HTMLParser
2
class MyHtmlParser(HTMLParser):
3
    '''
4
    Parse simple url to extract data and image url.
5
    This is expecting a simple url containing only one data block and one iimage url.
6
    '''
7
    
8
    def handle_starttag(self, tag, attrs):
9
        if tag == 'img':
10
            for a in attrs:
11
                if a[0] == 'src':
12
                    self.imageUrl = a[1]
13
    
14
    def handle_data(self, data):
15
        self.text = data
16
    
17
class HtmlIconLabel(QWidget):
18
    
19
    def __init__(self, parent=None):
20
        super(HtmlIconLabel, self).__init__(parent)
21
        self.setLayout(QHBoxLayout())
22
        self.setupUI()
23
24
    def setupUI(self):
25
        self.imgLabel = QLabel()
26
        self.textLabel = QLabel()
27
        self.textLabel.setTextFormat(Qt.RichText)
28
        self.textLabel.setWordWrap(True)
29
        self.textLabel.setOpenExternalLinks(True)
30
        self.textLabel.setStyleSheet('a:link {COLOR: #C74F24;}')
31
        self.layout().addWidget(self.imgLabel)
32
        self.layout().addWidget(self.textLabel)
33
34
35-
    def setHtmlText(self, html, baseUrl='http://www.nukepedia.com/'):
35+
    def setHtmlText(self, html, baseUrl=''):
36
        '''extract icon from html, download it and display it along with text contained in html'''
37
        import urllib2
38
        parser = MyHtmlParser()
39
        parser.feed(html)
40
41
        f = urllib2.urlopen(baseUrl + parser.imageUrl)
42
        imageFile = f.read()
43
        pixmap = QPixmap()
44
        pixmap.loadFromData(QByteArray(imageFile))
45
        self.pixmap = pixmap
46
        self.textLabel.setText(parser.text)
47
        self.imgLabel.setPixmap(self.pixmap)