def vis(): global ctx2 ctx2 = load_graph("state-visualization-graph.seamless", shares=True, mounts=False) ctx2.add_zip("state-visualization-graph.zip") state_callbacks = {} def state_callback(path, state): if ctx._gen_context is None or ctx2._gen_context is None: return if ctx._gen_context._destroyed or ctx2._gen_context._destroyed: return handle = ctx2.state.handle path2 = ".".join(path) handle[path2] = state def observe_graph(graph): ctx2.graph.set(graph) paths_to_delete = set(state_callbacks.keys()) for node in graph["nodes"]: path = tuple(node["path"]) if node["type"] == "cell": paths = [path] elif node["type"] == "transformer": paths = [ path, path + (node["INPUT"],), ] else: # TODO: libmacro, macro, reactor continue for path in paths: if path in state_callbacks: paths_to_delete.discard(path) continue #print("OBSERVE", path) observers = {} for attr in ("status", "exception"): subpath = path + (attr,) callback = partial(state_callback, subpath) state_callback(subpath, None) observer = ctx.observe(subpath, callback, 2, observe_none=True) observers[subpath] = observer state_callbacks[path] = observers for dpath in paths_to_delete: #print("DELETE", dpath) observers = state_callbacks.pop(dpath) for subpath, observer in observers.items(): state_callback(subpath, None) observer.destroy() #print("DONE") ctx.observe(("get_graph",), observe_graph, 0.5) ctx2.translate() print("Open http://localhost:5813/ctx1/state-visualization.html in the browser")
async def handler(request): text = await request.text() graph = json.loads(text) ctx = load_graph(graph) await ctx.computation() colored_graph = ctx.get_graph() body = json.dumps(colored_graph, indent=2, sort_keys=True) return web.Response( status=200, body=body, content_type='application/json', )
async def handler(request): text = await request.text() graph = json.loads(text) ctx = load_graph(graph) ctx.equilibrate() colored_graph = ctx.get_graph() body = json.dumps(colored_graph, indent=2, sort_keys=True) return web.Response( status=200, body=body, content_type='application/json', )
from math import pi from seamless.core import context, cell ctx0 = context(toplevel=True) ctx0.cell = cell("mixed").set(pi) ctx0.cell2 = cell("python").set("lambda a: 2 * a") ctx0.cell.set({"a": pi}) import json graph = json.load(open("twopi.seamless")) from seamless.highlevel import load_graph ctx = load_graph(graph, cache_ctx=ctx0) ctx.translate(force=True) print(ctx.pi.checksum) print(ctx.pi.value) print("compute") ctx.compute() print(ctx.twopi.value) graph2 = json.load(open("twopi-result.seamless")) ctx2 = load_graph(graph2, cache_ctx=ctx0) print(ctx2.pi.checksum) print(ctx2.pi.value) print("compute") ctx2.compute() print(ctx2.twopi.value)
os.environ["SEAMLESS_COMMUNION_INCOMING"] = "localhost:8602" import seamless seamless.set_ncores(0) from seamless import communion_server communion_server.configure_master( buffer=True, buffer_status=True, transformation_job=True, transformation_status=True ) from seamless.highlevel import load_graph import sys, json infile, outfile = sys.argv[1:] graph = json.load(open(infile)) seamless.database_cache.connect() ctx = load_graph(graph) import asyncio done = asyncio.sleep(1) asyncio.get_event_loop().run_until_complete(done) ctx.compute() colored_graph = ctx.get_graph() with open(outfile, "w") as f: json.dump(colored_graph, f, indent=2, sort_keys=True)
"""Test loading environment from graph 1. Context environment (adds Cython, should succeed) 2. Transformer environment (if loaded correctly, should fail unless extra packages are installed) """ from seamless.highlevel import load_graph ctx = load_graph("environment3.seamless", zip="environment3.zip") ctx.compute() print(ctx.tf.result.value) ctx = load_graph("environment4.seamless", zip="environment4.zip") ctx.compute() print(ctx.tf.exception) print(ctx.tf.result.value)
from math import pi from seamless.core import context, cell ctx0 = context(toplevel=True) ctx0.cell = cell("mixed").set(pi) ctx0.cell2 = cell("python").set("lambda a: 2 * a") ctx0.cell.set({"a": pi}) import json graph = json.load(open("twopi.seamless")) from seamless.highlevel import load_graph ctx = load_graph(graph, cache_ctx=ctx0) ctx.translate(force=True) print(ctx.pi.checksum) print(ctx.pi.value) print("equilibrate") ctx.equilibrate() print(ctx.twopi.value) graph2 = json.load(open("twopi-result.seamless")) ctx2 = load_graph(graph2, cache_ctx=ctx0) print(ctx2.pi.checksum) print(ctx2.pi.value) print("equilibrate") ctx2.equilibrate() print(ctx2.twopi.value)
if args.ncores is not None: seamless.set_ncores(args.ncores) shareserver_address = env.get("SHARESERVER_ADDRESS") if shareserver_address is not None: if shareserver_address == "HOSTNAME": shareserver_address = subprocess.getoutput("hostname -I | awk '{print $1}'") seamless.shareserver.DEFAULT_ADDRESS = shareserver_address print("Setting shareserver address to: {}".format(shareserver_address)) import seamless.stdlib from seamless.highlevel import load_graph, Context graph = json.load(args.graph) if args.zipfile is None and not args.add_zip: ctx = load_graph(graph, mounts=args.mounts, shares=args.shares) else: ctx = Context() if args.zipfile is not None: ctx.add_zip(args.zipfile) for zipf in args.add_zip: ctx.add_zip(zipf) ctx.set_graph(graph, mounts=args.mounts, shares=args.shares) if args.database: params = {} db_host = env.get("SEAMLESS_DATABASE_HOST") if db_host is not None: params["host"] = db_host db_port = env.get("SEAMLESS_DATABASE_PORT") if db_port is not None: params["port"] = db_port
import seamless from seamless.highlevel import load_graph, Cell import os, sys initial_graphfile = sys.argv[1] initial_zipfile = sys.argv[2] output_graphfile = sys.argv[3] output_zipfile = sys.argv[4] ctx = load_graph(initial_graphfile, zip=initial_zipfile) ctx.html = Cell("text").mount("index.html", authority="file").share("index.html") ctx.html.mimetype = "html" ctx.js = Cell("text").mount("index.js", authority="file").share("index.js") ctx.js.mimetype = "js" seamless_dir = os.path.dirname(seamless.__file__) seamless_client = open(seamless_dir + "/js/seamless-client.js").read() ctx.seamless_js = Cell("text").set(seamless_client).share("seamless-client.js") ctx.seamless_js.mimetype = "js" ctx.compute() ctx.save_graph(output_graphfile) ctx.save_zip(output_zipfile)
os.environ["SEAMLESS_COMMUNION_INCOMING"] = "localhost:8602" import seamless seamless.set_ncores(0) from seamless import communionserver communionserver.configure_master( value=True, transformer_job=True, transformer_result=True, transformer_result_level2=True ) from seamless.highlevel import load_graph import sys, json infile, outfile = sys.argv[1:] graph = json.load(open(infile)) redis_cache = seamless.RedisCache() ctx = load_graph(graph) import asyncio done = asyncio.sleep(1) asyncio.get_event_loop().run_until_complete(done) ctx.equilibrate() colored_graph = ctx.get_graph() with open(outfile, "w") as f: json.dump(colored_graph, f, indent=2, sort_keys=True)
# https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js import os; os.system("pip install mrcfile --no-dependencies") from seamless.highlevel import Context, Transformer, Cell, load_graph from functools import partial def save(): ctx.save_graph("graph.seamless") ctx.save_zip("graph.zip") ctx = load_graph("graph.seamless", mounts=True, shares=True) ctx.add_zip("graph.zip") ctx.translate(force=True) # kludge print("Open http://localhost:5813/ctx/index.html in the browser") def vis(): global ctx2 ctx2 = load_graph("state-visualization-graph.seamless", shares=True, mounts=False) ctx2.add_zip("state-visualization-graph.zip") state_callbacks = {} def state_callback(path, state): if ctx._gen_context is None or ctx2._gen_context is None: return if ctx._gen_context._destroyed or ctx2._gen_context._destroyed: return handle = ctx2.state.handle path2 = ".".join(path)
import json import seamless from seamless.highlevel import load_graph graph = json.load(open("twopi-result.seamless")) zipfile = "twopi-result.zip" sctx = load_graph(graph, static=True) sctx.add_zip(zipfile) print(sctx.pi.value) print(sctx.twopi.value)