Commit fe74b7a1 authored by Sebastian's avatar Sebastian

renderjs-extension: update ipy-extension to match ERP5Kernel version

parent 8827f2ce
# TODO See top-level README.md
\ No newline at end of file \ No newline at end of file
# #
# TODO # TODO
# #
# prevent multiple class of init_renderjs - ie. check if loading_gadget there # * prevent multiple class of init_renderjs - ie. check if loading_gadget there
# find proper location for static files # * find proper location for static files
# #
# #
# For each gadget which is to be integrated into the jupyter page one
# RJSGadget object is created. It is used to call call_declared_method
# and destroy.
class RJSGadget:
def __init__(self, gadgetId):
# The UID is used to identify the gadget and gets passed along
# to the events sent to the frontend so that the loading_gadget
# is able to decide to which gadget the fired event belongs
# Python call on object with (uid) ->
# -> Fire event (uid) ->
# -> loading_gadget (uid) ->
# -> pass on the call to gadget with (uid)
self.gadgetId = gadgetId
def __del__(self):
pass
# Fires an event with
# * the uid of the gadget
# * the name of the declared_method
# * the arguments to be passed to the declared_method
# The arguments are packed into a json string and passed to js as such
def call_declared_method(self, method_name, *args):
from IPython.core.display import display, HTML
import json
j_str = json.dumps(args)
script = '''
<script>
var call_event = new CustomEvent("call_gadget",
{ "detail": {
"gadgetId": "''' + self.gadgetId + '''",
"methodName": "''' + method_name + '''",
"methodArgs": ''' + "'" + j_str + "'" + '''
}});
var loadingDiv = document.querySelector(".loading_gadget");
if(loadingDiv != null) {
loadingDiv.dispatchEvent(call_event);
} else {
console.log("~~ call: RenderJS init required first!");
}
</script>
'''
display(HTML(script))
# Fires an event to the destroy this gadget (self)
# Only thing passed is the uid of the gadget
def destroy(self):
from IPython.core.display import display, HTML
script = '''
<script>
var destroy_event = new CustomEvent("destroy_gadget",
{ "detail": { "gadgetId": "''' + self.gadgetId + '''" }});
var loadingDiv = document.querySelector(".loading_gadget");
if(loadingDiv != null) {
loadingDiv.dispatchEvent(destroy_event);
} else {
console.log("~~ destroy: RenderJS init required first!");
}
</script>
'''
display(HTML(script))
# Do nothing on load
def load_ipython_extension(ipython):
pass
# Do nothing on unload
def unload_ipython_extension(ipython):
pass
# Create the original load_gadget with modified rsvp, renderjs # Create the original load_gadget with modified rsvp, renderjs
# Because jupyter notebook has already loaded when this can be called # Because jupyter notebook has already loaded when this can be called
# a manual intialization of the whole renderJS setup is required # a manual initialization of the whole renderJS setup is required
# #
# First the libs rsvp, renderjs-gadget-global and renderjs (patched) # First the libs rsvp, renderjs-gadget-global and renderjs (patched)
# are injected into the page. The patch on renderjs itself is to enable # are injected into the page. The patch on renderjs itself is to enable
...@@ -96,7 +21,7 @@ def unload_ipython_extension(ipython): ...@@ -96,7 +21,7 @@ def unload_ipython_extension(ipython):
# After everything is inplace, rJS.manualBootstrap initializes the # After everything is inplace, rJS.manualBootstrap initializes the
# loading_gadget in exactly the same way as when rJS is normally initialized # loading_gadget in exactly the same way as when rJS is normally initialized
# (on-load) # (on-load)
def init_renderjs(): def initRenderjs():
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
script = ''' script = '''
<script> <script>
...@@ -123,17 +48,13 @@ def init_renderjs(): ...@@ -123,17 +48,13 @@ def init_renderjs():
# Load a gadget given a URL to the HTML file of the gadget # Load a gadget given a URL to the HTML file of the gadget
# -> Generates a new uid for the gadget to-be-loaded
# -> Fires an event which loading_gadget listens on and passes on the URL # -> Fires an event which loading_gadget listens on and passes on the URL
# -> returns the python gadget-object which has the uid as member-variable def loadGadget(ref, gadgetUrl):
def load_gadget(gadgetUrl):
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
import uuid
gadgetId = str(uuid.uuid4())
script = ''' script = '''
<script> <script>
var load_event = new CustomEvent("load_gadget", var load_event = new CustomEvent("load_gadget",
{ "detail": { "url": "''' + gadgetUrl + '", "gadgetId": "' + gadgetId + '''" }}); { "detail": { "url": "''' + gadgetUrl + '", "gadgetId": "' + ref + '''" }});
var loadingDiv = document.querySelector(".loading_gadget"); var loadingDiv = document.querySelector(".loading_gadget");
if(loadingDiv != null) { if(loadingDiv != null) {
...@@ -145,3 +66,60 @@ def load_gadget(gadgetUrl): ...@@ -145,3 +66,60 @@ def load_gadget(gadgetUrl):
''' '''
display(HTML(script)) display(HTML(script))
return RJSGadget(gadgetId) return RJSGadget(gadgetId)
# Fires an event with
# * the ref of the gadget
# * the name of the declared_method
# * the arguments to be passed to the declared_method
# The arguments are packed into a json string and passed to js as such
def call_declared_method(ref, method_name, *args):
from IPython.core.display import display, HTML
import json
j_str = json.dumps(args)
script = '''
<script>
var call_event = new CustomEvent("call_gadget",
{ "detail": { "gadgetId": "''' + ref + '''",
"methodName": "''' + method_name + '''",
"methodArgs": ''' + "'" + j_str + "'" + '''
}});
var loadingDiv = document.querySelector(".loading_gadget");
if(loadingDiv != null) {
loadingDiv.dispatchEvent(call_event);
} else {
console.log("~~ call: RenderJS init required first!");
}
</script>
'''
display(HTML(script))
# Fires an event to the destroy this gadget
# Only thing passed is the ref of the gadget
def destroy(ref):
from IPython.core.display import display, HTML
script =
'''<script>
var destroy_event = new CustomEvent("destroy_gadget",
{ "detail": { "gadgetId": "''' + ref + '''" }});
var loadingDiv = document.querySelector(".loading_gadget");
if(loadingDiv != null) {
loadingDiv.dispatchEvent(destroy_event);
} else {
console.log("~~ destroy: RenderJS init required first!");
}
</script>
'''
display(HTML(script))
# Do nothing on load
def load_ipython_extension(ipython):
pass
# Do nothing on unload
def unload_ipython_extension(ipython):
pass
# Frontend part of the jupyter extension for RenderJS See top-level README.md
\ No newline at end of file
TODO
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment