Created
November 13, 2018 12:06
-
-
Save kim0/638122fa66c7f1f79720743e75ffe8ec to your computer and use it in GitHub Desktop.
First day with flexx, trying to invoke py method from JS
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
from flexx import flx | |
class RootUI(flx.Widget): # Using Widget as this should hold the UI! | |
def init(self, rpy): | |
super().init() | |
self.rpy = rpy # Capture the remote-python side | |
with flx.VBox(): | |
with flx.HBox(): | |
self.filename = flx.LineEdit(placeholder_text='foo.txt') | |
self.filecontent = flx.LineEdit(placeholder_text='Hello cruel world') | |
with flx.HBox(): | |
self.but = flx.Button(text='Reset') | |
self.btnWrite = flx.Button(text='Write') | |
self.label = flx.Label(flex=1) | |
@flx.reaction('filename.text', 'filecontent.text') | |
def greet(self, *events): | |
self.label.set_text('hi ' + self.filename.text + ' ' + self.filecontent.text) | |
@flx.reaction('but.pointer_click') | |
def reset(self, *events): | |
self.label.set_text('') | |
@flx.reaction('btnWrite.pointer_click') | |
def remote_write(self, *events): | |
self.rpy.write(self.filename, self.filecontent) # Call write method in remote-python | |
class Rpy(flx.Component): | |
def init(self): | |
pass | |
def write(self, fname, fcontent): | |
with open(fname, 'w') as f: | |
f.write(fcontent) | |
print("WROTE!!!") | |
rpy = Rpy() | |
if __name__ == '__main__': | |
a = flx.App(RootUI(rpy)) # Pass a remote python object | |
a.serve() | |
flx.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment