18

I am writing a small reporting app using wxPython (wxAUI). I want to render my data as HTML, to be displayed in a WebView 'widget'. I am looking for a sample 'hello world' snippet that will show how to display/render an HTML string in a WebView widget - but have been unable to find a single example - and the WebView widget does not seem to be well documented.

Could someone please provide a link to such an example or (better still), post a short snippet here that shows how to use the WebView widget to render an HTML string?

# sample html string to display in WebView widget
html_string = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
       <title>Hello World!</title>
       <script type="text/javascript" src="jquery.js"></script>
       <style type="text/css" src="main.css"></style>
    </head>
    <body>
        <span id="foo">The quick brown fox jumped over the lazy dog</span>
        <script type="text/javascript">
        $(document.ready(function(){
           $("span#foo").click(function(){ alert('I was clicked!'); });
         });
        </script>
    </body>
</html>
"""
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341

2 Answers2

30

This is a simple example that works for me.

Make sure you are running the latest version of wxpython. (wxpython 2.9)

import wx 
import wx.html2 

class MyBrowser(wx.Dialog): 
  def __init__(self, *args, **kwds): 
    wx.Dialog.__init__(self, *args, **kwds) 
    sizer = wx.BoxSizer(wx.VERTICAL) 
    self.browser = wx.html2.WebView.New(self) 
    sizer.Add(self.browser, 1, wx.EXPAND, 10) 
    self.SetSizer(sizer) 
    self.SetSize((700, 700)) 

if __name__ == '__main__': 
  app = wx.App() 
  dialog = MyBrowser(None, -1) 
  dialog.browser.LoadURL("http://www.google.com") 
  dialog.Show() 
  app.MainLoop() 
patchie
  • 615
  • 1
  • 9
  • 21
  • Thanks for the snippet. When I attempt to run it though, I get the following ImportError: `No module named html2`. I am running Python v 2.6.5 and wx version 2.8.12.1 (gtk2-unicode) – Homunculus Reticulli Jun 03 '12 at 10:18
  • 3
    You need the latest version of wxPython. You are using the "stable" version, but this function is so new that you need the "development release". You can find it here, under "Development release": http://www.wxpython.org/download.php – patchie Jun 04 '12 at 21:19
  • Why does the browser take up the whole window? – User Jan 08 '14 at 19:13
  • To anyone that tries to run this example under OSX: You'll get a load failure for the http://www.google.com reference. OSX doesn't allow http references by default. You can change it to https://... , but I also found the following change which I don't understand but fixes it: import AppKit..... AppKit.NSBundle.mainBundle().infoDictionary()['NSAppTransportSecurity'] = dict(NSAllowsArbitraryLoads = True) – RufusVS Feb 07 '18 at 04:15
12

I posted to this thread after reading the first two entries, and in my post I said something like:

There is an answer here, but it doesn't answer the question. The question was: How do I display an HTML file in a string in a browser window? The only answer opens the browser window, but gets data from a url and doesn't use the string contents.

But then I researched the answer further, I took the postings here and came up with the actual answer to the original question, which was: How do I display from a string?:

If you copy the html string assignment into the code sample, but replace the line:

  dialog.browser.LoadURL("http://www.google.com") 

with:

  dialog.browser.SetPage(html_string,"")

Everything should work as desired (displaying html page from a string (instead of url))

Share and Enjoy!

RufusVS
  • 4,008
  • 3
  • 29
  • 40