This is the demo that I used during the EuroScipy 2015 talk on Flexx.
Launch a web runtime. Can be a browser or something that looks like a desktop app.
In [ ]:
from flexx.webruntime import launch
rt = launch('http://flexx.rtfd.org', 'xul', title='Test title')
In [ ]:
from flexx.pyscript import py2js
In [ ]:
print(py2js('square = lambda x: x**2'))
In [ ]:
def foo(n):
res = []
for i in range(n):
res.append(i**2)
return res
print(py2js(foo))
In [ ]:
def foo(n):
return [i**2 for i in range(n)]
print(py2js(foo))
Reactive programming uses signals to communicate between different components of an app, and provides easy ways to react to changes in the values of these signals.
The API for flexx.react consists of a few decorators to turn functions into signals. One signal is the input signal.
In [ ]:
from flexx import react
In [ ]:
@react.input
def name(n='john doe'):
if not isinstance(n, str):
raise ValueError('Name must be a string')
return n.capitalize()
In [ ]:
name
In [ ]:
@react.connect('name')
def greet(n):
print('hello %s' % n)
In [ ]:
name("almar klein")
A signal can have multiple upstream signals.
In [ ]:
@react.connect('first_name', 'last_name')
def greet(first, last):
print('hello %s %s!' % (first, last))
Dynamism provides great flexibility
In [ ]:
class Person(react.HasSignals):
@react.input
def father(f):
assert isinstance(f, Person)
return f
@react.connect('father.last_name')
def last_name(s):
return s
@react.connect('children.*.name')
def child_names(*names):
return ', '.join(name)
In [ ]:
from flexx import app, react
app.init_notebook()
In [ ]:
class Greeter(app.Pair):
@react.input
def name(s):
return str(s)
class JS:
@react.connect('name')
def _greet(name):
alert('Hello %s!' % name)
In [ ]:
greeter = Greeter()
In [ ]:
greeter.name('John')