def _make_full_browser(W): resize_callback_id = 'resize-callback-' + str(uuid.uuid4()) vd.register_callback(resize_callback_id, lambda width, height: W.setSize( (width, height))) js = """ document.body.style="overflow:hidden"; let onresize_scheduled=false; function schedule_onresize() { if (onresize_scheduled) return; onresize_scheduled=true; setTimeout(function() { onresize(); onresize_scheduled=false; },100); } function onresize() { width = document.body.clientWidth; height = document.body.clientHeight; window.vdomr_invokeFunction('{resize_callback_id}', [width, height], {}) } window.addEventListener("resize", schedule_onresize); schedule_onresize(); """ js = js.replace('{resize_callback_id}', resize_callback_id) vd.devel.loadJavascript(js=js, delay=1)
def render(self): try: size = self._child.size() # if it has the size method except: print('Warning: child of LazyDiv does not have a size() method') size = (300, 300) div_style = dict() div_style['width'] = '{}px'.format(size[0]) div_style['height'] = '{}px'.format(size[1]) if self._has_been_seen: return vd.div(self._child, style=div_style) else: callback_id = 'lazydiv-callback-' + str(uuid.uuid4()) vd.register_callback(callback_id, self._on_visible) js = """ function do_initialize(elmt) { var called=false; var io = new IntersectionObserver( entries => { if (entries[0].isIntersecting) { if (!called) { window.vdomr_invokeFunction('{callback_id}', [], {}) called=true; io.disconnect(); } } }, { // Using default options. } ); io.observe(elmt); } var num_initialize_tries=0; function initialize() { num_initialize_tries=num_initialize_tries+1; var elmt=document.getElementById('{div_id}'); if (elmt) { do_initialize(elmt); } else { if (num_initialize_tries>100) { console.warn('Problem initializing LazyDiv.'); return; } setTimeout(function() { initialize(); },500); } } initialize(); """ js = self._div_id.join(js.split('{div_id}')) js = callback_id.join(js.split('{callback_id}')) vd.devel.loadJavascript(js=js, delay=100) return vd.div('Loading...', id=self._div_id, style=div_style)
def postRenderScript(self): js = """ if (!window.timeseries_models) window.timeseries_models={}; if (!window.timeseries_models['{component_id}']) { let TS0=new window.TimeseriesModel({samplerate:{samplerate}, num_channels:{num_channels}, num_timepoints:{num_timepoints}, segment_size:{segment_size}}); window.timeseries_models['{component_id}'] = TS0; } let TS = window.timeseries_models['{component_id}']; TS.onRequestDataSegment(request_data_segment); if (!window.spike_times) window.spike_times={}; //window.spike_times['{component_id}']={spike_trains}"; function request_data_segment(ds_factor, num) { window.vdomr_invokeFunction('{request_data_segment_callback_id}', [ds_factor, num], {}); } if (!window.timeseries_widgets) window.timeseries_widgets={}; if (!window.timeseries_widgets['{component_id}']) { let W0=new window.TimeseriesWidget(); W0.setSyncGroup('test'); W0.setTimeseriesModel(TS); window.timeseries_widgets['{component_id}'] = W0; } let W=window.timeseries_widgets['{component_id}']; //W.setMarkers(window.spike_times['{component_id}']); $('#{div_id}').empty(); $('#{div_id}').append(W.element()); """ request_data_segment_callback_id = 'request-data-segment-' + str( uuid.uuid4()) vd.register_callback( request_data_segment_callback_id, lambda ds_factor, segment_num: self._set_data_segment( ds_factor=ds_factor, segment_num=segment_num)) js = js.replace('{div_id}', self._div_id) js = js.replace('{component_id}', self.componentId()) # js = js.replace('{spike_trains}', spike_trains_str) js = js.replace('{samplerate}', str(self._recording.get_sampling_frequency())) js = js.replace('{num_channels}', str(self._recording.get_num_channels())) js = js.replace('{num_timepoints}', str(self._recording.get_num_frames())) js = js.replace('{segment_size}', str(self._segment_size)) js = js.replace('{request_data_segment_callback_id}', request_data_segment_callback_id) return js
def __init__(self, *, units): vd.Component.__init__(self) y_scale_factor = _compute_initial_y_scale_factor(units) self._current_unit_id = None self._selected_unit_ids = [] self._selected_unit_ids_changed_handlers = [] self._current_unit_id_changed_handlers = [] self._widgets = [ TemplateWidget(template=unit['template'], unit_id=unit['unit_id'], y_scale_factor=y_scale_factor, size=(150, 300)) for unit in units ] self._uuid = str(uuid.uuid4()) vd.register_callback(self._uuid + '_box_clicked', self._handle_box_clicked) vd.devel.loadBootstrap()
def postRenderScript(self): state_change_callback_id = 'state-change-callback-' + str(uuid.uuid4()) vd.register_callback(state_change_callback_id, lambda current_electrode_index: self._on_state_change(current_electrode_index=current_electrode_index)) js = """ let W=new window.ElectrodeGeometryWidget(); if (!window.geom_widgets) window.geom_widgets={}; window.geom_widgets['{component_id}']=W; W.onCurrentElectrodeIndexChanged(function() { window.vdomr_invokeFunction('{state_change_callback_id}', [Number(W.currentElectrodeIndex())], {}) }); let elmt = $('#{div_id}'); elmt.append(W.element()); """ js = js.replace('{div_id}', self._div_id) js = js.replace('{component_id}', self.componentId()) js = js.replace('{state_change_callback_id}', str(state_change_callback_id)) self._update_widget() return js