import webview import threading import time import sys import random """ This example demonstrates how to create a pywebview api without using a web server """ html = """

JS API Example





""" class Api: def __init__(self): self.cancel_heavy_stuff_flag = False def init(self, params): response = { 'message': 'Hello from Python {0}'.format(sys.version) } return response def getRandomNumber(self, params): response = { 'message': 'Here is a random number courtesy of randint: {0}'.format(random.randint(0, 100000000)) } return response def doHeavyStuff(self, params): time.sleep(0.1) # sleep to prevent from the ui thread from freezing for a moment now = time.time() self.cancel_heavy_stuff_flag = False for i in range(0, 1000000): _ = i * random.randint(0, 1000) if self.cancel_heavy_stuff_flag: response = {'message': 'Operation cancelled'} break else: then = time.time() response = { 'message': 'Operation took {0:.1f} seconds on the thread {1}'.format((then - now), threading.current_thread()) } return response def cancelHeavyStuff(self, params): time.sleep(0.1) self.cancel_heavy_stuff_flag = True def sayHelloTo(self, params): response = { 'message': 'Hello {0}!'.format(params) } return response def create_app(): webview.load_html(html) if __name__ == '__main__': t = threading.Thread(target=create_app) t.start() api = Api() webview.create_window('API example', js_api=api)