Tutorial for flexx.app - connecting to the browser


In [1]:
from flexx import flx


[I 22:27:08 flexx.app] Asset store collected 2 new modules.

In normal operation, one uses flx.launch() to fire up a browser (or desktop app) to run the JavaScript in. This is followed by flx.run() (or flx.start() for servers) to enter Flexx' main loop.

In the notebook, however, there already is a browser. To tell Flexx that we're in the notebook, use flx.init_notebook() at the start of your notebook. Since Flexx's event system is based on asyncio, we need to "activate" asyncio as well.


In [2]:
%gui asyncio
flx.init_notebook()


Flexx is ready for use

In [3]:
class MyComponent(flx.JsComponent):

    foo = flx.StringProp('', settable=True)
        
    @flx.reaction('foo')
    def on_foo(self, *events):
        if self.foo:
            window.alert('foo is ' + self.foo, + len(events))

In [4]:
m = MyComponent()



In [5]:
m.set_foo('helo')


Out[5]:
<JsComponent 'MyComponent_2' at 0x1dd22f0d668>

Let's use an example model:


In [6]:
from flexxamples.testers.find_prime import PrimeFinder
p = PrimeFinder()



In [7]:
p.find_prime_py(2000)


Out[7]:
<PyComponent 'PrimeFinder_3' at 0x1dd22f02c50>
17389 found in  0.5967290135927767 seconds

In [8]:
p.find_prime_js(2000)  # Result is written to JS console, open F12 to see it


Out[8]:
<PyComponent 'PrimeFinder_3' at 0x1dd22f02c50>

In [ ]: