I need help with embedding a base64 image code into HtmlWindow

Hi,

I converted a small png image to base64 code, and embedded into HtmlWindow as below:

import wx
import wx.html


sample_html = """<html>
                <body>
                <br>
                <br>
                <h2>Hello HTML World!</h2>
				
                
              <img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAAq8AAAKvAFm+v6GAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAs9JREFUSImVlTtsFFcUhr9zZnfBKxuzgLQGORAjwBCDxLtEIRKGCiojHkUIKZJISKFIGcnQQAENiIeEEFiASIFoaEKgARpIJJMFbBmhxISkIEthjHFm7dmdOSl2vRjjeXCqO3f+83937r1zjhATw3tW5ZBKpwTWjkoegMCKhj61GZlbuZ7CcFS+hBrval8vjh7C6ATSIbIy8Isp3bOvDDxMBLCu1oaR9KwzYF9GLWBqmsGF5kzpgPT8NRYKGO3qaPHT/g2QDWFO2roU55OlVH6/g425U1//5pizvfGnvuIHANv36cwRr+EusDHKvPHIdXBSVAr3cI99O52s97/G7KYF53pdAJ2YHfGyZ6PMAZzWJeCkqrD8wjDZuuyoe7K+KKgeaG3PI6NSuEul7z7B0L+MXzsRqhPYP7xn+TqAFEDttsQeqI25uEe/jpMBiKDdwHZ5vW/1bPXGi0AmTJ1au5n0xi3oohVQ9hi7dAT/j0dxkLKRyqfUG98WZQ6Q+WInurAdndtSfe7cSykekMb8LYrZsjile/w7Sqd/qD/b26G4FABEbJkiMj+J2GlbWR/7f/YlAiDMV8ASARZPAgw+SQYwTIGXHwMw9y1B8e9k/sZLNeFZnFCyTWhL9cfyB/uqlSdJiDzTwBu/CXhROqetA0TfAZJF2TKZ2zrn2uAb4FYk4L39Twgw+znXUxhWgED1RyAI02pbR30cPO9PYh+YI4ehVotyV/ofGVwMBcxbMLEqtK0DbV0S6S7I+YkGVK+mzZnSAYRfp0vwXwzUMoXswRPM2PFNlH9vU3nk4DvYpBjdvTLvi3+DqWXbSZFa8zmiiv9igKD4T5j5A3XSO5ouP341LQCqjeeN13BK4KvJXxgTgSDnmzLu95Etc3K83vXZGlUOg20lvBh6mN0MxOnOXe0vTCeI7QFDXYubNT2zU7DlQL42XTTRgcAr3a5d89D4H7H1+SvKTO4wAAAAAElFTkSuQmCC" />
                <br><br>
				<p>The End</p>
                </body>
                </html>"""

class MyHtmlFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(600, 400))
        html = wx.html.HtmlWindow(self)

        html.SetPage(sample_html)


app = wx.App()
frm = MyHtmlFrame(None, "Simple HTML Browser")
frm.Show()
app.MainLoop()

Normally an orange colored “i” image should appear in the HTML window, but I see a red cross.
However, the HTML code works fine in Google Chrome.

HtmlWindow doesn’t support base64 image codes? How can I fix it?

Using wx.html2 works using wxPython 4.2.1 + Python 3.10.12 + Linux Mint 21.3

import wx
import wx.html2 as html2

sample_html = """<html>
                <body>
                <br>
                <br>
                <h2>Hello HTML World!</h2>


              <img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAAq8AAAKvAFm+v6GAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAs9JREFUSImVlTtsFFcUhr9zZnfBKxuzgLQGORAjwBCDxLtEIRKGCiojHkUIKZJISKFIGcnQQAENiIeEEFiASIFoaEKgARpIJJMFbBmhxISkIEthjHFm7dmdOSl2vRjjeXCqO3f+83937r1zjhATw3tW5ZBKpwTWjkoegMCKhj61GZlbuZ7CcFS+hBrval8vjh7C6ATSIbIy8Isp3bOvDDxMBLCu1oaR9KwzYF9GLWBqmsGF5kzpgPT8NRYKGO3qaPHT/g2QDWFO2roU55OlVH6/g425U1//5pizvfGnvuIHANv36cwRr+EusDHKvPHIdXBSVAr3cI99O52s97/G7KYF53pdAJ2YHfGyZ6PMAZzWJeCkqrD8wjDZuuyoe7K+KKgeaG3PI6NSuEul7z7B0L+MXzsRqhPYP7xn+TqAFEDttsQeqI25uEe/jpMBiKDdwHZ5vW/1bPXGi0AmTJ1au5n0xi3oohVQ9hi7dAT/j0dxkLKRyqfUG98WZQ6Q+WInurAdndtSfe7cSykekMb8LYrZsjile/w7Sqd/qD/b26G4FABEbJkiMj+J2GlbWR/7f/YlAiDMV8ASARZPAgw+SQYwTIGXHwMw9y1B8e9k/sZLNeFZnFCyTWhL9cfyB/uqlSdJiDzTwBu/CXhROqetA0TfAZJF2TKZ2zrn2uAb4FYk4L39Twgw+znXUxhWgED1RyAI02pbR30cPO9PYh+YI4ehVotyV/ofGVwMBcxbMLEqtK0DbV0S6S7I+YkGVK+mzZnSAYRfp0vwXwzUMoXswRPM2PFNlH9vU3nk4DvYpBjdvTLvi3+DqWXbSZFa8zmiiv9igKD4T5j5A3XSO5ouP341LQCqjeeN13BK4KvJXxgTgSDnmzLu95Etc3K83vXZGlUOg20lvBh6mN0MxOnOXe0vTCeI7QFDXYubNT2zU7DlQL42XTTRgcAr3a5d89D4H7H1+SvKTO4wAAAAAElFTkSuQmCC" />
                <br><br>
				<p>The End</p>
                </body>
                </html>"""


class MyHtmlFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(600, 400))

        browser = html2.WebView.New(self)
        browser.SetPage(sample_html, '')


app = wx.App()
frm = MyHtmlFrame(None, "Simple HTML2 Browser")
frm.Show()
app.MainLoop()

Screenshot at 2024-06-10 14-51-42

1 Like

Thanks Richard.

But html2.WebView takes like 5 seconds to load the page. Is there a way to speed it up?

According to the docs (wx.html2.WebView — wxPython Phoenix 4.2.1 documentation) wx.html2.WebView uses different backend libraries, depending on the operating system you are using.

I’ve just checked on my Linux desktop PC and on my MacBook Pro and both of them took less than half a second from starting the program until the frame and the HTML page were displayed. I don’t have a Windows machine to test on.

1 Like