def __init__(self, data, model=None, all_models=None, axes_style={}, data_style={}, init_style={}, best_style={}, **kwargs): # Dropdown menu of all subclasses of Model, incl. user-defined. self.models_menu = Dropdown() # Dropbox API is very different between IPy 2.x and 3.x. if IPY2: if all_models is None: all_models = {m.__name__: m for m in Model.__subclasses__()} self.models_menu.values = all_models else: if all_models is None: all_models = [(m.__name__, m) for m in Model.__subclasses__()] self.models_menu.options = all_models self.models_menu.on_trait_change(self._on_model_value_change, 'value') # Button to trigger fitting. self.fit_button = Button(description='Fit') self.fit_button.on_click(self._on_fit_button_click) # Button to trigger guessing. self.guess_button = Button(description='Auto-Guess') self.guess_button.on_click(self._on_guess_button_click) # Parameter widgets are not built here. They are (re-)built when # the model is (re-)set. super(NotebookFitter, self).__init__(data, model, axes_style, data_style, init_style, best_style, **kwargs)
def user_function_panel(self): f = Text(description="Function, Args:") link((self.model,"user_f"),(f,"value")) fapp = Button(description = "Apply") fapp.on_click(lambda x: self.model.apply_userf(name='Apply clicked')) cp2 = ControlPanel(title='User Defined Function',children=[f,fapp]) link((self.more,"value"),(cp2,"visible")) return cp2
def user_function_panel(self): f = Text(description="Function, Args:") link((self.model, "user_f"), (f, "value")) fapp = Button(description="Apply") fapp.on_click(lambda x: self.model.apply_userf(name='Apply clicked')) cp2 = ControlPanel(title='User Defined Function', children=[f, fapp]) link((self.more, "value"), (cp2, "visible")) return cp2
def INOUT_panel(self): # create correlation controls. NOTE: should only be called once. incheck = Checkbox(description = 'Import') link((self.model, "inbox"), (incheck,"value")) outcheck = Checkbox(description = 'Export') link((self.model, "outbox"), (outcheck,"value")) #loaddata = Checkbox(description="Testdata") #link((self.model, "load_spec"), (loaddata, "value")) #testdataset = Text(description = "") #link((self.model, "testdataset"), (testdataset, "value")) filename = Text(description = "") link((self.model, "file_name"), (filename, "value")) loadbutton = Button(color='black',background_color='AliceBlue',description="Load") loadbutton.on_click(lambda x: self.model.load_from_ns()) boxi = HBox([filename,loadbutton]) #link((self.model, "load_spec"), (specbox, "visible")) # loadfile = Checkbox(description="NB Variable") #Test Data # link((self.model, "load_file"), (loadfile, "value")) # filebox = HBox([loadbutton, filename]) #link((self.model, "load_file"), (filebox, "visible")) #boxi = VBox([ #HBox([loaddata, loadfile]), #loaddata, # specbox, #filebox, # ]) link((self.model, "inbox"), (boxi,"visible")) saveplot = Button(color='black',background_color='AliceBlue',description='Save Plot') saveplot.on_click(lambda x: self.model.save_plot()) savespec = Button(color='black',background_color='AliceBlue',description='Export Dataset') savespec.on_click(lambda x: self.model.save_to_ns()) savespecas = Text(description="") link((self.model,"save_spec_as"),(savespecas,"value")) boxo = VBox([ savespecas, HBox([saveplot,savespec]), ]) link((self.model, "outbox"), (boxo,"visible")) #reset = Button(color='white',background_color='violet',description='Reset Defaults') #reset.on_click(lambda x: self.model.) #redraw = Button(description="Redraw") #redraw.on_click(lambda x: self.model.draw()) return ControlPanel(title="Import/Export Dataset", children=[HBox([ VBox([incheck,outcheck]), VBox([boxi,boxo]) ]) ] )
def display(self, n): # Hack! def add_selector(_): g.children = g.children[:-2] + tuple( make_selector(self.sels, add_selector)) + g.children[-2:] def get_data(): x_val, y_val = [f.value for f in c.children] if self.debug: print(x_val, y_val) pairs = self.pairs + [(self.x, x_val), (self.y, y_val)] if self.debug: pprint(pairs) return pairs def out_sents(_): pairs = get_data() display( sentences(pairs, debug=self.debug, limit=self.limit, kind=self.kind)) s_l = HTMLWidget(value='Query') self.sels = [make_selector(self.sels, add_selector) for _ in range(n)] sels = [s_l] + self.sels a_l = HTMLWidget(value='Aggregations') self.aggs = make_aggregator(self.aggs, self.aggs) aggs = [a_l] + [self.aggs] chart_button = Button(description=' Show Chart ') chart_button.on_click(self.chart4) self.field_chooser = c = make_field_chooser('', [], '', []) # clear_output(wait=True) # display(c) self.sent_button = sent_button = Button(description=' Show LMs ') sent_button.on_click(out_sents) # display(sent_button) g = self.group = make_vgroup(sels + aggs + [chart_button, c, sent_button]) display(self.group) return self.group
def display(self): # Hack! def get_data(): x_val, y_val = [f.value for f in c.children] if self.debug: print(x_val, y_val) pairs = self.pairs + [(self.x, x_val), (self.y, y_val)] if self.debug: pprint(pairs) return pairs def out_sents(_): pairs = get_data() display( sentences(pairs, debug=self.debug, limit=self.limit, kind=self.kind)) c = make_field_chooser(self.x, self.xs, self.y, self.ys) # clear_output(wait=True) display(c) sent_button = Button(description=' Show LMs ') sent_button.on_click(out_sents) display(sent_button)
def interactive(__interact_f, **kwargs): """ Builds a group of interactive widgets tied to a function and places the group into a Box container. Returns ------- container : a Box instance containing multiple widgets Parameters ---------- __interact_f : function The function to which the interactive widgets are tied. The **kwargs should match the function signature. **kwargs : various, optional An interactive widget is created for each keyword argument that is a valid widget abbreviation. """ f = __interact_f co = kwargs.pop('clear_output', True) manual = kwargs.pop('__manual', False) kwargs_widgets = [] container = Box(_dom_classes=['widget-interact']) container.result = None container.args = [] container.kwargs = dict() kwargs = kwargs.copy() new_kwargs = _find_abbreviations(f, kwargs) # Before we proceed, let's make sure that the user has passed a set of args+kwargs # that will lead to a valid call of the function. This protects against unspecified # and doubly-specified arguments. getcallargs(f, **{n: v for n, v, _ in new_kwargs}) # Now build the widgets from the abbreviations. kwargs_widgets.extend(_widgets_from_abbreviations(new_kwargs)) # This has to be done as an assignment, not using container.children.append, # so that traitlets notices the update. We skip any objects (such as fixed) that # are not DOMWidgets. c = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] # If we are only to run the function on demand, add a button to request this if manual: manual_button = Button(description="Run %s" % f.__name__) c.append(manual_button) container.children = c # Build the callback def call_f(name=None, old=None, new=None): container.kwargs = {} for widget in kwargs_widgets: value = widget.value container.kwargs[widget._kwarg] = value if co: clear_output(wait=True) if manual: manual_button.disabled = True try: container.result = f(**container.kwargs) except Exception as e: ip = get_ipython() if ip is None: container.log.warn("Exception in interact callback: %s", e, exc_info=True) else: ip.showtraceback() finally: if manual: manual_button.disabled = False # Wire up the widgets # If we are doing manual running, the callback is only triggered by the button # Otherwise, it is triggered for every trait change received # On-demand running also suppresses running the function with the initial parameters if manual: manual_button.on_click(call_f) else: for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def interactive(__interact_f, **kwargs): """Build a group of widgets to interact with a function.""" f = __interact_f co = kwargs.pop('clear_output', True) manual = kwargs.pop('__manual', False) kwargs_widgets = [] container = Box() container.result = None container.args = [] container.kwargs = dict() kwargs = kwargs.copy() new_kwargs = _find_abbreviations(f, kwargs) # Before we proceed, let's make sure that the user has passed a set of args+kwargs # that will lead to a valid call of the function. This protects against unspecified # and doubly-specified arguments. getcallargs(f, **{n:v for n,v,_ in new_kwargs}) # Now build the widgets from the abbreviations. kwargs_widgets.extend(_widgets_from_abbreviations(new_kwargs)) # This has to be done as an assignment, not using container.children.append, # so that traitlets notices the update. We skip any objects (such as fixed) that # are not DOMWidgets. c = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] # If we are only to run the function on demand, add a button to request this if manual: manual_button = Button(description="Run %s" % f.__name__) c.append(manual_button) container.children = c # Build the callback def call_f(name=None, old=None, new=None): container.kwargs = {} for widget in kwargs_widgets: value = widget.value container.kwargs[widget.description] = value if co: clear_output(wait=True) if manual: manual_button.disabled = True try: container.result = f(**container.kwargs) except Exception as e: ip = get_ipython() if ip is None: container.log.warn("Exception in interact callback: %s", e, exc_info=True) else: ip.showtraceback() finally: if manual: manual_button.disabled = False # Wire up the widgets # If we are doing manual running, the callback is only triggered by the button # Otherwise, it is triggered for every trait change received # On-demand running also suppresses running the fucntion with the initial parameters if manual: manual_button.on_click(call_f) else: for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def plot_panel(self): # create draw mode controls. NOTE: should only be called once. cbar = Checkbox(description="Colorbar") link((self.model, "colorbar"), (cbar, "value")) interact = Checkbox(description="Interactive") link((self.model, "interactive"), (interact, "value")) plug_select = Checkbox(description="Line Selection") link((self.model, "selectlines"), (plug_select, "value")) autoupdate = Checkbox(description="Auto Update") link((self.model, "autoupdate"), (autoupdate, "value")) plugin2= Checkbox(description='Cursor') plugin3= Checkbox(description='plugin3') fwidth = FloatText(description='Plot width') fheight = FloatText(description='Plot height') link((self.model, "figwidth"), (fwidth, "value")) link((self.model, "figheight"), (fheight, "value")) f = Text(description="Function:") link((self.model,"user_f"),(f,"value")) fapp = Button(color='black',background_color='AliceBlue',description = "Apply") fapp.on_click(lambda x: self.model.apply_userf(name='apply clicked')) #plugins = HBox([plugin1,plugin2,plugin3]) #more = Checkbox(description="More Options")### LINK IT #link((self, "moreopt"), (more, "value")) #popmore = Popup(children=[VBox([HBox([plug_select,plugin2,plugin3]), # HBox([f,fapp]), # VBox([fwidth, fheight]) # ])], # description='Advanced', button_text='Advanced') more = Checkbox(description="Advanced") link((self.model, "advancedbox"), (more, "value")) popmore = VBox([HBox([plug_select, plugin2, # plugin3 ]), HBox([f,fapp]), HBox([fwidth, fheight]) ]) link((self.model, "advancedbox"), (popmore,"visible")) cmapcheck = Checkbox(description="Colormap") link((self.model, "cmapbox"), (cmapcheck, "value")) cmap = Dropdown(description="Colormap",values=self.model.COLORMAPS) link((self.model,"colormap"),(cmap,"value")) link((self.model,"cmapbox"),(cmap,"visible")) colorcheck = Checkbox(description="Color") link((self.model, "colorbox"), (colorcheck, "value")) color = Dropdown(description="Color",values=self.model.COLORS) link((self.model, "color"), (color, "value")) link((self.model,"colorbox"),(color,"visible")) kind = Dropdown(description="Plot Type", values=PLOTPARSER.keys()) link((self.model, "kind"), (kind, "value")) return ControlPanel(title="Plot Settings", children=[ VBox([autoupdate,kind]), HBox([cbar, interact]), HBox([colorcheck, cmapcheck]), HBox([more]), cmap, color, popmore ] )
def INOUT_panel(self): # create correlation controls. NOTE: should only be called once. incheck = Checkbox(description='Import') link((self.model, "inbox"), (incheck, "value")) outcheck = Checkbox(description='Export') link((self.model, "outbox"), (outcheck, "value")) #loaddata = Checkbox(description="Testdata") #link((self.model, "load_spec"), (loaddata, "value")) #testdataset = Text(description = "") #link((self.model, "testdataset"), (testdataset, "value")) filename = Text(description="") link((self.model, "file_name"), (filename, "value")) loadbutton = Button(color='black', background_color='AliceBlue', description="Load") loadbutton.on_click(lambda x: self.model.load_from_ns()) boxi = HBox([filename, loadbutton]) #link((self.model, "load_spec"), (specbox, "visible")) # loadfile = Checkbox(description="NB Variable") #Test Data # link((self.model, "load_file"), (loadfile, "value")) # filebox = HBox([loadbutton, filename]) #link((self.model, "load_file"), (filebox, "visible")) #boxi = VBox([ #HBox([loaddata, loadfile]), #loaddata, # specbox, #filebox, # ]) link((self.model, "inbox"), (boxi, "visible")) saveplot = Button(color='black', background_color='AliceBlue', description='Save Plot') saveplot.on_click(lambda x: self.model.save_plot()) savespec = Button(color='black', background_color='AliceBlue', description='Export Dataset') savespec.on_click(lambda x: self.model.save_to_ns()) savespecas = Text(description="") link((self.model, "save_spec_as"), (savespecas, "value")) boxo = VBox([ savespecas, HBox([saveplot, savespec]), ]) link((self.model, "outbox"), (boxo, "visible")) #reset = Button(color='white',background_color='violet',description='Reset Defaults') #reset.on_click(lambda x: self.model.) #redraw = Button(description="Redraw") #redraw.on_click(lambda x: self.model.draw()) return ControlPanel( title="Import/Export Dataset", children=[HBox([VBox([incheck, outcheck]), VBox([boxi, boxo])])])
def plot_panel(self): # create draw mode controls. NOTE: should only be called once. cbar = Checkbox(description="Colorbar") link((self.model, "colorbar"), (cbar, "value")) interact = Checkbox(description="Interactive") link((self.model, "interactive"), (interact, "value")) plug_select = Checkbox(description="Line Selection") link((self.model, "selectlines"), (plug_select, "value")) autoupdate = Checkbox(description="Auto Update") link((self.model, "autoupdate"), (autoupdate, "value")) plugin2 = Checkbox(description='Cursor') plugin3 = Checkbox(description='plugin3') fwidth = FloatText(description='Plot width') fheight = FloatText(description='Plot height') link((self.model, "figwidth"), (fwidth, "value")) link((self.model, "figheight"), (fheight, "value")) f = Text(description="Function:") link((self.model, "user_f"), (f, "value")) fapp = Button(color='black', background_color='AliceBlue', description="Apply") fapp.on_click(lambda x: self.model.apply_userf(name='apply clicked')) #plugins = HBox([plugin1,plugin2,plugin3]) #more = Checkbox(description="More Options")### LINK IT #link((self, "moreopt"), (more, "value")) #popmore = Popup(children=[VBox([HBox([plug_select,plugin2,plugin3]), # HBox([f,fapp]), # VBox([fwidth, fheight]) # ])], # description='Advanced', button_text='Advanced') more = Checkbox(description="Advanced") link((self.model, "advancedbox"), (more, "value")) popmore = VBox([ HBox([ plug_select, plugin2, # plugin3 ]), HBox([f, fapp]), HBox([fwidth, fheight]) ]) link((self.model, "advancedbox"), (popmore, "visible")) cmapcheck = Checkbox(description="Colormap") link((self.model, "cmapbox"), (cmapcheck, "value")) cmap = Dropdown(description="Colormap", values=self.model.COLORMAPS) link((self.model, "colormap"), (cmap, "value")) link((self.model, "cmapbox"), (cmap, "visible")) colorcheck = Checkbox(description="Color") link((self.model, "colorbox"), (colorcheck, "value")) color = Dropdown(description="Color", values=self.model.COLORS) link((self.model, "color"), (color, "value")) link((self.model, "colorbox"), (color, "visible")) kind = Dropdown(description="Plot Type", values=PLOTPARSER.keys()) link((self.model, "kind"), (kind, "value")) return ControlPanel(title="Plot Settings", children=[ VBox([autoupdate, kind]), HBox([cbar, interact]), HBox([colorcheck, cmapcheck]), HBox([more]), cmap, color, popmore ])