Skip to content

Commit 752766a

Browse files
committed
Added an example of saving browser view as an image to cefadvanced.py.
Updated cefwxpanel samples. Fixed panda3d example, the browser was in grayscale.
1 parent ba2f430 commit 752766a

6 files changed

Lines changed: 91 additions & 28 deletions

File tree

cefpython/cef1/windows/binaries/cefadvanced.html

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<style>
77
body { font: 13px Segoe UI, Arial; line-height: 1.4em; }
88
pre { background: #ddd; font: 12px Consolas, Courier New; }
9+
h4 { color: #666; margin: 1em 0em; }
910
</style>
1011
</head>
1112
<body>
@@ -112,9 +113,22 @@ <h3>Frame object</h3>
112113
<a href="javascript:((frame.Undo()))">Undo()</a><br>
113114
<a href="javascript:((frame.ViewSource()))">ViewSource()</a><br>
114115

116+
<h3>Browser view as an image</h3>
117+
118+
Browser.GetImage() returns PaintBuffer, that object
119+
has method called GetString() that returns the buffer
120+
as a string that can be later passed to Python Imaging Library
121+
by calling PIL.Image.fromstring() and saved as a PNG (or
122+
other format) to a file. Test it:<br>
123+
<a href="javascript:python.SaveImage('!cefadvanced.png', 'PNG')">
124+
python.SaveImage('!cefadvanced.png', 'PNG')</a><br>
125+
(you need to have installed the PIL extension).
126+
115127
<h3>Test unicode string</h3>
116128

117-
<a href="javascript:alert(python.GetUnicodeString())">python.GetUnicodeString()</a>
129+
<a href="javascript:alert(python.GetUnicodeString())">
130+
python.GetUnicodeString()</a><br>
131+
You should see a long dash in python 2.7.
118132

119133
<h3>Test creation of second browser</h3>
120134

@@ -146,10 +160,6 @@ <h3>Frame.ExecuteJavascript()</h3>
146160
<textarea id=jscode>alert('test me')</textarea><br>
147161
<a href="javascript:python.ExecuteJavascript(jscode.value)">python.ExecuteJavascript(jscode.value)</a> - execute javascript from the textarea above.
148162

149-
<h3>Frame.LoadUrl(url)</h3>
150-
151-
<a href="javascript:python.LoadUrl()">python.LoadUrl()</a> - will load "cefsimple.html" into main frame of this window.
152-
153163
<h3>Frames</h3>
154164

155165
<iframe src="cefsimple.html" name=simple></iframe>
@@ -183,29 +193,23 @@ <h4>Passing arguments</h4>
183193
Call __browser.GetMainFrame().GetProperty("PyConfig"): <a href="javascript:python.PrintPyConfig()">python.PrintPyConfig()</a><br>
184194
Try setting PyConfig during runtime using __browser.GetMainFrame().SetProperty(): <a href="javascript:python.ChangePyConfig()">python.ChangePyConfig()</a> (then call python.PrintPyConfig())
185195

186-
<h4>Infinite recursion</h4>
187-
Test infinite recursion: <a href="javascript:python.Test1(window)">python.Test1(window)</a> - you will see a python exception in console (also in error.log), data structures can have maximum 8 levels of nesting.
188-
189196
<h3>Javasript callbacks</h3>
190197

191198
<script>
192199
function MyJSCallback(arg1, arg2, arg3, arg4)
193200
{
194201
alert("MyJSCallback() called\narg1="+JSON.stringify(arg1)+"\narg2="+JSON.stringify(arg2)+"\narg3="+JSON.stringify(arg3)+"\narg4="+JSON.stringify(arg4))
195202
}
196-
function MyJSCallback2()
197-
{
198-
throw new Error("asd")
199-
}
200203
</script>
201204

202-
<a href="javascript:python.TestJavascriptCallback(MyJSCallback)">python.TestJavascriptCallback(MyJSCallback)</a> - MyJSCallback alerts arguments
203-
<br>
204-
<a href="javascript:python.TestJavascriptCallback(MyJSCallback2)">python.TestJavascriptCallback(MyJSCallback2)</a> - MyJSCallback2 throws new Error("asd")
205-
205+
<a href="javascript:python.TestJavascriptCallback(MyJSCallback)">
206+
python.TestJavascriptCallback(MyJSCallback)</a><br>
207+
MyJSCallback alerts arguments.
206208

207209
<h3>Python callbacks</h3>
208210

211+
<p>See the output in the console.</p>
212+
209213
<script>
210214
function JSCallbackForPythonCallback(pycallback)
211215
{
@@ -216,13 +220,36 @@ <h3>Python callbacks</h3>
216220
<a href="javascript:python.TestPythonCallbackThroughReturn()('some')">python.TestPythonCallbackThroughReturn()('some')</a><br>
217221
<a href="javascript:python.TestPythonCallbackThroughJavascriptCallback(JSCallbackForPythonCallback)">python.TestPythonCallbackThroughJavascriptCallback(JSCallbackForPythonCallback)</a>
218222

219-
<h3>Catching javascript errors programmatically</h3>
223+
<h3>Errors</h3>
220224

221225
<p>Typically when you browse a webpage your javascript errors appear in javascript console in developer tools (in cefadvanced.py there is a binding to F12 key for developer tools).</p>
222226

223-
<h3>Test javascript error, OnUncaughtException() callback will catch it</h3>
227+
<h4>Global exception handler</h4>
228+
229+
<p>You can catch javascript errors programmatically by
230+
using JavascriptContextHandler and the OnUncaughtException callback,
231+
test it:<br>
232+
<a href="javascript:DoSomeError()">
233+
DoSomeError()</a>
234+
</p>
235+
236+
<h4>Test error when calling javascript callback from python</h4>
237+
238+
<script>
239+
function JsCallbackThrowingError()
240+
{
241+
throw new Error("some error message")
242+
}
243+
</script>
244+
<a href="javascript:python.TestJavascriptCallback(JsCallbackThrowingError)">
245+
python.TestJavascriptCallback(JsCallbackThrowingError)</a><br>
246+
JsCallbackThrowingError throws new Error().
247+
248+
<h4>Infinite recursion</h4>
224249

225-
<p>Test javascript error: <a href="javascript:Undefined()">Undefined()</a></p>
250+
<a href="javascript:python.Test1(window)">python.Test1(window)</a><br>
251+
You should see a python exception in the console (also in error.log),
252+
data structures can have maximum 8 levels of nesting.
226253

227254
<h3>Moving and resizing window</h3>
228255

cefpython/cef1/windows/binaries/cefadvanced.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,22 @@ def OnTitleChange(self, browser, title):
269269
class Python:
270270
browser = None
271271

272+
def SaveImage(self, outfile, format):
273+
outfile = cefpython.GetRealPath(outfile)
274+
try:
275+
from PIL import Image
276+
except:
277+
print("PIL library not available, can't save image")
278+
return
279+
(width, height) = self.browser.GetSize(cefpython.PET_VIEW)
280+
buffer = self.browser.GetImage(cefpython.PET_VIEW, width, height)
281+
image = Image.fromstring(
282+
"RGBA", (width,height),
283+
buffer.GetString(mode="rgba", origin="top-left"),
284+
"raw", "RGBA", 0, 1)
285+
image.save(outfile, format)
286+
os.system(outfile)
287+
272288
def ExecuteJavascript(self, jsCode):
273289
self.browser.GetMainFrame().ExecuteJavascript(jsCode)
274290

cefpython/cef1/windows/binaries/cefwxpanel.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CEFWindow(wx.Window):
2727
"""Standalone CEF component. The class provides facilites for interacting with wx message loop"""
2828
def __init__(self, parent, url="", size=(-1, -1), *args, **kwargs):
2929
wx.Window.__init__(self, parent, id=wx.ID_ANY, size=size, *args, **kwargs)
30-
30+
self.url = url
3131
windowInfo = cefpython.WindowInfo()
3232
windowInfo.SetAsChild(self.GetHandle())
3333
self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings={}, navigateURL=url)
@@ -63,12 +63,19 @@ def OnSetFocus(self, event):
6363
def OnSize(self, event):
6464
cefpython.WindowUtils.OnSize(self.GetHandle(), 0, 0, 0)
6565

66+
#def Shutdown(self):
67+
#print "CLOSING PAGE %s, YA MAN" % self.url
68+
#self.timer.Stop()
69+
#self.browser.CloseBrowser()
6670
#-------------------------------------------------------------------------------
6771

68-
def initCEF():
69-
"""Initializes CEF, We should do it before initializing wx"""
72+
def initCEF(settings=None):
73+
"""Initializes CEF, We should do it before initializing wx
74+
If no settings passed a default is used
75+
"""
7076
sys.excepthook = cefpython.ExceptHook
71-
settings = {"log_severity": cefpython.LOGSEVERITY_VERBOSE, "release_dcheck_enabled": True}
77+
if not settings:
78+
settings = {"log_severity": cefpython.LOGSEVERITY_VERBOSE, "release_dcheck_enabled": True}
7279
cefpython.Initialize(settings)
7380

7481
def shutdownCEF():

cefpython/cef1/windows/binaries/cefwxpanel_sample2.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
# __author__ = "Greg Kacy <grkacy@gmail.com>"
33

44
import wx
5+
import wx.lib.agw.flatnotebook as fnb
56

67
from cefwxpanel import initCEF, shutdownCEF, CEFWindow
78

89
ROOT_NAME = "My Locations"
910

1011
URLS = ["cefsimple.html",
12+
"http://google.com",
13+
"http://maps.google.com",
14+
"http://youtube.com",
1115
"http://yahoo.com",
1216
"http://wikipedia.com",
13-
"http://maps.google.com"]
17+
"http://cyaninc.com",
18+
]
1419

1520
class MainFrame(wx.Frame):
1621
def __init__(self):
@@ -26,16 +31,17 @@ def initComponents(self):
2631
self.tree.AppendItem(self.root, url)
2732
self.tree.Expand(self.root)
2833

29-
self.tabs = wx.Notebook(self, id=-1, style=wx.BK_DEFAULT)
34+
self.tabs = fnb.FlatNotebook(self, wx.ID_ANY, agwStyle=fnb.FNB_NODRAG|fnb.FNB_X_ON_TAB)
3035

3136
def layoutComponents(self):
32-
sizer = wx.BoxSizer()
37+
sizer = wx.BoxSizer(wx.HORIZONTAL)
3338
sizer.Add(self.tree, 0, wx.EXPAND)
3439
sizer.Add(self.tabs, 1, wx.EXPAND)
3540
self.SetSizer(sizer)
3641

3742
def initEventHandlers(self):
3843
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
44+
self.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING, self.OnPageClosing)
3945
self.Bind(wx.EVT_CLOSE, self.OnClose)
4046

