Skip to content

Instantly share code, notes, and snippets.

@hkucuk
Last active July 16, 2019 20:03
Show Gist options
  • Save hkucuk/2fe8c9c5b6ff854ac8d1703b78c041cf to your computer and use it in GitHub Desktop.
Save hkucuk/2fe8c9c5b6ff854ac8d1703b78c041cf to your computer and use it in GitHub Desktop.
pywebview API sample
import random
import webview
html = """
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>JS API Example</h1>
<button onClick="getRandomNumber()">Get Random Number</button><br/>
<div id="response-container"></div>
<script>
function showResponse(response) {
var container = document.getElementById('response-container')
container.innerText = response.message
container.style.display = 'block'
}
function getRandomNumber() {
pywebview.api.getRandomNumber().then(showResponse)
}
</script>
</body>
</html>
"""
class Api:
def getRandomNumber(self, params):
response = {
'message': 'Here is a random number courtesy of randint: {0}'.format(random.randint(0, 100000000))
}
return response
if __name__ == '__main__':
api = Api()
window = webview.create_window('API example', html=html, js_api=api)
webview.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment