from pyhtmlgui import PyHtmlGui, PyHtmlView, Observable from exampleApp17Import import AppView class App(Observable): def __init__(self): super().__init__() self.value = 0 self.paused = False self.worker_thread = threading.Thread(target=self._worker_thread, daemon=True) self.worker_thread.start() def _worker_thread(self): while True: if self.paused is False: self.value = time.time() self.notifyObservers() time.sleep(1) def pause_restart(self): self.paused = not self.paused self.notifyObservers() if __name__ == "__main__": gui = PyHtmlGui( appInstance = App(), appViewClass = AppView, auto_reload=True ) gui.start(show_frontend=True, block=True)
def get_time(self): return time.time() if __name__ == "__main__": listen_host = "127.0.0.1" listen_port = 8001 secret = "i_am_secret" electron_exe = sys.argv[1] gui = PyHtmlGui( appInstance=App(), appViewClass=AppView, listen_host=listen_host, listen_port=listen_port, mode="electron", template_dir="templates", static_dir="static", main_html="window.html", shared_secret=secret, # must be the same in electron pyhtmlgui.json, ) if "launch_from_within_electron" in sys.argv: gui.start(show_frontend=False, block=True) else: # in a deployed app, set these value in package.json and launch electron.exe as your app so the all code below is unneccessary args = sys.argv.copy() args.append("launch_from_within_electron") env = os.environ.copy() env.update({ "PYHTMLGUI_HOST": listen_host,
def _worker_thread(self): while True: self.appview_value += 1 if self.is_visible is True: # if we call update ourself, we need to check visibility, we cant update invisible components. self.update() self.call_javascript("get_frontend_id", [])(self._frontend_feedback) time.sleep(1) def _frontend_feedback( self, values): # values containes results from all connected frontends, self.observedObject.connected_view_feedback[ self.appview_identifier] = values self.connected_frontend_feedback = values if __name__ == "__main__": gui = PyHtmlGui( appInstance=App(), appViewClass=AppView, auto_reload=True, static_dir="static", template_dir="templates", main_html="window.html", shared_secret=None, single_instance=False, # ) gui.start(show_frontend=True, block=True)
<button onclick="pyhtmlgui.call(this.exit)">Exit App</button> ''' def clicked(self): self.call_javascript( "create_random_string", ["my prefix"])(lambda x: print("Received result:", x)) def exit(self): self.call_javascript("electron.exit", [], skip_results=True) if __name__ == "__main__": electron_exe = sys.argv[1] app = App() gui = PyHtmlGui( appInstance=app, appViewClass=AppView, mode="electron", template_dir="templates", static_dir="static", main_html="window.html", executable=electron_exe, shared_secret= "shared_secret_replace_me", # must be the same in electron pyhtmlgui.json, on_frontend_ready=app.on_frontend_ready, on_frontend_exit=app.on_frontend_exit, ) gui.start(show_frontend=True, block=True)
self.echo_back_to_frontends ) # only call with callback of ignore result, don't call with () here, this will block the event loop # if you set shared_instance = True below, and open multile windows, # you will see feedback values from all open frontends here, else every connected frontend has its own AppView Instance def echo_back_to_frontends(self, values): #self.eval_javascript('''document.getElementById(args.uid).querySelector('[name="result"').innerHTML = args.innerHTML;''', skip_results=True, uid=self.uid, innerHTML="<br>".join(values)) self.call_javascript("pyhtmlgui.eval_script", [ '''document.getElementById(args.uid).querySelector('[name="result"').innerHTML = args.innerHTML;''', { "uid": self.uid, "innerHTML": "<br>".join(values) } ], skip_results=True) if __name__ == "__main__": gui = PyHtmlGui( appInstance=App(), appViewClass=AppView, static_dir="examples/static", template_dir="examples/templates", main_html="window.html", auto_reload= True, # Debug only, if you changed the class template_str or files, the frontend will update views on rumtime. try editing app.html while app is running and waid few seconds single_instance= True, # if you set this to False, open multiple windows, see one AppView connected to multiple frontends, ) gui.start(show_frontend=True, block=True)
def ping(self): self.call_javascript("electron.ping", [])(self.set_result) def set_result(self, values): self.eval_javascript( "document.getElementById('electron_ping_result').innerHTML = args.value", value=json.dumps(values)) def get_time(self): return time.time() def _exit(): print("FRONTEND EXIT CALLBACK") exit(0) if __name__ == "__main__": package_json = json.loads( open(os.path.join(os.path.dirname(__file__), "package.json"), "r").read()) # so electron and we use the same values gui = PyHtmlGui(mode="electron", appInstance=App(), appViewClass=AppView, listen_host=package_json["PYHTMLGUI_HOST"], listen_port=package_json["PYHTMLGUI_PORT"], shared_secret=package_json["PYHTMLGUI_SECRET"], on_frontend_exit=_exit) gui.start(show_frontend=False, block=True)