def ephys_viz_disable(*, params, title='View', external_link=False, height=450): if external_link: query = '' for key in params: query = query + '{}={}&'.format(key, params[key]) href = 'https://ephys-viz.herokuapp.com/?{}'.format(query) display(vdom.a(title, href=href, target='_blank')) else: if title: display(vdom.h3(title)) W = jp_proxy_widget.JSProxyWidget() W.load_js_files(['ephys-viz/web/bundle.js']) W.load_js_files(['ephys-viz/node_modules/d3/dist/d3.min.js']) W.load_css( 'ephys-viz/node_modules/bootstrap/dist/css/bootstrap.min.css') W.load_css('ephys-viz/web/ml-layout.css') display(W) W.js_init(''' element.empty() window.init_ephys_viz(params,element); element.css({height:height,overflow:'auto'}) ''', params=params, height=height)
def get_a_widget(): greeter = jp_proxy_widget.JSProxyWidget() greeter.element.html("<h2>%s</h2>" % test_string) greeter.element.css("color", "magenta") greeter.element.css("background-color", "blue") greeter.element.width(200) display(greeter)
def run_all_in_widget(self, delay_ms=1000): """ Validate all in a widget display. This is suitable for running in a notebook using "run all" as the last step because the final widget is guaranteed to initialize last (which is not true for other cell code execution at this writing). The implementation includes a delay in the python kernel and a javascript delay to allow any unresolved operations in tested widgets to complete. """ print("sleeping in kernel interface...") time.sleep(delay_ms / 1000.0) print("initializing validator widget.") def validate_all(): for (widget, validator) in self.widget_validator_list: validator() validator_widget = jp_proxy_widget.JSProxyWidget() validator_widget.js_init(""" element.html("<em>Delaying validators to allow environment to stabilize</em>"); var call_back = function() { element.html("<b>Validator Summary</b>"); validate_all(); }; setTimeout(call_back, delay_ms); """, validate_all=validate_all, delay_ms=delay_ms) return validator_widget.debugging_display()
def __init__(self, item): self.item = item self.widget = jp_proxy_widget.JSProxyWidget() self.widget.js_init(""" element.show_json = function(json, node, all_json) { all_json = all_json || json; if (!node) { node = element; element.empty(); } var txt = json.txt; var prefix = json.prefix || " "; var indicator = prefix; if (json.expanded) { node.css({ "display": "grid", "grid-template-columns": "25px auto", "grid-template-rows": "25px auto" }); if (json.expandable) { indicator = prefix + "V"; } } else { node.css({ "display": "grid", "grid-template-columns": "25px auto", "grid-template-rows": "25px" }); if (json.expandable) { indicator = prefix + ">"; } } var indicator_div = $("<div>" + indicator + "</div>").appendTo(node); indicator_div.css("background-color", "cornsilk") var toggle_expanded = function () { if (json.expandable) { json.expanded = !json.expanded; // encode as string to avoid recursion limit var json_str = JSON.stringify(all_json); get_json(json_str); } } var txt_div = $("<div>" + txt + "</div>").appendTo(node); indicator_div.click(toggle_expanded); txt_div.click(toggle_expanded); if (json.expanded) { var spacer = $("<div>..</div>").appendTo(node); var member_node = $("<div></div>").appendTo(node); var members = json.members; for (var i=0; i<members.length; i++) { var member = members[i]; element.show_json(member, member_node, all_json); } } }; """, get_json=self.get_json) self.get_json()
def analytics_connect(): """Return an EncapsiaApi object connected to this encapsia server. Only intended to be used from within Encapsia Analytics. Typical usage: !pip install encapsia_api import encapsia_api api = encapsia_api.analytics_connect() # <prints output explaining what has happened> # Then use it with e.g. api.whoami() # etc NB: Because of the way the encapsia token is fetched from the user's current session, make sure you have seen the output message before trying to use the returned object!!! """ try: # We need the jp_proxy_widget module and JupyterLab plugin installed. # They should be present in Encapsia Analytics. import jp_proxy_widget except ModuleNotFoundError: raise RuntimeError( "Cannot find jp_proxy_widget module. Are you using a standard Encapsia Analytics environment?" ) # Create the api object now, and then re-initialise in the callback called from the browser code. # This allows us to populate it with the settings from the user's own browser environment. api = encapsia_api.EncapsiaApi("https://placeholder", "placeholder") def receive_url_and_token(url, token): api.__init__(url, token) p = jp_proxy_widget.JSProxyWidget() p.js_init( """ callback( window.location.protocol + "//" + window.location.hostname, sessionStorage.getItem("token") ); """, callback=receive_url_and_token, ) p.element.html( "Created connection to local encapsia server from current user's login" ) # The `display` function is a global from IPython.core.display. display(p) # NOQA return api
def snapshot_button(self, text=None): fn = self.snapshot_filename if text is None: text = "snapshot to " + repr(fn) result = jp_proxy_widget.JSProxyWidget() result.js_init(""" element.empty(); var button = $("<button>" + text + "</button>").appendTo(element); element.click(snapshot_callback) """, text=text, snapshot_callback=self.snapshot_callback) return result
def config_jupyter(): vdomr_global['mode'] = 'jp_proxy_widget' import jp_proxy_widget jp_widget = jp_proxy_widget.JSProxyWidget() jp_widget.element.html("<span id=jp_widget_empty></span>") jp_widget.js_init(""" // Attach the callback to the global window object so // you can find it from anywhere: window.vdomr_invokeFunction = invokeFunction; """, invokeFunction=invoke_callback) vdomr_global['jp_widget'] = jp_widget display(jp_widget)
def load_requirements(widget=None, silent=True, additional=()): """ Load Javascript prerequisites into the notebook page context. """ if widget is None: widget = jp_proxy_widget.JSProxyWidget() silent = False # Make sure jQuery and jQueryUI are loaded. widget.check_jquery() # load additional jQuery plugin code. all_requirements = list(required_javascript_modules) + list(additional) widget.load_js_files(all_requirements) if not silent: widget.element.html("<div>Requirements for <b>chart_ipynb</b> have been loaded.</div>") display(widget)
def __init__(self, html_title=None, content_callback=None, to_filename=None, size_limit=None, chunk_size=1000000): # by default segment files into chunks to avoid message size limits self.chunk_size = chunk_size assert content_callback is None or to_filename is None, ( "content_callback and to_filename are mutually exclusive, please do not provide both. " + repr((content_callback, to_filename))) assert content_callback is not None or to_filename is not None, ( "one of content_callback or to_filename must be specified.") self.size_limit = size_limit self.to_filename = to_filename self.content_callback = content_callback w = self.widget = jp_proxy_widget.JSProxyWidget() _load_required_js(w) element = w.element if html_title is not None: element.html(html_title) level = 2 options = self.upload_options() options["size_limit"] = size_limit options["chunk_size"] = chunk_size #proxy_callback = w.callback(self.widget_callback_handler, data="upload click", level=level, # segmented=self.segmented) #element = w.element() #upload_button = element.simple_upload_button(proxy_callback, options) w.js_init(""" debugger; var upload_callback = function(data) { var content = data.content; if (!($.type(content) === "string")) { content = data.hexcontent; } handle_chunk(data.status, data.name, content, data); } var upload_button = element.simple_upload_button(upload_callback, options); element.append(upload_button); """, handle_chunk=self.handle_chunk_wrapper, options=options) #w(element.append(upload_button)) #w.flush() self.chunk_collector = [] self.status = "initialized"
def config_jupyter(): VDOMR_GLOBAL['mode'] = 'jp_proxy_widget' import jp_proxy_widget jp_widget = jp_proxy_widget.JSProxyWidget() jp_widget.element.html("<span id=jp_widget_empty></span>") jp_widget.js_init(""" // Attach the callback to the global window object so // you can find it from anywhere: window.vdomr_invokeFunction = invokeFunction; """, invokeFunction=invoke_callback) jp_widget.js_init(_get_init_javascript()) # thx, A. Morley # plotly tests for this but doesn't need it to do anything :/ jp_widget.js_init("window.URL.createObjectURL = function() {};") VDOMR_GLOBAL['jp_widget'] = jp_widget display(jp_widget) # pylint: disable=undefined-variable
def createWidget(component_name, props, onStateChanged=None): reload_javascript() W = jp_proxy_widget.JSProxyWidget() W.state = {} def on_state_changed(state0): W.state = state0 if onStateChanged: onStateChanged() W.js_init(''' element.empty(); props.onStateChanged=function(state) {{ on_state_changed(state); }}; X=window.render_widget('{}',props,element); '''.format(component_name), props=props, on_state_changed=on_state_changed) return W
def n_k_slider(update_callback, n=7, k=3, max_n=50, width=600, height=50): w = jp_proxy_widget.JSProxyWidget() w.check_jquery() w.js_init(""" element.empty(); element.width(width); element.height(height); var slider_div = $("<div/>").appendTo(element); //slider_div.height(height); slider_div.slider({ range: true, min: 0, max: max_n, values: [k, n], slide: function(event, ui) { element.on_slide(event, ui); } }); element.slider_div = slider_div element.info_div = $("<div/>").appendTo(element); element.on_slide = function(event, ui) { var k = ui.values[0]; var n = ui.values[1]; element.display_info(n, k); if (callback) { callback(n, k); } }; element.display_info = function(n, k) { element.info_div.html("n=" + n + "; k=" +k); }; element.display_info(n, k); """, n=n, k=k, max_n=max_n, callback=update_callback, width=width, height=height) return w
def __init__(self): import jp_proxy_widget self._W=jp_proxy_widget.JSProxyWidget() self._initialize_widget(self._W)
def test_create(self): widget = jp_proxy_widget.JSProxyWidget() self.assertEqual(type(widget), jp_proxy_widget.JSProxyWidget)