4147
def OnSelChanged(self, event):
@@ -44,7 +50,11 @@ def OnSelChanged(self, event):
4450
if url and url != ROOT_NAME:
4551
cefPanel = CEFWindow(self.tabs, url=str(url))
4652
self.tabs.AddPage(cefPanel, url)
53+
self.tabs.SetSelection(self.tabs.GetPageCount()-1)
54+
event.Skip()
4755

56+
def OnPageClosing(self, event):
57+
print "One could place some extra closing stuff here"
4858
event.Skip()
4959

5060
def OnClose(self, event):

cefpython/cef1/windows/binaries/panda3d_.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ def __init__(self):
5555
windowHandle = base.win.getWindowHandle().getIntHandle()
5656

5757
self.texture = Texture()
58-
self.texture.setup2dTexture(400, 300, Texture.CMOff,
59-
Texture.FLuminanceAlpha)
58+
self.texture.setXSize(400)
59+
self.texture.setYSize(300)
60+
self.texture.setCompression(Texture.CMOff)
61+
self.texture.setComponentType(Texture.TUnsignedByte)
62+
self.texture.setFormat(Texture.FRgba4)
6063

6164
cardMaker = CardMaker("browser2d")
6265
cardMaker.setFrame(-0.75,.75,-0.75,0.75)

cefpython/var/panda3d.png

74.7 KB
Loading

0 commit comments

Comments
 (0)