forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add javascript_bindings.py snippet (cztomczak#403) and others.
Add cef.GetDataUrl(). Update README-examples.md - add Snippets section. Update PyInstaller example.
- Loading branch information
Showing
13 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
Communicate between Python and Javascript asynchronously using | ||
inter-process messaging with the use of Javascript Bindings. | ||
""" | ||
|
||
from cefpython3 import cefpython as cef | ||
|
||
g_htmlcode = """ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<style> | ||
body, html { | ||
font-family: Arial; | ||
font-size: 11pt; | ||
} | ||
</style> | ||
<script> | ||
function print(msg) { | ||
console.log(msg+" [JS]"); | ||
document.getElementById("console").innerHTML += msg+"<br>"; | ||
} | ||
function js_function(value) { | ||
print("Value sent from Python: <b>"+value+"</b>"); | ||
py_function("I am a Javascript string #1", js_callback); | ||
} | ||
function js_callback(value, py_callback) { | ||
print("Value sent from Python: <b>"+value+"</b>"); | ||
py_callback("I am a Javascript string #2"); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>Javascript Bindings</h1> | ||
<div id=console></div> | ||
</body> | ||
</html> | ||
""" | ||
|
||
|
||
def main(): | ||
cef.Initialize() | ||
browser = cef.CreateBrowserSync(url=cef.GetDataUrl(g_htmlcode), | ||
window_title="OnBeforeClose") | ||
browser.SetClientHandler(LifespanHandler()) | ||
bindings = cef.JavascriptBindings() | ||
bindings.SetFunction("py_function", py_function) | ||
bindings.SetFunction("py_callback", py_callback) | ||
browser.SetJavascriptBindings(bindings) | ||
cef.MessageLoop() | ||
del browser | ||
cef.Shutdown() | ||
|
||
|
||
def py_function(value, js_callback): | ||
print("Value sent from Javascript: "+value) | ||
js_callback.Call("I am a Python string #2", py_callback) | ||
|
||
|
||
def py_callback(value): | ||
print("Value sent from Javascript: "+value) | ||
|
||
|
||
class LifespanHandler(object): | ||
def OnLoadEnd(self, browser, **_): | ||
browser.ExecuteFunction("js_function", "I am a Python string #1") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters