class LockingView(LonaView): def handle_request(self, request): now = Span() self.message = Div('Button not clicked') self.button = Button('Button') self.html = Div( H2('Server State Locking View'), Div(Strong('Now: '), now), Br(), self.message, self.button, ) while True: self.server.state['now'] = str(datetime.now()) now.set_text(self.server.state['now']) self.show(self.html) self.sleep(1) def handle_input_event(self, input_event): with self.server.state.lock: self.message.set_text('Button clicked; Lock') self.show(self.html) self.sleep(2) self.message.set_text('Unlock') self.show(self.html)
def handle_request(self, request): div = Div() html = HTML( H2('Set Title'), A('Back', href='/'), div, ) self.show(html) while True: for i in range(3): title = 'Title {}'.format(i) div.set_text( "Using set_title(); Title should be '{}'".format(title)) self.set_title(title) self.show() self.sleep(1) for i in range(3): title = 'Title {}'.format(i) div.set_text("using show; Title should be '{}'".format(title)) self.show(html, title=title) self.sleep(1)
def handle_request(self, request): request_data = { 'GET': request.GET, 'POST': request.POST, } message = Div() html = HTML( H2('Form View'), Pre(pformat(request_data)), Button('Interactive GET', _id='interactive-get'), Button('Interactive POST', _id='interactive-post'), Br(), Button('Non Interactive GET', _id='non-interactive-get'), Button('Non Interactive POST', _id='non-interactive-post'), ) input_event = self.await_click(html=html) action = '.' method = 'post' if (input_event.node_has_id('non-interactive-get') or input_event.node_has_id('interactive-get')): method = 'get' html = HTML( H2('Form View'), message, Form( TextInput(name='text'), Submit('Submit'), method=method, action=action, ), ) if (input_event.node_has_id('non-interactive-get') or input_event.node_has_id('non-interactive-post')): message.set_text('View stopped') return html else: message.set_text('View waits for input events') self.await_input_event(html=html)
def handle_request(self, request, name='blank'): message = Div('View not started yet') log = Div() start = Button('Start') start_daemonized = Button('Start daemonized') html = Div( H2('Daemonized View (name={})'.format(name)), message, log, start, start_daemonized, ) input_event = self.await_input_event(html=html) if input_event.node == start_daemonized: self.daemonize() message.set_text('View started daemonized') else: message.set_text('View started normal') for i in range(15): log.set_text('Counting ({}/15)'.format(i + 1)) self.show(html) self.sleep(1) message.set_text('View stopped') self.show(html)
def handle_request(self, request): timestamp = Div() html = HTML( H1('Interactive View'), A('Home', href='/'), timestamp, ) while True: timestamp.set_text(str(datetime.now())) self.show(html) self.sleep(1)
class TestWidget(Widget): STATIC_FILES = [ Script(name='TestWidget', path='widget_data.js'), ] FRONTEND_WIDGET_CLASS = 'widget_data' def __init__(self): self.server_state = Div() self.nodes = [ self.server_state, Div(_id='client-state'), ] def update_state(self): self.server_state.set_text(dumps(self.data))
def handle_request(self, request): message = Div('Button not clicked') button = Button('Click me!') html = HTML( H1('Click the button!'), message, button, ) self.show(html) # this call blocks until the button was clicked input_event = self.await_click(button) if input_event.node == button: message.set_text('Button clicked') return html
class ClassBasedView(LonaView): def handle_request(self, request): self.message = Div('Nothing clicked yet') self.html = HTML( H2('Class Based View'), self.message, Br(), Button('handle_input_event_root()', _id='handle_input_event_root'), Button('handle_input_event()', _id='handle_input_event'), Button('handle_request()', _id='handle_request'), Br(), Button('Stop View', _id='stop_view')) while True: input_event = self.await_click( self.html[-3], self.html[-1], html=self.html, ) if input_event.node == self.html[-1]: self.message.set_text('View Stopped') self.show(self.html) return self.message.set_text('handled by handle_request()') def handle_input_event_root(self, input_event): if input_event.node_has_id('handle_input_event_root'): self.message.set_text('handled by handle_input_event_root()') else: return input_event def handle_input_event(self, input_event): if input_event.node_has_id('handle_input_event'): self.message.set_text('handled by handle_input_event()') else: return input_event