from seamless.core import context, reactor, cell, csoncell, textcell, jsoncell, pythoncell with macro_mode_on(): ctx = context(toplevel=True) ctx.cson = csoncell().set(""" test: "a" test2: ["b", "c", "d"] """) ctx.json = jsoncell() ctx.cson.connect(ctx.json) ctx.text = textcell() ctx.cson.connect(ctx.text) ctx.rc = reactor({ "json": ("input", "ref", "json", "json"), "cson": ("input", "ref", "text", "cson"), "cson2": ("input", "ref", "json", "cson"), "text": ("input", "ref", "text", "text"), "silk": ("input", "ref", "silk", "json"), }) ctx.json.connect(ctx.rc.json) ctx.cson.connect(ctx.rc.cson) ctx.cson.connect(ctx.rc.cson2) ctx.text.connect(ctx.rc.text) ctx.json.connect(ctx.rc.silk) ctx.rc.code_start.cell().set("") ctx.rc.code_stop.cell().set("") ctx.code = pythoncell().set(""" if PINS.json.updated: print("JSON updated") if PINS.cson.updated: print("CSON text updated")
"aa": "input", "a": "output" }) ctx.aa.connect(ctx.tf.aa) ctx.code = pythoncell().set("""import time time.sleep(2) a = aa""") ctx.code.connect(ctx.tf.code) ctx.a = cell() ctx.tf.a.connect(ctx.a) ctx.b = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "c": "output" }, pure=False) # change between pure=True/"semi"/False (default) ctx.a.connect(ctx.rc.a) ctx.b.connect(ctx.rc.b) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" a = PINS.a.get() b = PINS.b.get() PINS.c.set(a+b) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = pythoncell().set("") ctx.code_stop.connect(ctx.rc.code_stop) ctx.rc.c.connect(ctx.result)
"as": "code", }, }) def run(ctx, code, header): ctx.result = cell("str").set("MACRO: " + header + code) ctx.macro.code.set(run) ctx.code.connect(ctx.macro.code_) ctx.header.connect(ctx.macro.header) ctx.result2 = cell("str") mctx = path(ctx.macro.ctx) mctx.result.connect(ctx.result2) ctx.reactor = reactor({ "header": "input", "code_": { "io": "input", "as": "code", }, "result": "output" }) ctx.code.connect(ctx.reactor.code_) ctx.header.connect(ctx.reactor.header) ctx.reactor.code_start.cell().set("") ctx.reactor.code_update.cell().set( "PINS.result.set('REACTOR: ' + PINS.header.value + PINS.code.value)" ) ctx.reactor.code_stop.set("") ctx.result3 = cell("str") ctx.reactor.result.connect(ctx.result3) ctx.compute() print(ctx.tf.status)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, pythoncell, link with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.rc.a) ctx.cell2.connect(ctx.rc.b) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" a = PINS.a.get() b = PINS.b.get() PINS.c.set(a+b) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = pythoncell().set("") ctx.code_stop.connect(ctx.rc.code_stop) ctx.result_link = link(ctx.result) ctx.rc.c.connect(ctx.result_link) ctx.equilibrate()
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, transformer with macro_mode_on(): ctx = context(toplevel=True) ctx.x = cell("int").set(2) ctx.y = cell("int").set(3) ctx.c = cell("int") ctx.rc = reactor({ "a": "input", "b": "input", "c": { "io": "edit", "must_be_defined": False }, }) ctx.x.connect(ctx.rc.a) ctx.y.connect(ctx.rc.b) ctx.code_start = cell("python").set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = cell("python").set(""" if PINS.a.updated or PINS.b.updated: a = PINS.a.get() b = PINS.b.get() PINS.c.set(a+b) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = cell("python").set("") ctx.code_stop.connect(ctx.rc.code_stop)
def translate_py_reactor(node, root, namespace, inchannels, outchannels, editchannels, lib_path00, is_lib): #TODO: simple-mode translation, without a structured cell skip_channels = ("code_start", "code_update", "code_stop") inchannels = [ic for ic in inchannels if ic[0] not in skip_channels] editchannels = [ec for ec in editchannels if ec[0] not in skip_channels] parent = get_path(root, node["path"][:-1], None, None) name = node["path"][-1] lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None ctx = context(context=parent, name=name) setattr(parent, name, ctx) io_name = node["IO"] if len(inchannels): lib_path0 = None #partial authority or no authority; no library update in either case buffered = node["buffered"] interchannels_in = [ as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "output" ] interchannels_out = [ as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "input" ] interchannels_edit = [ as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "edit" ] all_inchannels = interchannels_in + inchannels #highlevel must check that there are no duplicates all_outchannels = interchannels_out + [ p for p in outchannels if p not in interchannels_out ] all_editchannels = interchannels_edit + [ p for p in editchannels if p not in interchannels_edit ] plain = node["plain"] io_state = node.get("stored_state_io", None) if io_state is None: io_state = node.get("cached_state_io", None) io = build_structured_cell(ctx, io_name, True, plain, buffered, all_inchannels, all_outchannels, io_state, lib_path0, editchannels=all_editchannels) setattr(ctx, io_name, io) for inchannel in inchannels: path = node["path"] + inchannel namespace[path, True] = io.inchannels[inchannel], node for outchannel in outchannels: path = node["path"] + outchannel namespace[path, False] = io.outchannels[outchannel], node for channel in editchannels: path = node["path"] + channel namespace[path, True] = io.editchannels[channel], node namespace[path, False] = io.editchannels[channel], node ctx.rc = reactor(node["pins"]) for attr in ("code_start", "code_stop", "code_update"): if lib_path00 is not None: lib_path = lib_path00 + "." + name + "." + attr c = libcell(lib_path) setattr(ctx, attr, c) else: c = core_cell(node["language"]) c._sovereign = True setattr(ctx, attr, c) if "mount" in node and attr in node["mount"]: c.mount(**node["mount"][attr]) c.connect(getattr(ctx.rc, attr)) code = node.get(attr) if code is None: code = node.get("cached_" + attr) try_set(c, code) namespace[node["path"] + (attr, ), True] = c, node namespace[node["path"] + (attr, ), False] = c, node for pinname, pin in node["pins"].items(): target = getattr(ctx.rc, pinname) iomode = pin["io"] if iomode == "input": io.connect_outchannel((pinname, ), target) elif iomode == "edit": io.connect_editchannel((pinname, ), target) elif iomode == "output": io.connect_inchannel(target, (pinname, )) temp = node.get("TEMP") if temp is None: temp = {} for attr in ("code_start", "code_stop", "code_update"): if attr in temp: try_set(getattr(ctx, attr), temp[attr]) iohandle = io.handle for k, v in temp.items(): if k in ("code_start", "code_stop", "code_update"): continue setattr(iohandle, k, v) if not is_lib: #clean up cached state and in_equilibrium, unless a library context node.pop("cached_state_io", None) namespace[node["path"], True] = io, node namespace[node["path"], False] = io, node node.pop("TEMP", None)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, transformer with open("cell-ipython.ipy", "w") as f: with open("cell-ipython-ORIGINAL.ipy", "r") as f2: f.write(f2.read()) with macro_mode_on(): ctx = context(toplevel=True) ctx.i = cell().set(100) ctx.result = cell() ctx.rc = reactor({ "i": "input", "testmodule": ("input", "plain", "module"), "result": "output", "html": "output" }) ctx.i.connect(ctx.rc.i) ctx.code_start = cell("reactor").set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = cell("reactor").set(""" import time from .testmodule import func, func_html i = PINS.i.get() t = time.time() result = func(i) print("Time: %.1f" % (time.time() - t)) PINS.result.set(result) if PINS.testmodule.updated: PINS.html.set(func_html)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, pythoncell, ipythoncell with macro_mode_on(): ctx = context(toplevel=True) ctx.i = cell().set(100) ctx.result = cell() ctx.rc = reactor({ "i": "input", "testmodule": ("input", "ref", "module", "ipython"), "result": "output", "html": "output" }) ctx.i.connect(ctx.rc.i) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" import time from .testmodule import func, func_html i = PINS.i.get() t = time.time() result = func(i) print("Time: %.1f" % (time.time() - t)) PINS.result.set(result) if PINS.testmodule.updated: PINS.html.set(func_html) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = pythoncell().set("") ctx.code_stop.connect(ctx.rc.code_stop)
import seamless from seamless.core import context, cell, reactor, pythoncell, link ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.rc.a) ctx.cell2.connect(ctx.rc.b) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" a = PINS.a.get() b = PINS.b.get() PINS.c.set(a+b) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = pythoncell().set("") ctx.code_stop.connect(ctx.rc.code_stop) ctx.result_link = link(ctx.result) ctx.rc.c.connect(ctx.result_link) ctx.equilibrate() print(ctx.result.value) ctx.cell1.set(10)
from seamless.core import context, reactor, cell, csoncell, textcell, plaincell, pythoncell with macro_mode_on(): ctx = context(toplevel=True) ctx.cson = csoncell().set(""" test: "a" test2: ["b", "c", "d"] """) ctx.json = plaincell() ctx.cson.connect(ctx.json) ctx.text = textcell() ctx.cson.connect(ctx.text) ctx.rc = reactor({ "json": ("input", "ref", "json", "json"), "cson": ("input", "ref", "text", "cson"), "cson2": ("input", "ref", "json", "cson"), "text": ("input", "ref", "text", "text"), "silk": ("input", "ref", "silk", "json"), }) ctx.json.connect(ctx.rc.json) ctx.cson.connect(ctx.rc.cson) ctx.cson.connect(ctx.rc.cson2) ctx.text.connect(ctx.rc.text) ctx.json.connect(ctx.rc.silk) ctx.rc.code_start.cell().set("") ctx.rc.code_stop.cell().set("") ctx.code = pythoncell().set(""" if PINS.json.updated: print("JSON updated") if PINS.cson.updated: print("CSON text updated")
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, unilink with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "testmodule": ("input", "plain", "module"), "c": "output" }) ctx.cell1_unilink = unilink(ctx.cell1) ctx.cell1_unilink.connect(ctx.rc.a) ctx.cell2.connect(ctx.rc.b) ctx.code_start = cell("reactor").set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = cell("reactor").set(""" print("reactor execute") print(testmodule) print(testmodule.q) from .testmodule import q print(q) import sys print([m for m in sys.modules if m.find("testmodule") > -1]) a = PINS.a.get() b = PINS.b.get()
w.setWindowTitle(PINS.title.get()) """) def make_text_editor(ed): ed.code_start.cell().from_file(editor_pycell2) ed.code_stop.cell().set('w.destroy()') ed.code_update.cell().set(""" if PINS.value.updated: b.setText(PINS.value.get()) if PINS.title.updated: w.setWindowTitle(PINS.title.get()) """) ed1 = ctx.ed1 = reactor(eparams) ed2 = ctx.ed2 = reactor(eparams) ed1.title.cell("text").set("Editor #1") ed2.title.cell("text").set("Editor #2") make_editor(ed1) make_editor(ed2) c_data.connect(ed1.value) c_output.connect(ed2.value) ted1 = ctx.ted1 = reactor(teparams) ted1.title.cell().set("Formula editor") make_text_editor(ted1) c = ed1.title.cell() c = c_code c.connect(ted1.value) meta_ted = ctx.meta_ted = reactor(teparams)
storage = None, form = ctx.v_form, schema = None, buffer = None, inchannels = [("result",), ("c",)], outchannels = [("a",), ("c",)], editchannels = [("b",)], ) ctx.v.connect_outchannel(("a",), ctx.tf.a) ctx.v.connect_outchannel(("b",), ctx.tf.b) ctx.v.connect_inchannel(ctx.tf.c, ("c",)) ctx.tf.c.connect(ctx.result) ctx.rc = reactor({ "result": "output", "b": {"io": "edit", "must_be_defined": False}, "c": {"io": "input", "must_be_defined": False} }) ctx.v.connect_inchannel(ctx.rc.result, ("result",)) ctx.v.connect_editchannel(("b",), ctx.rc.b) ctx.v.connect_outchannel(("c",), ctx.rc.c) ctx.rc.code_start.cell().set(""" print("reactor start") count = 0 """) ctx.rc.code_update.cell().set(""" if count < 100: print("reactor update", count) count += 1 c = PINS.c.value if c is None:
from seamless.core import macro_mode_on from seamless.core import context, transformer, reactor, cell ctx = context(toplevel=True) cont = ctx.cont = transformer(tparams) c_data = cont.value.cell() c_data.set(4) c_code = cont.code.cell() c_output = cont.outp.cell() c_code.set("outp = value*2") c_output2 = ctx.c_output2 = cell("plain") cont2 = ctx.cont2 = reactor({ "x": "input", "xcopy": "edit", }) c_output.connect(cont2.x) ###cont2.xcopy.connect(c_output2) c_output2.connect(cont2.xcopy) cont2.code_start.cell().set("") cont2.code_stop.cell().set("") cont2.code_update.cell().set( """ if PINS.x.updated: PINS.xcopy.set(PINS.x.get()) """ ) ctx.equilibrate() print("VALUE", c_data.value, "'" + c_code.value + "'", c_output.value)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, pythoncell, link with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "testmodule": ("input", "ref", "module", "python"), "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.rc.a) ctx.cell2.connect(ctx.rc.b) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" print("macro execute") print(testmodule) print(testmodule.q) from .testmodule import q print(q) import sys print([m for m in sys.modules if m.find("testmodule") > -1]) a = PINS.a.get() b = PINS.b.get()