Skip to content

Instantly share code, notes, and snippets.

@oeway
Last active December 14, 2020 01:21
Show Gist options
  • Save oeway/3e77aa6644a47a78424ba7d3cc013d6d to your computer and use it in GitHub Desktop.
Save oeway/3e77aa6644a47a78424ba7d3cc013d6d to your computer and use it in GitHub Desktop.
<config lang="json">
{
"name": "SharedWebPython",
"type": "web-python",
"version": "0.1.1",
"api_version": "0.1.2",
"description": "Sunning plugins in a shared web-python worker",
"tags": [],
"ui": "",
"inputs": null,
"outputs": null,
"flags": ["engine"],
"icon": "extension",
"env": [],
"requirements": [],
"dependencies": [],
"runnable": false
}
</config>
<script lang="python">
from imjoy import api
import sys
from types import ModuleType
import asyncio
import micropip
def wrap_promise(promise):
loop = asyncio.get_event_loop()
fut = loop.create_future()
promise.then(fut.set_result).catch(fut.set_exception)
return fut
async def start_plugin(config, imjoy_interface, engine_utils):
script = config["scripts"][0]
content = script["content"]
lang = script["attrs"]["lang"]
assert lang == 'python', "lang must be python"
loop = asyncio.get_event_loop()
fut = loop.create_future()
def export(api, config=None):
fut.set_result(api)
imjoy_interface["export"] = export
# make a imjoy module for the plugin
m = ModuleType("imjoy")
sys.modules[m.__name__] = m
m.__file__ = m.__name__ + ".py"
m.api = imjoy_interface
import micropip
for r in config["requirements"]:
await wrap_promise(micropip.install(r))
# run the script
exec(content)
return await fut
def connect():
return True
def disconnect():
return True
def heartbeat():
return True
async def setup():
await api.register({
"_rintf": True,
"type": "engine",
"pluginType": "shared-web-python",
"icon": "🐍",
"name": 'Shared Web Python',
"url": 'local/shared-web-python',
"connect": connect,
"disconnect": disconnect,
"startPlugin": start_plugin,
"heartbeat": heartbeat
})
api.export({"setup": setup})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment