<div dir="ltr"><div class="markdown-here-wrapper" id="markdown-here-wrapper-215404" style><p style="margin:1.2em 0px!important">Looks great! Thanks for sharing your approach. Keep in mind that if the HTML fragment is untrusted and doesn’t have an <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">img</code> tag with a <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">src</code> attribute, your app will crash on an <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">AttributeError</code>. Tuple unpacking could make the for loop code a little more readable:</p>


<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">    <span class="function"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" style="color:rgb(153,0,0);font-weight:bold">handle_starttag</span><span class="params">(self, tag, attrs)</span>:</span>
        <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">if</span> tag == <span class="string" style="color:rgb(221,17,68)">'img'</span>:
            <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">for</span> attr_name, attr_value <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">in</span> attrs:
                <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">if</span> attr_name == <span class="string" style="color:rgb(221,17,68)">'src'</span>:
                    self.imageUrl = attr_value</code></pre>
<p style="margin:1.2em 0px!important">I like the use of built-in libraries. I used requests and lxml because I am using them for a large project and already knew how to use them, but they are definitely overkill for this.</p>


</div></div><div class="gmail_extra"><br clear="all"><div><div><br></div><div>--</div><div>Sean Fisk</div></div>
<br><br><div class="gmail_quote">On Tue, Nov 12, 2013 at 7:28 PM, Frank Rueter | OHUfx <span dir="ltr"><<a href="mailto:frank@ohufx.com" target="_blank">frank@ohufx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    just to follow up, here is what I have done so far (tried to stay
    away from external libraries as I will have to distribute the app):<br>
    <br>
    <a href="http://pastebin.com/VSjYBrSF" target="_blank">http://pastebin.com/VSjYBrSF</a><br>
    <br>
    This seems to work for me but am happy to hear any comments on this
    approach.<div class="im"><br>
    <br>
    Cheers,<br>
    frank<br>
    <br>
    <br>
    <div>On 12/11/13 21:13, Sean Fisk wrote:<br>
    </div>
    </div><div><div class="h5"><blockquote type="cite">
      <div dir="ltr">
        <div>
          <p style="margin:1.2em 0px!important">Hi Frank,</p>
          <p style="margin:1.2em 0px!important">I think it depends
            whether the HTML dictates the layout or you would like to
            dictate the layout:</p>
          <ul style="margin:1.2em 0px;padding-left:2em">
            <li style="margin:0.5em 0px">HTML dictates layout → Use the
              web view.</li>
            <li style="margin:0.5em 0px">I want to dictate the layout →
              Parse the HTML to extract the URL, download the image
              manually, and put it into a QLabel.</li>
          </ul>
          <p style="margin:1.2em 0px!important">Also remember that if
            you would like to distribute this and bundle PySide/Qt,
            using WebKit will add a significant size to your executable.
            If you truly need it, include it. However, for this, I don’t
            think it’s necessary.</p>
          <p style="margin:1.2em 0px!important">From what you have said,
            it sounds like you would like to dictate the layout. The
            process isn’t exactly simple, but it’s doable in not too
            many lines. Off the top of my head:</p>
          <pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre-wrap;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial"><span style="color:rgb(153,153,136);font-style:italic">#!/usr/bin/env python</span>

<span style="color:rgb(51,51,51);font-weight:bold">import</span> sys

<span style="color:rgb(51,51,51);font-weight:bold">from</span> PySide <span style="color:rgb(51,51,51);font-weight:bold">import</span> QtGui
<span style="color:rgb(51,51,51);font-weight:bold">import</span> requests
<span style="color:rgb(51,51,51);font-weight:bold">import</span> lxml.html

