Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cefpython/examples/tutorial.py at cefpython49-winxp · PythonNexus/cefpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
PythonNexus
/
cefpython
Public
forked from
cztomczak/cefpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
cefpython49-winxp
Breadcrumbs
cefpython
/
examples
/
tutorial.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
189 lines (157 loc) · 6.86 KB
cefpython49-winxp
Breadcrumbs
cefpython
/
examples
/
tutorial.py
Copy path
Top
File metadata and controls
Code
Blame
189 lines (157 loc) · 6.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Tutorial example. Doesn't depend on any third party GUI framework.
# Tested with CEF Python v49.0+
from
cefpython3
import
cefpython
as
cef
import
base64
import
platform
import
sys
import
threading
# HTML code. Browser will navigate to a Data uri created
# from this html code.
HTML_code
=
"""
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body,html { font-family: Arial; font-size: 11pt; }
div.msg { margin: 0.2em; line-height: 1.4em; }
b { background: #ccc; font-weight: bold; font-size: 10pt;
padding: 0.1em 0.2em; }
b.Python { background: #eee; }
i { font-family: Courier new; font-size: 10pt; border: #eee 1px solid;
padding: 0.1em 0.2em; }
</style>
<script>
function js_print(lang, event, msg) {
msg = "<b class="+lang+">"+lang+": "+event+":</b> " + msg;
console = document.getElementById("console")
console.innerHTML += "<div class=msg>"+msg+"</div>";
}
function js_callback_1(ret) {
js_print("Javascript", "html_to_data_uri", ret);
}
function js_callback_2(msg, py_callback) {
js_print("Javascript", "js_callback", msg);
py_callback("String sent from Javascript");
}
window.onload = function(){
js_print("Javascript", "window.onload", "Called");
js_print("Javascript", "python_property", python_property);
js_print("Javascript", "navigator.userAgent", navigator.userAgent);
js_print("Javascript", "cefpython_version", cefpython_version.version);
html_to_data_uri("test", js_callback_1);
external.test_multiple_callbacks(js_callback_2);
};
</script>
</head>
<body>
<h1>Tutorial example</h1>
<div id="console"></div>
</body>
</html>
"""
def
main
():
check_versions
()
sys
.
excepthook
=
cef
.
ExceptHook
# To shutdown all CEF processes on error
cef
.
Initialize
()
set_global_handler
()
browser
=
cef
.
CreateBrowserSync
(
url
=
html_to_data_uri
(
HTML_code
),
window_title
=
"Tutorial"
)
set_client_handlers
(
browser
)
set_javascript_bindings
(
browser
)
cef
.
MessageLoop
()
cef
.
Shutdown
()
def
check_versions
():
print
(
"[tutorial.py] CEF Python {ver}"
.
format
(
ver
=
cef
.
__version__
))
print
(
"[tutorial.py] Python {ver} {arch}"
.
format
(
ver
=
platform
.
python_version
(),
arch
=
platform
.
architecture
()[
0
]))
assert
cef
.
__version__
>=
"49.0"
,
"CEF Python v49.0+ required to run this"
def
html_to_data_uri
(
html
,
js_callback
=
None
):
# This function is called in two ways:
# 1. From Python: in this case value is returned
# 2. From Javascript: in this case value cannot be returned because
# inter-process messaging is asynchronous, so must return value
# by calling js_callback.
html
=
html
.
encode
(
"utf-8"
,
"replace"
)
b64
=
base64
.
b64encode
(
html
).
decode
(
"utf-8"
,
"replace"
)
ret
=
"data:text/html;base64,{data}"
.
format
(
data
=
b64
)
if
js_callback
:
js_print
(
js_callback
.
GetFrame
().
GetBrowser
(),
"Python"
,
"html_to_data_uri"
,
"Called from Javascript. Will call Javascript callback now."
)
js_callback
.
Call
(
ret
)
else
:
return
ret
def
set_global_handler
():
# A global handler is a special handler for callbacks that
# must be set before Browser is created using
# SetGlobalClientCallback() method.
global_handler
=
GlobalHandler
()
cef
.
SetGlobalClientCallback
(
"OnAfterCreated"
,
global_handler
.
OnAfterCreated
)
def
set_client_handlers
(
browser
):
client_handlers
=
[
LoadHandler
(),
DisplayHandler
()]
for
handler
in
client_handlers
:
browser
.
SetClientHandler
(
handler
)
def
set_javascript_bindings
(
browser
):
external
=
External
(
browser
)
bindings
=
cef
.
JavascriptBindings
(
bindToFrames
=
False
,
bindToPopups
=
False
)
bindings
.
SetProperty
(
"python_property"
,
"This property was set in Python"
)
bindings
.
SetProperty
(
"cefpython_version"
,
cef
.
GetVersion
())
bindings
.
SetFunction
(
"html_to_data_uri"
,
html_to_data_uri
)
bindings
.
SetObject
(
"external"
,
external
)
browser
.
SetJavascriptBindings
(
bindings
)
def
js_print
(
browser
,
lang
,
event
,
msg
):
# Execute Javascript function "js_print"
browser
.
ExecuteFunction
(
"js_print"
,
lang
,
event
,
msg
)
class
GlobalHandler
(
object
):
def
OnAfterCreated
(
self
,
browser
,
**
_
):
"""Called after a new browser is created."""
# DOM is not yet loaded. Using js_print at this moment will
# throw an error: "Uncaught ReferenceError: js_print is not defined".
# We make this error on purpose. This error will be intercepted
# in DisplayHandler.OnConsoleMessage.
js_print
(
browser
,
"Python"
,
"OnAfterCreated"
,
"This will probably never display as DOM is not yet loaded"
)
# Delay print by 0.5 sec, because js_print is not available yet
args
=
[
browser
,
"Python"
,
"OnAfterCreated"
,
"(Delayed) Browser id="
+
str
(
browser
.
GetIdentifier
())]
threading
.
Timer
(
0.5
,
js_print
,
args
).
start
()
class
LoadHandler
(
object
):
def
OnLoadingStateChange
(
self
,
browser
,
is_loading
,
**
_
):
"""Called when the loading state has changed."""
if
not
is_loading
:
# Loading is complete. DOM is ready.
js_print
(
browser
,
"Python"
,
"OnLoadingStateChange"
,
"Loading is complete"
)
class
DisplayHandler
(
object
):
def
OnConsoleMessage
(
self
,
browser
,
message
,
**
_
):
"""Called to display a console message."""
# This will intercept js errors, see comments in OnAfterCreated
if
"error"
in
message
.
lower
()
or
"uncaught"
in
message
.
lower
():
# Prevent infinite recurrence in case something went wrong
if
"js_print is not defined"
in
message
.
lower
():
if
hasattr
(
self
,
"js_print_is_not_defined"
):
print
(
"Python: OnConsoleMessage: "
"Intercepted Javascript error: "
+
message
)
return
else
:
self
.
js_print_is_not_defined
=
True
# Delay print by 0.5 sec, because js_print may not be
# available yet due to DOM not ready.
args
=
[
browser
,
"Python"
,
"OnConsoleMessage"
,
"(Delayed) Intercepted Javascript error: <i>{error}</i>"
.
format
(
error
=
message
)]
threading
.
Timer
(
0.5
,
js_print
,
args
).
start
()
class
External
(
object
):
def
__init__
(
self
,
browser
):
self
.
browser
=
browser
def
test_multiple_callbacks
(
self
,
js_callback
):
"""Test both javascript and python callbacks."""
js_print
(
self
.
browser
,
"Python"
,
"test_multiple_callbacks"
,
"Called from Javascript. Will call Javascript callback now."
)
def
py_callback
(
msg_from_js
):
js_print
(
self
.
browser
,
"Python"
,
"py_callback"
,
msg_from_js
)
js_callback
.
Call
(
"String sent from Python"
,
py_callback
)
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.