class ToDoApp(MainApp): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.input = Input() self.add = Button('Add') self.taskentry = HBox() self.taskentry.children = [ self.input, self.add ] self.tasksarea = VBox() self.delete = Button('Delete') self.children = [ self.taskentry, self.tasksarea, self.delete ] self.add.register("click", self.on_add) self.delete.register("click", self.on_delete) def on_add(self, source): self.tasksarea.children.append( TaskItem(self.input.value) ) def on_delete(self, source): self.tasksarea.children = [ item for item in self.tasksarea.children if not item.chk.checked ]
class TodoListWidget(VBox): def __init__(self): super().__init__() self.entry = HBox() self.input = Input() self.entry.children = [self.input] self.todolist = VBox() self.clear_button = Button("Clear Completed") self.children = [self.entry, self.todolist, self.clear_button] # This form of attaching an event handler is for "secondary" # hanlers. Primary handlers are attached with @event_handler, # which are for events of its own widget. The primary handler # calls the secondary handlers. This is a secondary handler # beause it is an event of self.input, not self. self.input.register('change', self.on_add_todo) self.clear_button.register('click', self.on_clear_completed) def on_add_todo(self, source, message): new_todo = TodoWidget(self.input.value) self.todolist.children.append(new_todo) print(f"Added {repr(new_todo)}") def on_clear_completed(self, source, message): print(f'{self.__class__.__name__}.on_clear_completed({repr(source)}') for todo in self.todolist.children: if todo.completed: self.todolist.children.remove(todo)
class ExampleApp(MainApp): """ The cookie 'sessionid' is checked and created if non-existent by MainHandler.get(), which is invoked when the body of the page is requested. I believe it is not possible to set cookies via websockets and that is why it is done that way. At the time an instance of the MainApp exists, the 'sessionid' cookie must exist as well. """ def __init__(self, connection): super().__init__(connection) self.button = Button() self.inbox = Input() self.box1 = HBox() self.box1.children = [self.button, self.inbox] self.children = [self.box1] self.button.register('click', self.on_button_click) def on_button_click(self, source, message): print(f'{self.__class__.__name__}.on_button_click()') self.inbox.value = f"{self.connection.get_cookie('sessionid')}"
def __init__(self, connection): super().__init__(connection) self.button = Button("Swap", style={'margin': '3px'}) self.secbox = HBox() st = { 'width': '300px', 'padding': '2px', 'border': '1px solid gray', 'margin': '3px' } self.left = VBox(style=st) self.right = VBox(style=st) self.secbox.children = [self.left, self.right] self.labelA = Label("Label A") self.labelB = Label("label B") self.left.children = [self.labelA] self.right.children = [self.labelB] self.children = [self.button, self.secbox] self.button.register('click', self.on_swap)
def __init__(self, connection): super().__init__(connection) self.btn_go2a = Button(label='Go to A') self.chk = CheckBox() self.btn = Button() self.redir = Redirect() self.children = [self.btn_go2a, self.chk, self.btn, self.redir] self.btn_go2a.register("click", self.on_go2a) self.btn.register("click", self.on_btn_click)
def __init__(self, connection): super().__init__(connection) self.chk = CheckBox() self.btn = Button() self.children = [self.chk, self.btn] self.btn.register("click", self.on_btn_click)
def __init__(self, connection): super().__init__(connection) self.count = 0 self.button = Button() self.children = [ self.button, ] self.button.register('click', self.on_button_click)
def __init__(self, connection): super().__init__(connection) self.btn_go2b = Button(label='Go to B') self.btn = Button() self.input = Input(value='The value') self.redir = Redirect() self.children = [self.btn_go2b, self.btn, self.input, self.redir] self.btn.register('click', self.on_button_click) self.btn_go2b.register("click", self.on_go2b)
class AuthenticationApp(MainApp): def __init__(self, connection): super().__init__(connection) self.label = Label('Password') self.pwinput = Input() self.btn = Button('Login') # TODO: If not a child, should raise error when calling redirect(). self.redir = Redirect() self.status = Label('Please enter your password.') lg = HBox() lg.children = [ self.label, self.pwinput, self.btn, self.redir ] vb = VBox() vb.children = [ lg, self.status ] self.children = [ vb ] self.btn.register("click", self.on_click) # ---- Authentication Verification ---- # If authenticated, redirect immediately to the # main (restricted app). sid = connection.get_cookie('sessionid') session = connection.application.get_session(sid) print(f'[{__class__.__name__}] Session: {session}') if session.authenticated: self.redir.redirect('/b') # ------------------------------------- def on_click(self, source, message): self.status.value = '' if self.pwinput.value == "password": sid = self.connection.get_cookie('sessionid') session = self.connection.application.get_session(sid) session.authenticated = True self.redir.redirect('/b') self.status.value = 'Success' # This works??? else: self.status.value = "Incorrect password. Try again."
def __init__(self, connection): super().__init__(connection) self.button = Button() self.inbox = Input() self.box1 = HBox() self.box1.children = [self.button, self.inbox] self.children = [self.box1] self.button.register('click', self.on_button_click)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.input = Input() self.add = Button('Add') self.taskentry = HBox() self.taskentry.children = [self.input, self.add] self.tasksarea = VBox() self.delete = Button('Delete') self.children = [self.taskentry, self.tasksarea, self.delete]
def __init__(self, connection): super().__init__(connection) self.btn_go2a = Button(label='Go to A') self.redir = Redirect() self.children = [ self.btn_go2a, HTML('<img src="/b/assets/image_blue.png"/>'), HTML('<img src="/image.png"/>'), self.redir ] self.btn_go2a.register("click", self.on_go2a)
def __init__(self, connection): super().__init__(connection) self.btn_go2b = Button(label='Go to B') self.redir = Redirect() self.children = [ self.btn_go2b, HTML('<img src="/a/assets/image_orange.png"/>'), HTML('<img src="/image.png"/>'), self.redir ] self.btn_go2b.register("click", self.on_go2b)
class ExampleApp(MainApp): def __init__(self, connection): super().__init__(connection) self.count = 0 self.button = Button() self.children = [ self.button, ] self.button.register('click', self.on_button_click) def on_button_click(self, source, message): print(f'{self.__class__.__name__}.on_button_click()') self.count += 1 self.children.append(Label(value=f'{self.count}'))
class ExampleAppB(MainApp): def __init__(self, connection): super().__init__(connection) self.btn_go2a = Button(label='Go to A') self.chk = CheckBox() self.btn = Button() self.redir = Redirect() self.children = [self.btn_go2a, self.chk, self.btn, self.redir] self.btn_go2a.register("click", self.on_go2a) self.btn.register("click", self.on_btn_click) def on_btn_click(self, source, message): print(f'Checked? {self.chk.checked}') self.chk.checked = True def on_go2a(self, source, message): self.redir.redirect('/a')
class ExampleAppA(MainApp): def __init__(self, connection): super().__init__(connection) self.btn_go2b = Button(label='Go to B') self.btn = Button() self.input = Input(value='The value') self.redir = Redirect() self.children = [self.btn_go2b, self.btn, self.input, self.redir] self.btn.register('click', self.on_button_click) self.btn_go2b.register("click", self.on_go2b) def on_button_click(self, source, message): self.input.value = "Hello!" def on_go2b(self, source, message): self.redir.redirect('/b')
def __init__(self, connection): super().__init__(connection) self.button = Button() self.progress = ProgressBar() self.progress.value = 10 self.box1 = HBox() self.box1.children = [ self.button, self.progress ] self.children = [ self.box1 ] self.button.register('click', self.on_button_click)
def __init__(self): super().__init__() self.entry = HBox() self.input = Input() self.entry.children = [self.input] self.todolist = VBox() self.clear_button = Button("Clear Completed") self.children = [self.entry, self.todolist, self.clear_button] # This form of attaching an event handler is for "secondary" # hanlers. Primary handlers are attached with @event_handler, # which are for events of its own widget. The primary handler # calls the secondary handlers. This is a secondary handler # beause it is an event of self.input, not self. self.input.register('change', self.on_add_todo) self.clear_button.register('click', self.on_clear_completed)
class ExampleApp(MainApp): def __init__(self, connection): super().__init__(connection) self.chk = CheckBox() self.btn = Button() self.children = [self.chk, self.btn] self.btn.register("click", self.on_btn_click) def on_btn_click(self, source, message): """ Callback for the button's click event. These always receive the source widget and the client message with the event. In this case source is self.btn. """ print(f'Checked? {self.chk.checked}') self.chk.checked = True
def __init__(self, connection): super().__init__(connection) # Nothing is sent to the browser during the construction. # However, as soon as we start adding children, messages # announcing the children are being queued for delivery. # Only when this widget is ready on the browser side, # these messages will be sent. # Note: This "serial" delivery is slow and probably # unnecessary. self.children = [Button(), Input(value='The value')] self.children[0].register('click', self.on_button_click)
def __init__(self, connection): super().__init__(connection) self.chart = Image() self.button1 = Button('Button 1') self.button2 = Button('Button 2') self.button3 = Button('Button 3') self.buttonbox = HBox() self.buttonbox.children = [self.button1, self.button2, self.button3] self.button1.register('click', self.on_btn1) self.button2.register('click', self.on_btn2) self.button3.register('click', self.on_btn3) self.children = [self.chart, self.buttonbox] self.figure, self.ax = plt.subplots(1, 1, figsize=(6, 4), dpi=100) x = np.linspace(0, 10, num=100) y = np.sin(x) image = BytesIO() self.ax.plot(x, y) self.ax.grid() self.figure.savefig(image, format='png') w, h = self.figure.get_size_inches() * self.figure.dpi self.chart.attr({'width': str(w), 'height': str(h)}) self.chart.data = base64.encodebytes(image.getvalue()).decode('utf8')
def __init__(self, connection): super().__init__(connection) self.label = Label('Password') self.pwinput = Input() self.btn = Button('Login') # TODO: If not a child, should raise error when calling redirect(). self.redir = Redirect() self.status = Label('Please enter your password.') lg = HBox() lg.children = [ self.label, self.pwinput, self.btn, self.redir ] vb = VBox() vb.children = [ lg, self.status ] self.children = [ vb ] self.btn.register("click", self.on_click) # ---- Authentication Verification ---- # If authenticated, redirect immediately to the # main (restricted app). sid = connection.get_cookie('sessionid') session = connection.application.get_session(sid) print(f'[{__class__.__name__}] Session: {session}') if session.authenticated: self.redir.redirect('/b')
def __init__(self, connection): # Widget.__init__: A lot of stuff. # MainApp.__init__: self.connection = connection super().__init__(connection) # #### Authentication Verification ##### sid = connection.get_cookie('sessionid') session = connection.application.get_session(sid) print(f'[{__class__.__name__}] Session: {session}') if not session.authenticated: self.children = [Label('Not authorized')] # Will never happen connection.close() return # ### Done with authentication verification #### self.button = Button() self.inbox = Input() self.box1 = HBox() self.box1.children = [ self.button, self.inbox ] self.logoutbtn = Button('Log out') self.box2 = VBox() self.box2.children = [ Label("This the main app. You have been successfully authenticated."), self.box1, self.logoutbtn ] self.redir = Redirect() self.children = [self.box2, self.redir] self.button.register('click', self.on_button_click) self.logoutbtn.register('click', self.on_logout)
class ExampleAppB(MainApp): assets_path = { 'to': 'assets', 'from': f'{Path(__file__).parent.absolute()}/data' } def __init__(self, connection): super().__init__(connection) self.btn_go2a = Button(label='Go to A') self.redir = Redirect() self.children = [ self.btn_go2a, HTML('<img src="/b/assets/image_blue.png"/>'), HTML('<img src="/image.png"/>'), self.redir ] self.btn_go2a.register("click", self.on_go2a) def on_go2a(self, source, message): self.redir.redirect('/a')
class ExampleApp(MainApp): def __init__(self, connection): super().__init__(connection) self.button = Button() self.progress = ProgressBar() self.progress.value = 10 self.box1 = HBox() self.box1.children = [ self.button, self.progress ] self.children = [ self.box1 ] self.button.register('click', self.on_button_click) def on_button_click(self, source, message): print(f'{self.__class__.__name__}.on_button_click()') self.progress.value += 10
class MPLApp(MainApp): """ Images and dynamic update. Specifically, we plot some curves using the Matplotlib plotting library. """ def __init__(self, connection): super().__init__(connection) self.chart = Image() self.button1 = Button('Button 1') self.button2 = Button('Button 2') self.button3 = Button('Button 3') self.buttonbox = HBox() self.buttonbox.children = [self.button1, self.button2, self.button3] self.button1.register('click', self.on_btn1) self.button2.register('click', self.on_btn2) self.button3.register('click', self.on_btn3) self.children = [self.chart, self.buttonbox] self.figure, self.ax = plt.subplots(1, 1, figsize=(6, 4), dpi=100) x = np.linspace(0, 10, num=100) y = np.sin(x) image = BytesIO() self.ax.plot(x, y) self.ax.grid() self.figure.savefig(image, format='png') w, h = self.figure.get_size_inches() * self.figure.dpi self.chart.attr({'width': str(w), 'height': str(h)}) self.chart.data = base64.encodebytes(image.getvalue()).decode('utf8') def on_btn1(self, source, message): self.ax.clear() x = np.linspace(0, 10, num=100) y = np.sin(2 * x) image = BytesIO() self.ax.plot(x, y) self.ax.grid() self.figure.savefig(image, format='png') self.chart.data = base64.encodebytes(image.getvalue()).decode('utf8') def on_btn2(self, source, message): self.ax.clear() x = np.linspace(0, 10, num=100) y = np.sin(x) image = BytesIO() self.ax.plot(x, y) self.ax.grid() self.figure.savefig(image, format='png') self.chart.data = base64.encodebytes(image.getvalue()).decode('utf8') def on_btn3(self, source, message): w, h = self.figure.get_size_inches() * self.figure.dpi self.chart.attr({'width': str(w), 'height': str(h)})
class ExampleApp(MainApp): """ The cookie 'sessionid' is checked and created if non-existent by MainHandler.get(), which is invoked when the body of the page is requested. I think it is not possible to set cookies via websockets and that is why it is done that way. At the time an instance of the MainApp exists, the 'sessionid' cookie does exist as well. """ def __init__(self, connection): # Widget.__init__: A lot of stuff. # MainApp.__init__: self.connection = connection super().__init__(connection) # #### Authentication Verification ##### sid = connection.get_cookie('sessionid') session = connection.application.get_session(sid) print(f'[{__class__.__name__}] Session: {session}') if not session.authenticated: self.children = [Label('Not authorized')] # Will never happen connection.close() return # ### Done with authentication verification #### self.button = Button() self.inbox = Input() self.box1 = HBox() self.box1.children = [ self.button, self.inbox ] self.logoutbtn = Button('Log out') self.box2 = VBox() self.box2.children = [ Label("This the main app. You have been successfully authenticated."), self.box1, self.logoutbtn ] self.redir = Redirect() self.children = [self.box2, self.redir] self.button.register('click', self.on_button_click) self.logoutbtn.register('click', self.on_logout) # def on_inbox_change(self): # print(f'{self.__class__.__name__}.on_inbox_change()') def on_button_click(self, source, message): # print(f'{self.__class__.__name__}.on_button_click()') self.inbox.value = f"{self.connection.get_cookie('sessionid')}" def on_logout(self, source, message): sid = self.connection.get_cookie('sessionid') del self.connection.application.sessions[sid] self.redir.redirect('/a')