async def test_sharing_state_between_sessions(): """ 7 7 42 42 ---------- 7 7 42 42 """ # Test sharing state between multiple sessions class SharedComponent(event.Component): foo = event.IntProp(0, settable=True) shared = SharedComponent() # This lambda thingy at a PyComponent is the magic to share state # Note that this needs to be setup for each property. It would be nice # to really share a component (proxy), but this would mean that a # PyComponent could have multiple sessions, which would complicate things # too much to be worthwhile. c1 = app.App(PyComponentA, foo=lambda: shared.foo).launch() c2 = app.App(PyComponentA, foo=lambda: shared.foo).launch() s1, s2 = c1.session, c2.session with c1: c11 = JsComponentA() with c2: c22 = JsComponentA() await roundtrip(s1, s2) shared.set_foo(7) await roundtrip(s1, s2) print(c1.foo) s1.send_command('EVAL', c1.id, 'foo') await roundtrip(s1, s2) print(c2.foo) s2.send_command('EVAL', c2.id, 'foo') shared.set_foo(42) await roundtrip(s1, s2) print(c1.foo) s1.send_command('EVAL', c1.id, 'foo') await roundtrip(s1, s2) print(c2.foo) s2.send_command('EVAL', c2.id, 'foo') await roundtrip(s1, s2)
def test_launching_with_props(): m = app.launch(MyPropClass1) assert m.foo == 1 m.session.close() m = app.App(MyPropClass1, foo=3).launch() assert m.foo == 3 m.session.close()
def test_launching_with_init_args(): m = app.launch(MyPropClass2) assert m.foo == 11 m.session.close() m = app.App(MyPropClass2, 13).launch() assert m.foo == 13 m.session.close()
# Set connections n = info.sessions, info.total_sessions self.status.set_html('There are %i connected clients.<br />' % n[0] + 'And in total we served %i connections.<br />' % n[1]) # Prepare plots times = list(self.cpu_plot.xdata) times.append(time() - self.start_time) times = times[-self.nsamples:] # cpu data usage = list(self.cpu_plot.ydata) usage.append(info.cpu) usage = usage[-self.nsamples:] self.cpu_plot.set_data(times, usage) # mem data usage = list(self.mem_plot.ydata) usage.append(info.mem) usage = usage[-self.nsamples:] self.mem_plot.set_data(times, usage) if __name__ == '__main__': a = app.App(Monitor) a.serve() # m = a.launch('browser') # for use during development app.start()
for p in sessions] # _chatroom_name is what we set del sessions text = '<br />%i persons in this chat:<br /><br />' % len(names) text += '<br />'.join([name or 'anonymous' for name in sorted(names)]) self.people.text = text app.call_later(3, self._update_participants) @event.connect('ok.mouse_down', 'message.submit') def _send_message(self, *events): text = self.message.text if text: name = self.name.text or 'anonymous' relay.new_message('<i>%s</i>: %s' % (name, text)) self.message.text = '' @event.connect('name.text') def _name_changed(self, *events): self.session._chatroom_name = self.name.text class JS: @event.connect('!new_message') def _update_total_text(self, *events): self.messages.text += ''.join([ev.msg for ev in events]) if __name__ == '__main__': a = app.App(ChatRoom, title='Flexx chatroom demo') a.serve() # a.launch() # for use during development app.start()
password_mode=True) self.b1 = ui.Button(text='提交') self.l4 = ui.Label(text='') self.l5 = ui.Label(text='') ui.Widget(flex=1) self.decrypt = ui.Widget(title='解密') with self.decrypt: pass @event.reaction('b1.pointer_down') def calculate_encryp(self, *events): if self.e1.text == '' or self.e2.text == '' or self.e3.text == '': self.l4.set_text('需加密的文本或密匙不能为空!') elif len(self.e1.text) > 10000: self.l4.set_text('加密文本字符数应小于10000!') elif self.e2.text != self.e3.text: self.l4.set_text('两次密码输入不一致!') else: self.l4.set_text('加密密文为:') encrypt_str = encrypt_api.encryption(self.e1.text, self.e2.text) self.l5.set_text(encrypt_str) if __name__ == '__main__': ##发布为网页服务时使用 #app.create_server(host="0.0.0.0",port=12358) #app.App(encrypt_decrypt, title='对称加密解密程序').serve() #app.start() ##桌面程序上面三句话不需要,用下面两句话 app.App(encrypt_decrypt, title='对称加密解密程序').launch('app', size=(900, 400)) app.run()
def init(self): ui.Button(text='Not much to see here ...') class Icons2(ui.Widget): def init(self): self.set_title('Icon demo') self.set_icon(black_png) ui.Button(text='Not much to see here ...') if __name__ == '__main__': # Select application icon. Can be a url, a relative url to a shared asset, # a base64 encoded image, or a local filename. Note that the local filename # works for setting the aplication icon in a desktop-like app, but not for # a web app. File types can be ico or png. # << Uncomment any of the lines below >> # icon = None # use default # icon = 'https://assets-cdn.github.com/favicon.ico' # icon = app.assets.add_shared_data('ico.icon', open(fname, 'rb').read()) icon = black_png # icon = fname m = app.App(Icons1, title='Icon demo', icon=icon).launch('app') app.start()
get_img_blob(random.choice(image_names))) class Example(app.PyComponent): def init(self): # Randomly select image - different between sessions link2 = self.session.add_data('image.png', get_img_blob(random.choice(image_names))) # Create widget to show images View(link1, link2) class View(ui.Label): def init(self, link1, link2): html = '<p>Hit F5 to reload the page (i.e. create a new session' html += ', unless this is an exported app)</p>' html += '<p>This is session "%s"</p>' % self.session.id html += '<img src="%s" />' % link1 html += '<img src="%s" />' % link2 self.set_text(html) if __name__ == '__main__': # Launch the app twice to show how different sessions have different data a = app.App(Example) m1 = a.launch('browser') m2 = a.launch('browser') app.run()
def _send_message(self, *events): text = self.msg_edit.text if text: name = self.name.text or 'anonymous' relay.create_message('<i>%s</i>: %s' % (name, text)) self.msg_edit.set_text('') @relay.reaction('create_message') # note that we connect to relay def _push_info(self, *events): for ev in events: self.messages.add_message(ev.message) @app.manager.reaction('connections_changed') def _update_participants(self, *events): if self.session.status: # Query the app manager to see who's in the room sessions = app.manager.get_connections(self.session.app_name) names = [s.app.name_edit.text for s in sessions] del sessions text = '<br />%i persons in this chat:<br /><br />' % len(names) text += '<br />'.join( [name or 'anonymous' for name in sorted(names)]) self.people_label.set_text(text) if __name__ == '__main__': a = app.App(ChatRoom) a.serve() # a.launch() # for use during development app.start()
def launch(cls, *args, **kwargs): """ Shorthand for app.launch() that also returns session. """ # from flexx import app c = app.App(cls, *args, **kwargs).launch('firefox-app') return c, c.session
ylabel=u'temperature (°C)') ui.Widget(flex=1) @event.reaction def _update_plot(self): smoothing = self.smoothing.value yy1 = data[self.month.selected_index] yy2 = [] sm2 = int(smoothing / 2) for i in range(len(yy1)): val = 0 n = 0 for j in range(max(0, i - sm2), min(len(yy1), i + sm2 + 1)): val += yy1[j] n += 1 if n == 0: yy2.append(yy1[i]) else: yy2.append(val / n) self.plot.set_data(self.plot.xdata, yy2) if __name__ == '__main__': a = app.App(Twente, title='Temperature 1951 - 2014', style='background:#eee;') m = a.launch('app', size=(900, 400)) app.run()
# doc-export: Demo """ A demo with few lines of code with some fancy widgets, which works as an exported app, so it can be embedded e.g. on the main page. """ from flexx import ui, app from flexx.ui.examples.splines import Splines from flexx.ui.examples.twente import Twente from flexx.ui.examples.dock import Dock from flexx.ui.examples.drawing import Drawing class Demo(ui.Widget): def init(self): with ui.TabPanel(): Splines(title='Spline demo') Twente(title='Temperature vis') Dock(title='Dock panel demo') Drawing(title='Drawing app') if __name__ == '__main__': m = app.App(Demo, title='Flexx demo').launch('firefox') app.run()
ui.Widget(flex=1) # Init context to draw to self._ctx = self.canvas.node.getContext('2d') @event.reaction def __update_color(self): self.canvas.apply_style('border: 10px solid ' + self.model.color.hex) @event.reaction('canvas.mouse_down') def __on_click(self, *events): for ev in events: self.model.add_paint(ev.pos) @event.action def add_paint_to_canvas(self, pos, color): """ Actually draw a dot on the canvas. """ self._ctx.globalAlpha = 0.8 self._ctx.beginPath() self._ctx.fillStyle = color self._ctx.arc(pos[0], pos[1], 5, 0, 6.2831) self._ctx.fill() if __name__ == '__main__': a = app.App(ColabPainting) a.serve() # m = a.launch('browser') # for use during development app.start()
self.set_transform() #self.strokeWidth = 0.25 if self._zoom > 1 else 1 self.strokeWidth = 1 / self._zoom self.cursorSize = 2 / self._zoom self.move_cursor(self._last_cursor_pos) if not self._do_drawing: window.requestAnimationFrame(self.draw) def clear(self): self._line_paths = [] self._cursor_paths = [] if not self._do_drawing: window.requestAnimationFrame(self.draw) def move(self, x, y): self._position = (self._mouse_down_position[0] + x, self._mouse_down_position[1] + y) self.set_transform() if not self._do_drawing: window.requestAnimationFrame(self.draw) #config.hostname = 'localhost' #config.port = 80 a = app.App(AppRoot) #a.serve() #app.start() #a.export(filename='C:/Users/Andi/Downloads/AppRoot.html') a.launch() app.run()
# doc-export: Demo """ A demo with few lines of code with some fancy widgets, which works as an exported app, so it can be embedded e.g. on the main page. """ from flexx import app, ui from flexx.ui.examples.splines import Splines from flexx.ui.examples.twente import Twente from flexx.ui.examples.drawing import Drawing class Demo(ui.Widget): def init(self): with ui.TabLayout(): Splines(title='Spline demo') Twente(title='Temperature vis') Drawing(title='Drawing app') ui.YoutubeWidget(title='Video') if __name__ == '__main__': m = app.App(Demo, title='Flexx demo').launch() app.run()
CSS = """ .flx-Circle { background: #f00; border-radius: 10px; width: 10px; height: 10px; } """ class Circles(ui.Widget): def init(self): with ui.PinboardLayout(): self._circles = [Circle() for i in range(32)] self.tick() def tick(self): global Math, window t = time() for i, circle in enumerate(self._circles): x = Math.sin(i * 0.2 + t) * 30 + 50 y = Math.cos(i * 0.2 + t) * 30 + 50 circle.apply_style(dict(left=x + '%', top=y + '%')) window.setTimeout(self.tick, 30) if __name__ == '__main__': m = app.App(Circles).launch('app') app.run()