<span><span style="color:rgb(51,51,51);font-weight:bold">def</span> <span style="color:rgb(153,0,0);font-weight:bold">main</span><span>(argv)</span>:</span>
    app = QtGui.QApplication(argv)
    widget = QtGui.QWidget()
    layout = QtGui.QVBoxLayout(widget)
    html = <span style="color:rgb(221,17,68)">'''<p><span style="font-size: 17px;"><span style="color: #993300;"><img style="margin-right: 15px; vertical-align: top;" src="<a href="https://pypi.python.org/static/images/python-3.png" target="_blank">https://pypi.python.org/static/images/python-3.png</a>" alt="announcements" /><cite>some text that is pulled from a website but which will be quite simple.</cite></span></span><img src="<a href="http://placekitten.com/200/100" target="_blank">http://placekitten.com/200/100</a>" /></p>'''</span>  <span style="color:rgb(153,153,136);font-style:italic"># NOPEP8</span>
    doc = lxml.html.fragment_fromstring(html)
    <span style="color:rgb(51,51,51);font-weight:bold">for</span> img_src_attribute <span style="color:rgb(51,51,51);font-weight:bold">in</span> doc.xpath(<span style="color:rgb(221,17,68)">'//img/@src'</span>):
        response = requests.get(img_src_attribute)
        pixmap = QtGui.QPixmap()
        pixmap.loadFromData(response.content)
        label = QtGui.QLabel()
        label.setPixmap(pixmap)
        layout.addWidget(label)
    widget.show()
    widget.raise_()
    <span style="color:rgb(51,51,51);font-weight:bold">return</span> app.exec_()

<span style="color:rgb(51,51,51);font-weight:bold">if</span> __name__ == <span style="color:rgb(221,17,68)">'__main__'</span>:
    <span style="color:rgb(51,51,51);font-weight:bold">raise</span> SystemExit(main(sys.argv))</code></pre>
        </div>
        <div>
          <div>... which yields...</div>
          <div><img src="cid:part3.03010403.07060907@ohufx.com" alt="Inline image 2"><br>
          </div>
          <div>I didn't address the issue of transparent backgrounds,
            etc., because I'm not sure exactly what's going on there.
            But all three of the widgets on-screen are QLabels, so they
            should be able to do anything a QLabel can do.</div>
          <div><br>
          </div>
          <div>Obviously, do some more error checking than I did if you
            are pulling this from a website with untrusted data. Let me
            know if you need help with that. Hope this helps!</div>
          <div><br>
          </div>
          <div>Sincerely,</div>
          <div><br>
          </div>
        </div>
      </div>
      <div class="gmail_extra"><br clear="all">
        <div>
          <div><br>
          </div>
          <div>--</div>
          <div>Sean Fisk</div>
        </div>
        <br>
        <br>
        <div class="gmail_quote">On Mon, Nov 11, 2013 at 11:26 PM, Frank
          Rueter | OHUfx <span dir="ltr"><<a href="mailto:frank@ohufx.com" target="_blank">frank@ohufx.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
            Hi all,<br>
            <br>
            I have a QLabel in my app that displays some very simple
            html code<br>
            pulled from a website.<br>
            <br>
            So far I have been using it successfully as a sort of news
            feed, but now<br>
            I need to display an image from a url as well, which QLabel
            can't do afaik.<br>
            <br>
            So I had a go with QWebView but that just won't be have the
            same as<br>
            QLabel when it comes to background colour (tried to set it
            to be<br>
            transparent), sizes, size policies etc.<br>
            I'm also getting some redraw problems when the QWebView is
            set to a<br>
            transparent background.<br>
            <br>
            Here is some test code:<br>
            <a href="http://pastebin.com/cSizj9ET" target="_blank">http://pastebin.com/cSizj9ET</a><br>
            <br>
            <br>
            Is it the right approach to force QWebView to behave like
            QLabel for the<br>
            sole reason of being able to display an image via html? Or
            is there a<br>
            simpler way of doing this?<br>
            <br>
            <br>
            Cheers,<br>
            frank<br>
            _______________________________________________<br>
            PySide mailing list<br>
            <a href="mailto:PySide@qt-project.org" target="_blank">PySide@qt-project.org</a><br>
            <a href="http://lists.qt-project.org/mailman/listinfo/pyside" target="_blank">http://lists.qt-project.org/mailman/listinfo/pyside</a><br>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </div></div></div>

</blockquote></div><br></div>