def displayGlobalButton(): ''' 显示全局按钮 :return: ''' config = Config(GLOBAL_SECTION) if not config.isReady(): config.initConf(GLOBAL_SECTION) config.writeConf(GLOBAL_SECTION, 'ref', 'True') config.writeConf(GLOBAL_SECTION, 'debug', 'False') isDisplayRef = config.readConf(GLOBAL_SECTION, 'ref', 'True') == str(True) isDisplayDebug = config.readConf(GLOBAL_SECTION, 'debug', 'True') == str(True) btnRef = ToggleButton(description = '隐藏参考信息' if isDisplayRef else '显示参考信息', value = isDisplayRef) btnDebug = ToggleButton(description = '隐藏调试信息' if isDisplayDebug else '显示调试信息', value = isDisplayDebug) btnDebugClear = Button(description = '清除调试信息') debug.setCleaner(btnDebugClear) if isDisplayDebug: debug.enable() else: debug.disable() btnRef.observe(on_ref_change) btnDebug.observe(on_debug_change) btnDebugClear.on_click(on_debugclear_clicked) boxGlobal = Box([btnRef, btnDebug, btnDebugClear]) display(boxGlobal)
def __init__(self): plt.close("all") self.backend = TextInputLoop(use_widget=True) self.highlighter = SentimentHighlighter(self.backend) self.backend.add_interface(self.highlighter) self.backend.start() self.cwd_label = Label( value="Working directory: {}".format(Path.cwd()), layout=dict(margin="2px 0px 0px 20px"), ) self.save_path = Text( value=str(Path("saved_html.html")), description='Save path:', disabled=False, layout=dict(width="50%"), ) self.save_button = Button( value=False, description='Save to HTML', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='', icon='', layout=Layout(width='18%', margin="2px 0px 0px 20px"), ) self.progress_label = Label( value="", layout=dict(margin="2px 0px 0px 20px"), ) self.full_contrast = Checkbox(value=False, description='Full Contrast', disabled=False) self.do_highlighting = Checkbox(value=True, description='Do Highlighting', disabled=False) box1 = HBox( (self.do_highlighting, self.full_contrast), layout=Layout(align_content="flex-start"), ) box2 = HBox( (self.save_path, self.save_button), layout=Layout(align_content="flex-start"), ) self.storage_dashboard = VBox( (box1, self.cwd_label, box2, self.progress_label)) # Set observers self.save_button.on_click(self._save) self.full_contrast.observe(self._special_options) self.do_highlighting.observe(self._special_options) # noinspection PyTypeChecker display(self.storage_dashboard)
def setup_widget(self): ### grid widget construction: def items_callback (index, column): items = {} for tool,output in self.metadata.items(): for row,col in output.get('Cell_errors', []): if row == index and self.columns[col] == column: items [f"info_{tool}"] = f"False : Detected By {tool}" items ["label_False"] = 'Label as False' for action, text in zip(['label', 'update'], ['Labeld By', 'Updated By']): for name, data in self.collaborators.items(): df = data[action] val = df.loc[index,column] if name == self.user_id or pd.isna(val): continue key = f"{action}_{val}_{name}" items[key]= f"{val} : {text} {name}" return items def click_callback(index, column, key): action, val = key.split('_')[0:2] if action == 'update': self.grid_widget.edit_cell(index,column, val) elif action == 'label': self.label.loc[index,column] = val context_menu = { 'items_callback' : items_callback, 'click_callback' : click_callback } grid_widget = qgrid.show_grid(self, context_menu = context_menu) def handle_cell_edited(event, grid_widget): index, column, new_value = event['index'], event['column'], event['new'] self.loc[index, column] = new_value grid_widget.on('cell_edited', handle_cell_edited) self.grid_widget = grid_widget #refresh button refresh_button = Button(description='Refresh') refresh_button.on_click(self.refresh) #commit button commit_button = Button(description='Commit') commit_button.on_click(self.commit) def get_widget(): self.refresh() return VBox([ HBox([refresh_button,commit_button]), grid_widget ]) self.get_widget = get_widget
def create_button(self, name): button = Button(description=name, disabled=False, button_style='info', tooltip='Click me', icon='') return button
def __init__(self): self.start_button = Button( value=False, description='Start Camera', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Start the camera and the recognition algorithm.', icon='') self.select_network = Dropdown( options=KerasDetector.available_models, value=KerasDetector.available_models[0], description='Algorithm:', disabled=False, ) self.label_names = Text( value='', placeholder='separated by commas', description='Labels', disabled=False, ) self.num_pictures = IntText(value=2.0, description='#pictures', disabled=False, layout=Layout(width='18%')) self.text = Label(value='', layout=Layout(justify_content='space-around', )) self.widget_box = VBox((HBox( (self.start_button, self.label_names, self.num_pictures, self.select_network), layout=Layout(justify_content="space-around")), self.text)) # Initialize field self.collector = None self.start_button.on_click(self._start_video)
def __init__(self, lp, filename='', **kwargs): self.__lp = lp self.filename = filename self.__accordion = Accordion() self.__auto_apply_cbx = Checkbox(description='Auto apply') self.__auto_save_cbx = Checkbox(description='Auto save') self.__apply_btn = Button(description='Apply changes') self.__save_btn = Button(description='Save changes') self.__add_category_btn = Button(description='Add category') self.__add_category_txt = Text(placeholder='category name') self.__initialize() super().__init__([ VBox([ HBox([ HBox((self.__apply_btn, self.__auto_apply_cbx)), HBox((self.__save_btn, self.__auto_save_cbx)), HBox((self.__add_category_btn, self.__add_category_txt)) ], layout=Layout(flex_flow='row wrap', margin='5px')), self.__accordion ], layout=Layout(margin='5px')) ], **kwargs)
def __init__(self, qNo): self.qNo = qNo self.qOut = Output() self.queryArea = Textarea( value='', placeholder='Type your Query here', description='', disabled=False, #layout = Layout(max_width='30%') ) self.execute = Button( description='Execute', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Execute', #layout = Layout(max_width='20%') ) self.resultMessage = Output() self.execute.on_click(self.executeQuery) self.yourOutput = Output(layout=Layout()) self.expectedOutput = Output(layout=Layout()) self.yourOut = Output() self.expectOut = Output() with self.yourOut: display(HTML('<b>Your Ouput:</b>')) with self.expectOut: display(HTML('<b>Expected Ouput:</b>')) self.disPlayWindow = VBox([HBox([self.qOut,self.queryArea,self.execute,self.resultMessage]),\ VBox([VBox([self.expectOut,self.expectedOutput]\ ),VBox([self.yourOut,self.yourOutput])])]\ ,layout = Layout(width='80%')) self.qset = pd.read_csv('questions.csv') self.questionData = self.qset.loc[self.qset.qNo == self.qNo] expected = self.getExpected() with self.expectedOutput: display(expected) with self.qOut: print(self.questionData.question.values[0])
def __init__(self): self.data_titles = [] self.data_scores = [] self.afinn = None self.select = Dropdown( options={ 'Politiken.dk': 0, 'DR.dk': 1, 'BT.dk': 2, 'Information.dk': 3, 'Børsen.dk': 4, 'Ekstrabladet.dk': 5 }, value=0, description='Vælg nyhedskilde:', disabled=False, layout=Layout(width='300px'), style={'description_width': '130px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs nyheder', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Få nyheder fra RSS-feed og lav sentiment-analyse', icon='') self.widget_box = VBox( (self.select, self.submit_button, self.container), ) self.submit_button.on_click(self._do_sentiment_analysis)
def make_buttons(input_widget, inverse=False): button_layout = {'width': '20px'} factor = 0.5 if inverse else 2.0 double_btn = Button(description="", button_style='warning', layout=button_layout) half_btn = Button(description="", button_style='success', layout=button_layout) def double_value(_): input_widget.value *= factor def half_value(_): input_widget.value /= factor widgets.jslink((double_btn, 'disabled'), (input_widget, 'disabled')) widgets.jslink((half_btn, 'disabled'), (input_widget, 'disabled')) double_btn.on_click(double_value) half_btn.on_click(half_value) return [half_btn, double_btn]
def _create_notebook_form(entity_id, entity_type, message, responses, credentials): check_package('ipywidgets') from ipywidgets.widgets import HTML, Layout, Button, GridspecLayout text = HTML(message) button_yes = Button(description='Yes', button_style='info', layout=Layout(height='32px', width='176px')) button_no = Button(description='No', button_style='', layout=Layout(height='32px', width='176px')) buttons = GridspecLayout(1, 5) buttons[0, 0] = button_yes buttons[0, 1] = button_no def disable_buttons(): button_yes.disabled = True button_no.disabled = True def on_button_yes_clicked(b): disable_buttons() response = trigger_subscription(entity_id, entity_type, credentials) if response: display(HTML(responses.get('ok'))) else: display(HTML(responses.get('error'))) def on_button_no_clicked(b): disable_buttons() display(HTML(responses.get('cancel'))) button_yes.on_click(on_button_yes_clicked) button_no.on_click(on_button_no_clicked) return (text, buttons)
def predictors_widget(preds, target): # plen = len(preds) logger.debug("inside predictors_widget") preds_btn = [] for key, val in preds: btn = Button(description='%s : %s' % (key, val), disabled=False, button_style='', tooltip=str(val), layout=predbtn_layout, display='flex', flex_flow='column', align_items='stretch') # , icon='check') # set btn color btn.style.button_color = 'lightgreen' preds_btn.append(btn) logger.info("Done creating predictor buttons") head_out = HTML( "<h3> Predictors of `%s` with their predictive power </h3>" % (target)) preds_btn.insert(0, head_out) children = preds_btn preds_widget = VBox(children, layout=pred_wiget_layout) bar_w = pp_barplot_widget(preds) # set width of bar plot bar_w.layout.width = 450 rightside_vis = HBox([bar_w], display='flex', align_items="stretch") #rightside_vis.layout.align_items = 'center' rightside_vis.layout.flex = "1.5 1 auto" rightside_vis.layout.width = "60%" rightside_vis.layout.border = '1px solid black' preds_widget.layout.flex = "1 1 auto" preds_widget = HBox([preds_widget, rightside_vis], layout=layout, display='flex') return preds_widget
def displayExecute(self): ''' 执行请求 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') elif self.env.tester is not None: self.btnExecute = Button(description = '执行请求', button_style = 'primary') btnCopy = Button(description = '复制请求链接') self.btnExecute.on_click(self.on_execute_clicked) btnCopy.on_click(self.on_copy_clicked) self.htmExecute = HTML(value = '') # boxExecute = VBox([Box([self.btnExecute, btnCopy]), self.htmExecute]) boxExecute = VBox([self.btnExecute, self.htmExecute]) display(boxExecute)
D_widget = IntSlider(min=10, max=100, step=1, value=50) p_widget = FloatSlider(min=0.0, max=1.0, step=0.1, value=0.5) P_widget = IntSlider(min=5, max=50, step=5, value=20) A_widget = FloatSlider(min=0.0, max=1.0, step=0.1, value=0.5) params = interact(set_params, nn=nn_widget, np=np_widget, D=D_widget, p=p_widget, P=P_widget, A=A_widget) button_init = Button(description="Init", on_click=init) button_init.on_click(init) button_init ########## """ Button( description='Click me', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Click me', icon='check' # (FontAwesome names without the `fa-` prefix) ) """ #button_init = Button(description="Init")
def __menu(self, parameter_type, box, category_name=None): btn_add = Button(description='Add') ddn_add = Dropdown() if parameter_type == 'scalars': def fn_add(self): value = None if ddn_add.value == 'Integer': value = 1 elif ddn_add.value == 'Float': value = 1.0 elif ddn_add.value == 'Bool': value = True name = f'{ddn_add.value}_{len(box.children)}' scalar = self.__lp.add_scalar(name, value, category=category_name) editor = make_scalar_editor(scalar) editor.observe(self.__on_editor_changed(scalar)) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Integer', 'Float', 'Bool'] elif parameter_type == 'materials': def fn_add(self): index = max(self.__lp.get_colors().keys()) + 1 if len( self.__lp.get_colors().keys()) else 0 self.__lp.set_color( index, pgl.Material(ambient=(80, 80, 80), diffuse=1)) editor = make_color_editor(self.__lp.get_color(index), index, no_name=True) editor.observe( self.__on_editor_changed(self.__lp.get_color(index))) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Color'] elif parameter_type == 'curves': def fn_add(self): name = f'{ddn_add.value}_{len(box.children)}' if ddn_add.value == 'Function': curve = self.__lp.add_function(name, category=category_name) else: curve = self.__lp.add_curve(name, category=category_name) editor = make_curve_editor(curve) editor.observe(self.__on_editor_changed(curve)) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Curve', 'Function'] btn_add.on_click(lambda x: fn_add(self)) return HBox((btn_add, ddn_add))
class ParameterEditor(VBox): """TODO: Add docstring here """ filename = '' _model_name = Unicode('ParameterEditorModel').tag(sync=True) _model_module = Unicode(module_name).tag(sync=True) _model_module_version = Unicode(module_version).tag(sync=True) _view_name = Unicode('ParameterEditorView').tag(sync=True) _view_module = Unicode(module_name).tag(sync=True) _view_module_version = Unicode(module_version).tag(sync=True) __auto_apply = False __auto_save = False __accordion = None __auto_apply_cbx = None __auto_save_cbx = None __apply_btn = None __save_btn = None __add_category_btn = None __add_category_txt = None __lp = None def __init__(self, lp, filename='', **kwargs): self.__lp = lp self.filename = filename self.__accordion = Accordion() self.__auto_apply_cbx = Checkbox(description='Auto apply') self.__auto_save_cbx = Checkbox(description='Auto save') self.__apply_btn = Button(description='Apply changes') self.__save_btn = Button(description='Save changes') self.__add_category_btn = Button(description='Add category') self.__add_category_txt = Text(placeholder='category name') self.__initialize() super().__init__([ VBox([ HBox([ HBox((self.__apply_btn, self.__auto_apply_cbx)), HBox((self.__save_btn, self.__auto_save_cbx)), HBox((self.__add_category_btn, self.__add_category_txt)) ], layout=Layout(flex_flow='row wrap', margin='5px')), self.__accordion ], layout=Layout(margin='5px')) ], **kwargs) def dumps(self): return self.__lp.dumps() def __initialize(self): if self.__lp.is_valid(): children, titles = self.__build_gui() accordion = Accordion(children) for i, title in enumerate(titles): accordion.set_title(i, title) self.__accordion = accordion self.__auto_apply_cbx.observe(self.__on_auto_apply_cbx_change, names='value') self.__auto_save_cbx.observe(self.__on_auto_save_cbx_change, names='value') self.__apply_btn.on_click( lambda x: self.on_lpy_context_change(self.__lp.dumps())) self.__save_btn.on_click(lambda x: self.__save()) self.__add_category_btn.on_click(lambda x: self.__add_category( self.__add_category_txt.value.strip())) def __add_category(self, name): if not name or name in self.__lp.get_category_names(): return item_layout = Layout(margin='20px', flex_flow='row wrap') acc = self.__accordion box_scalars = HBox(layout=item_layout) box_curves = HBox(layout=item_layout) acc.children = (*self.__accordion.children, VBox([ self.__menu('scalars', box_scalars, name), box_scalars, self.__menu('curves', box_curves, name), box_curves ])) acc.set_title(len(acc.children) - 1, name) self.__lp.add_category(name) if self.__auto_save: self.__save() def __on_auto_apply_cbx_change(self, change): self.__auto_apply = change['owner'].value self.__apply_btn.disabled = self.__auto_apply if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) def __on_auto_save_cbx_change(self, change): self.__auto_save = change['owner'].value self.__save_btn.disabled = self.__auto_save if self.__auto_save: self.__save() def on_lpy_context_change(self, context): pass def __build_gui(self): children = [] titles = ['materials'] item_layout = Layout(margin='20px', flex_flow='row wrap') if self.__lp: box_materials = HBox(layout=item_layout) for index, color in self.__lp.get_colors().items(): editor = make_color_editor(color, index, no_name=True) if editor: editor.observe(self.__on_editor_changed(color)) box_materials.children = (*box_materials.children, editor) children.append( VBox([self.__menu('materials', box_materials), box_materials])) for category_name in self.__lp.get_category_names(): box_scalars = HBox(layout=item_layout) box_curves = HBox(layout=item_layout) for scalar in self.__lp.get_category_scalars(category_name): editor = make_scalar_editor(scalar, False) if editor: editor.observe(self.__on_editor_changed(scalar)) box_scalars.children = (*box_scalars.children, editor) for param in self.__lp.get_category_graphicalparameters( category_name): editor = make_curve_editor(param, False) if editor: editor.observe(self.__on_editor_changed(param)) box_curves.children = (*box_curves.children, editor) children.append( VBox([ self.__menu('scalars', box_scalars, category_name), box_scalars, self.__menu('curves', box_curves, category_name), box_curves ])) titles.append(category_name) return (children, titles) def __menu(self, parameter_type, box, category_name=None): btn_add = Button(description='Add') ddn_add = Dropdown() if parameter_type == 'scalars': def fn_add(self): value = None if ddn_add.value == 'Integer': value = 1 elif ddn_add.value == 'Float': value = 1.0 elif ddn_add.value == 'Bool': value = True name = f'{ddn_add.value}_{len(box.children)}' scalar = self.__lp.add_scalar(name, value, category=category_name) editor = make_scalar_editor(scalar) editor.observe(self.__on_editor_changed(scalar)) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Integer', 'Float', 'Bool'] elif parameter_type == 'materials': def fn_add(self): index = max(self.__lp.get_colors().keys()) + 1 if len( self.__lp.get_colors().keys()) else 0 self.__lp.set_color( index, pgl.Material(ambient=(80, 80, 80), diffuse=1)) editor = make_color_editor(self.__lp.get_color(index), index, no_name=True) editor.observe( self.__on_editor_changed(self.__lp.get_color(index))) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Color'] elif parameter_type == 'curves': def fn_add(self): name = f'{ddn_add.value}_{len(box.children)}' if ddn_add.value == 'Function': curve = self.__lp.add_function(name, category=category_name) else: curve = self.__lp.add_curve(name, category=category_name) editor = make_curve_editor(curve) editor.observe(self.__on_editor_changed(curve)) box.children = (*box.children, editor) if self.__auto_save: self.__save() if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) ddn_add.options = ['Curve', 'Function'] btn_add.on_click(lambda x: fn_add(self)) return HBox((btn_add, ddn_add)) def __validate_name(self, name): if not _property_name_regex.match(name): return False if name in self.lpy_context: return False for key in self.lpy_context.keys(): if name in self.lpy_context[key]: return False return True def __save(self): if self.filename: params = self.__lp.dumps() with io.open(self.filename, 'w') as file: file.write(json.dumps(json.loads(params), indent=4)) def __on_editor_changed(self, param): def on_param_changed(change): if 'new' in change: value = change['new'] name = change['name'] if isinstance(param, BaseScalar): if name == 'name': param.name = value else: param.value = value elif isinstance(param, tuple): if name == 'value': if isinstance(param[1], (pgl.NurbsCurve2D, pgl.BezierCurve2D)): param[1].ctrlPointList = pgl.Point3Array( [pgl.Vector3(p[0], p[1], 1) for p in value]) elif isinstance(param[1], pgl.Polyline2D): param[1].pointList = pgl.Point2Array( [pgl.Vector2(p[0], p[1]) for p in value]) else: param[1].name = value if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) if self.__auto_save: self.__save() def on_material_changed(change): if 'new' in change: if change['name'] == 'index': self.__lp.unset_color(change['old']) self.__lp.set_color(change['new'], param) else: setattr(param, change['name'], change['new']) if self.__auto_apply: self.on_lpy_context_change(self.__lp.dumps()) if self.__auto_save: self.__save() return on_material_changed if isinstance( param, pgl.Material) else on_param_changed
def mk_map_region_selector(height='600px', **kwargs): from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl from ipywidgets.widgets import Layout, Button, HTML from types import SimpleNamespace state = SimpleNamespace(selection=None, bounds=None, done=False) btn_done = Button(description='done', layout=Layout(width='5em')) btn_done.style.button_color = 'green' btn_done.disabled = True html_info = HTML( layout=Layout(flex='1 0 20em', width='20em', height='3em')) def update_info(txt): html_info.value = '<pre style="color:grey">' + txt + '</pre>' m = Map(**kwargs) if len(kwargs) else Map(zoom=2) m.scroll_wheel_zoom = True m.layout.height = height widgets = [ WidgetControl(widget=btn_done, position='topright'), WidgetControl(widget=html_info, position='bottomleft'), ] for w in widgets: m.add_control(w) draw = DrawControl() draw.circle = {} draw.polyline = {} draw.circlemarker = {} shape_opts = { "fillColor": "#fca45d", "color": "#000000", "fillOpacity": 0.1 } draw.rectangle = {"shapeOptions": shape_opts} poly_opts = {"shapeOptions": dict(**shape_opts)} poly_opts["shapeOptions"]["original"] = dict(**shape_opts) poly_opts["shapeOptions"]["editing"] = dict(**shape_opts) draw.polygon = poly_opts draw.edit = True draw.remove = True m.add_control(draw) m.add_control(FullScreenControl()) def on_done(btn): state.done = True btn_done.disabled = True m.remove_control(draw) for w in widgets: m.remove_control(w) def bounds_handler(event): (lat1, lon1), (lat2, lon2) = event['new'] txt = 'lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]'.format( lat1, lat2, lon1, lon2, n=4) update_info(txt) state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2)) def on_draw(event): v = event['new'] action = event['name'] if action == 'last_draw': state.selection = v['geometry'] elif action == 'last_action' and v == 'deleted': state.selection = None btn_done.disabled = state.selection is None draw.observe(on_draw) m.observe(bounds_handler, ('bounds', )) btn_done.on_click(on_done) return m, state
class QueryWindow: def __init__(self, qNo): self.qNo = qNo self.qOut = Output() self.queryArea = Textarea( value='', placeholder='Type your Query here', description='', disabled=False, #layout = Layout(max_width='30%') ) self.execute = Button( description='Execute', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Execute', #layout = Layout(max_width='20%') ) self.resultMessage = Output() self.execute.on_click(self.executeQuery) self.yourOutput = Output(layout=Layout()) self.expectedOutput = Output(layout=Layout()) self.yourOut = Output() self.expectOut = Output() with self.yourOut: display(HTML('<b>Your Ouput:</b>')) with self.expectOut: display(HTML('<b>Expected Ouput:</b>')) self.disPlayWindow = VBox([HBox([self.qOut,self.queryArea,self.execute,self.resultMessage]),\ VBox([VBox([self.expectOut,self.expectedOutput]\ ),VBox([self.yourOut,self.yourOutput])])]\ ,layout = Layout(width='80%')) self.qset = pd.read_csv('questions.csv') self.questionData = self.qset.loc[self.qset.qNo == self.qNo] expected = self.getExpected() with self.expectedOutput: display(expected) with self.qOut: print(self.questionData.question.values[0]) def getExpected(self): expected = pd.read_pickle('results/' + self.questionData.results.values[0]) if self.questionData.result_type.values[0] != 'set': expected = expected.iloc[0][0] return expected def display(self): return self.disPlayWindow def executeQuery(self, event): self.resultMessage.clear_output() self.yourOutput.clear_output() db = databases.getDB(self.questionData.database.values[0]) error = False try: result = pd.read_sql_query(self.queryArea.value, db) except: with self.resultMessage: display( HTML( '<span style="color:red">Error in processing your query. Please check syntax and retry</span>' )) error = True if not error: match = False expected = self.getExpected() if self.questionData.result_type.values[0] != 'set': if result.shape == (1, 1): result = result.iloc[0][0] match = result == expected else: if not self.questionData.isOrder.values[0]: #check all columns are same if set(result.columns.values) == set( expected.columns.values) == 0: match=result.sort_values(by=[self.questionData.sort_key.values[0]])\ .reset_index(drop=True).equals(expected.sort_values(by=[self.questionData.sort_key.values[0]])\ .reset_index(drop=True)) else: match = result.reset_index(drop=True).equals( expected.reset_index(drop=True)) with self.yourOutput: display(result) msg = '<span style="color:green">Success!!!!!</span>' if not match: msg = '<span style="color:red">Sorry your query results doesnot match the expected result. Please try again</span>' with self.resultMessage: display(HTML(msg))
def __init__(self, wikipedia: Wikipedia, rsspediainit: RsspediaInit): """ :param Wikipedia wikipedia: wikipedia class object initialized with correct language :param RsspediaInit rsspediainit: rsspedia init object - prepares the data and embeddings """ self.data_titles = [] self.data_results = [] self.rsspediainit = rsspediainit self.wikipedia = wikipedia self.select_feed = Dropdown( options={ 'Politiken.dk': 0, 'DR.dk': 1, 'BT.dk': 2, 'Information.dk': 3, 'Børsen.dk': 4, 'Ekstrabladet.dk': 5 }, value=0, description='Vælg nyhedskilde:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_search = Dropdown( options={ 'Okapi BM-25': 0, 'Explicit Semantic Analysis': 1, 'FTN-a': 2, 'FTN-b': 3 }, value=0, description='Vælg søgnings-algorytme:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_nmatches = Dropdown( options={ '3': 3, '5': 5, '10': 10 }, value=3, description='Vælg antal matches:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs nyheder', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Få nyheder fra RSS-feed og find Wikipedia matches', icon='') self.widget_box = VBox( (self.select_feed, self.select_search, self.select_nmatches, self.submit_button, self.container), ) self.submit_button.on_click(self._run)
def __init__(self): """ :param int i: """ self.test_sound = None self.test_data = None self.select_test_data_options = None self.select_test_data = Dropdown( options={'Record test data': 0, 'Load test data files': 1 }, value=0, description='Choose training data:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_test_data.observe(self.on_change) self.select_nseconds = Dropdown( options={ '2s': 2, '3s': 3, '5s': 5 }, value=2, description='Choose recording length:', disabled=False, layout=Layout(width='400px', display='block'), style={'description_width': '160px'}, ) self.container = Output( value="", ) self.submit_button = Button( value=False, description='Get test data', disabled=False, button_style='success', # 'success', 'info', 'warning', 'danger' or '' tooltip='TEST: use recorded sounds or record new', icon='' ) self.play_button = Button( value=False, description='Play the recording', disabled=False, button_style='info', # 'success', 'info', 'warning', 'danger' or '' tooltip='TEST: play recorded or loaded sounds', icon='' ) self.widget_box = VBox( ( HBox( ( VBox(( self.select_test_data, self.select_nseconds ), ), VBox(( self.submit_button, self.play_button ), ) ), ), self.container ), ) self.submit_button.on_click(self._run) self.play_button.on_click(self._playback)
def on_delete(self, btn: widgets.Button): "Flag this image as delete or keep." payload = self._get_change_payload(btn) flagged = payload.get('flagged_for_delete', False) btn.button_style = "" if flagged else "danger" payload['flagged_for_delete'] = not flagged
class PrimeMinisterSpeechDashboard: def __init__(self): self.afinn = Afinn(language="da") self.speeches_names = [('2018 (Lars Løkke Rasmussen)', '2018'), ('2017 (Lars Løkke Rasmussen)', '2017'), ('2016 (Lars Løkke Rasmussen)', '2016'), ('2015 (Lars Løkke Rasmussen)', '2015'), ('2014 (Helle Thorning-Schmidt)', '2014'), ('2013 (Helle Thorning-Schmidt)', '2013'), ('2012 (Helle Thorning-Schmidt)', '2012'), ('2011 (Helle Thorning-Schmidt)', '2011'), ('2010 (Lars Løkke Rasmussen)', '2010'), ('2009 (Lars Løkke Rasmussen)', '2009'), ('2008 (Anders Fogh Rasmussen)', '2008'), ('2007 (Anders Fogh Rasmussen)', '2007'), ('2006 (Anders Fogh Rasmussen)', '2006'), ('2005 (Anders Fogh Rasmussen)', '2005'), ('2004 (Anders Fogh Rasmussen)', '2004'), ('2003 (Anders Fogh Rasmussen)', '2003'), ('2002 (Anders Fogh Rasmussen)', '2002'), ('2001 (Poul Nyrup Rasmussen)', '2001'), ('2000 (Poul Nyrup Rasmussen)', '2000'), ('1999 (Poul Nyrup Rasmussen)', '1999'), ('1998 (Poul Nyrup Rasmussen)', '1998'), ('1997 (Poul Nyrup Rasmussen)', '1997')] self.speeches = {} self.speeches_sentiments = {} self.select = Dropdown( options={ '2018 (Lars Løkke Rasmussen)': 0, '2017 (Lars Løkke Rasmussen)': 1, '2016 (Lars Løkke Rasmussen)': 2, '2015 (Lars Løkke Rasmussen)': 3, '2014 (Helle Thorning-Schmidt)': 4, '2013 (Helle Thorning-Schmidt)': 5, '2012 (Helle Thorning-Schmidt)': 6, '2011 (Helle Thorning-Schmidt)': 7, '2010 (Lars Løkke Rasmussen)': 8, '2009 (Lars Løkke Rasmussen)': 9, '2008 (Anders Fogh Rasmussen)': 10, '2007 (Anders Fogh Rasmussen)': 11, '2006 (Anders Fogh Rasmussen)': 12, '2005 (Anders Fogh Rasmussen)': 13, '2004 (Anders Fogh Rasmussen)': 14, '2003 (Anders Fogh Rasmussen)': 15, '2002 (Anders Fogh Rasmussen)': 16, '2001 (Poul Nyrup Rasmussen)': 17, '2000 (Poul Nyrup Rasmussen)': 18, '1999 (Poul Nyrup Rasmussen)': 19, '1998 (Poul Nyrup Rasmussen)': 20, '1997 (Poul Nyrup Rasmussen)': 21 }, value=0, description='Vælg talen:', disabled=False, layout=Layout(width='400px'), style={'description_width': '100px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs talen', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Indlæs statsministerens tale og lav sentiment-analyse', icon='') self.widget_box = VBox( (self.select, self.submit_button, self.container), ) self.submit_button.on_click(self._do_sentiment_analysis) @property def start(self): return self.widget_box def load_speeches(self): for filepath in glob.iglob('data/statsminister/*.txt'): self.speeches[os.path.basename(filepath).replace(".txt", "")] = [ line.rstrip('\n') for line in open(filepath, mode="r", encoding="utf-8") ] current_sentiment = 0 for line in self.speeches[os.path.basename(filepath).replace( ".txt", "")]: current_sentiment += self.afinn.score(line) self.speeches_sentiments[os.path.basename(filepath).replace( ".txt", "")] = current_sentiment def _do_sentiment_analysis(self, number_of_averaged_scores=10, smoothing_constant=0.9, use_exp_smoothing=True, use_imputation=True, speech_number=None): if speech_number: current_speech = self.speeches_names[speech_number][1] current_speech_title = self.speeches_names[speech_number][0] else: current_speech = self.speeches_names[self.select.value][1] current_speech_title = self.speeches_names[self.select.value][0] scores = [] for i in range(len(self.speeches[current_speech])): scores.append(self.afinn.score(self.speeches[current_speech][i])) # Dataframe pd.set_option('display.max_colwidth', -1) # Used to display whole title (non-truncated) df = pd.DataFrame({ "Line": self.speeches[current_speech], "Score": scores }) # Creating the data frame and populating it # Highlight the positive and negative sentiments def highlight(s): if s.Score > 0: return ['background-color: #AAFFAA'] * 2 elif s.Score < 0: return ['background-color: #FFAAAA'] * 2 else: return ['background-color: #FFFFFF'] * 2 df = df.style.apply(highlight, axis=1) # Imputation - using previous nonzero score instead of zero running_score = 0 imputed_scores = [] for i in range(len(scores)): if scores[i] != 0: running_score = scores[i] imputed_scores.append(scores[i]) else: imputed_scores.append(running_score) smoothed_scores = [] if not use_imputation: imputed_scores = scores for i in range(len(imputed_scores)): if use_exp_smoothing: # Exp smoothing if i == 0: smoothed_scores.append(imputed_scores[i]) else: smoothed_scores.append(imputed_scores[i - 1] * (1 - smoothing_constant) + imputed_scores[i] * smoothing_constant) else: # Arithmetic smoothing s = 0 if i > number_of_averaged_scores: n = number_of_averaged_scores else: n = i + 1 for j in range(n): s = s + imputed_scores[i - j] smoothed_scores.append(s / n) # Data for plotting y = np.array(smoothed_scores) x = np.array(range(1, len(smoothed_scores) + 1)) x_s = np.linspace( x.min(), x.max(), 1800 ) #300 represents number of points to make between T.min and T.max y_s = spline(x, y, x_s) fig, ax = plt.subplots(figsize=(20, 10)) ax.plot(x_s, y_s, color="black", linewidth=3) ax.tick_params(labelsize=13) ax.set_xlabel('Tid', fontsize=16) ax.set_ylabel('Sentiment', fontsize=15) ax.set_title( 'Statsministeren\'s tale: {}'.format(current_speech_title), fontsize=18) # use xnew, ynew to plot filled-color graphs plt.fill_between(x_s, 0, y_s, where=(y_s - 1) < -1, color='#E7D1AC') plt.fill_between(x_s, 0, y_s, where=(y_s - 1) > -1, color='#A8D2D1') ax.grid() plt.show() display(df) return
class RSSDashboard: def __init__(self): self.data_titles = [] self.data_scores = [] self.afinn = None self.select = Dropdown( options={ 'Politiken.dk': 0, 'DR.dk': 1, 'BT.dk': 2, 'Information.dk': 3, 'Børsen.dk': 4, 'Ekstrabladet.dk': 5 }, value=0, description='Vælg nyhedskilde:', disabled=False, layout=Layout(width='300px'), style={'description_width': '130px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs nyheder', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Få nyheder fra RSS-feed og lav sentiment-analyse', icon='') self.widget_box = VBox( (self.select, self.submit_button, self.container), ) self.submit_button.on_click(self._do_sentiment_analysis) @property def start(self): return self.widget_box def _do_sentiment_analysis(self, selected_value=None): RSS_feeds = [ ('Politiken.dk', 'http://politiken.dk/rss/senestenyt.rss'), ('DR.dk', 'http://www.dr.dk/Forms/Published/rssNewsFeed.aspx?config=6b82610d-b898-49b2-80ef-85c5642519c3&rss=Yes&rssTitle=DR+Nyheder+Online&overskrift=Politik+-+seneste+20&Url=%2fnyheder%2f' ), ('BT.dk', 'https://www.bt.dk/bt/seneste/rss'), ('Information.dk', 'https://www.information.dk/feed'), ('Børsen.dk', 'https://borsen.dk/rss/'), ('Ekstrabladet.dk', 'http://ekstrabladet.dk/seneste10.rss') ] if selected_value: feed = feedparser.parse(RSS_feeds[selected_value][1]) else: feed = feedparser.parse(RSS_feeds[self.select.value][1]) self.afinn = Afinn(language="da") # Get relevant objects from RSS feed ans store titles and scores for i in range(len(feed["entries"])): self.data_titles.append(feed["entries"][i]["title"]) self.data_scores.append( self.afinn.score(feed["entries"][i]["title"])) # Dataframe pd.set_option('display.max_colwidth', -1) # Used to display whole title (non-truncated) df = pd.DataFrame({ "Title": self.data_titles, "Score": self.data_scores }) # Creating the data frame and populating it # Highlight the positive and negative sentiments def highlight(s): if s.Score > 0: return ['background-color: #AAFFAA'] * 2 elif s.Score < 0: return ['background-color: #FFAAAA'] * 2 else: return ['background-color: #FFFFFF'] * 2 df = df.style.apply(highlight, axis=1) display(df)
def mk_map_region_selector(map=None, height="600px", **kwargs): from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl from ipywidgets.widgets import Layout, Button, HTML from types import SimpleNamespace state = SimpleNamespace(selection=None, bounds=None, done=False) btn_done = Button(description="done", layout=Layout(width="5em")) btn_done.style.button_color = "green" btn_done.disabled = True html_info = HTML(layout=Layout(flex="1 0 20em", width="20em", height="3em")) def update_info(txt): html_info.value = '<pre style="color:grey">' + txt + "</pre>" def render_bounds(bounds): (lat1, lon1), (lat2, lon2) = bounds txt = "lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]".format( lat1, lat2, lon1, lon2, n=4 ) update_info(txt) if map is None: m = Map(**kwargs) if len(kwargs) else Map(zoom=2) m.scroll_wheel_zoom = True m.layout.height = height else: m = map render_bounds(m.bounds) widgets = [ WidgetControl(widget=btn_done, position="topright"), WidgetControl(widget=html_info, position="bottomleft"), ] for w in widgets: m.add_control(w) draw = DrawControl() draw.circle = {} draw.polyline = {} draw.circlemarker = {} shape_opts = {"fillColor": "#fca45d", "color": "#000000", "fillOpacity": 0.1} draw.rectangle = {"shapeOptions": shape_opts, "metric": ["km", "m"]} poly_opts = {"shapeOptions": dict(**shape_opts)} poly_opts["shapeOptions"]["original"] = dict(**shape_opts) poly_opts["shapeOptions"]["editing"] = dict(**shape_opts) draw.polygon = poly_opts draw.edit = True draw.remove = True m.add_control(draw) m.add_control(FullScreenControl()) def on_done(btn): state.done = True btn_done.disabled = True m.remove_control(draw) for w in widgets: m.remove_control(w) def bounds_handler(event): bounds = event["new"] render_bounds(bounds) (lat1, lon1), (lat2, lon2) = bounds state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2)) def on_draw(event): v = event["new"] action = event["name"] if action == "last_draw": state.selection = v["geometry"] elif action == "last_action" and v == "deleted": state.selection = None btn_done.disabled = state.selection is None draw.observe(on_draw) m.observe(bounds_handler, ("bounds",)) btn_done.on_click(on_done) return m, state
class TwoClassCameraDashboard: def __init__(self): self.start_button = Button( value=False, description='Start Camera', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Start the camera to take pictures for classifier ', icon='', layout=Layout(width='25%'), ) self.save_button = Button( value=False, description='Save images', disabled=True, button_style= 'danger', # 'success', 'info', 'warning', 'danger' or '' tooltip='Save images to folder ', icon='', layout=Layout(width='18%', margin="2px 0px 0px 20px"), ) self.load_button = Button( value=False, description='Load images', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Load images from folder ', icon='', layout=Layout(width='18%', margin="2px 0px 0px 20px"), ) self.save_path = Text( value=str(Path("path", "to", "directory")), description='Save path:', disabled=False, ) self.num_pictures = FloatText( value=12, description='#pictures:', disabled=False, layout=Layout(width='20%'), ) self.use_augmentation = Checkbox( value=False, description='Augmentation', disabled=False, tooltip='Use Augmentation', icon='check', ) self.progress_text = Label( value='', layout=Layout(margin="5px 0px 5px 20px"), ) self.widget_box = VBox(( HBox( (self.num_pictures, self.use_augmentation, self.start_button), layout=Layout(align_content="flex-start"), ), HBox( (self.progress_text, ), layout=Layout(align_content="flex-start"), ), HBox( (self.save_path, self.save_button, self.load_button), layout=Layout(align_content="flex-start"), ), )) self.start_button.on_click(self._start_video) self.save_button.on_click(self._save_images) self.load_button.on_click(self._load_images) self.collector = ImageCollector() def load_ml_data(self, random_permutation=True, train_split=0.9): return self.collector.load_ml_data( random_permutation=random_permutation, train_split=train_split) @property def start(self): return self.widget_box def _load_images(self, _=None): # Can no longer use camera or save images self.save_button.disabled = True self.start_button.disabled = True # Get path from widget load_path = Path(self.save_path.value) if load_path.name != images_dir_name: load_path = Path(load_path, images_dir_name) # Load data self.progress_text.value = "Loading images from: {}".format( load_path.resolve()) self.collector.load_data_from_files(file_path=load_path) if self.collector.loaded: self.progress_text.value = "Images loaded from: {}".format( load_path.resolve()) else: self.progress_text.value = "This directory does not seem to contain jpg-files: {}"\ .format(load_path.resolve()) def _save_images(self, _=None): # Get values from widgets use_augmentation = self.use_augmentation.value save_path = Path(self.save_path.value) self.progress_text.value = "Saving images to: {}".format( save_path.resolve()) # Use collector to save images self.collector.save_images(save_path, use_augmentation=use_augmentation) self.progress_text.value = "Images saved to: {}".format( save_path.resolve()) def _start_video(self, _): # Reset start-button and notify self.start_button.value = False self.progress_text.value = "Starting camera! (please wait)" # Disable controls self.start_button.disabled = True self.num_pictures.disabled = True self.use_augmentation.disabled = True self.save_path.disabled = True # Get settings self.collector.num_pictures = int(self.num_pictures.value) # Start video self.collector.run_collector(use_binary=True) # Re-enable controls self.start_button.disabled = False self.num_pictures.disabled = False self.save_button.disabled = False self.use_augmentation.disabled = False self.save_path.disabled = False self.progress_text.value = "Video done."
def __init__(self): self.start_button = Button( value=False, description='Start Camera', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Start the camera to take pictures for classifier ', icon='', layout=Layout(width='25%'), ) self.save_button = Button( value=False, description='Save images', disabled=True, button_style= 'danger', # 'success', 'info', 'warning', 'danger' or '' tooltip='Save images to folder ', icon='', layout=Layout(width='18%', margin="2px 0px 0px 20px"), ) self.load_button = Button( value=False, description='Load images', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Load images from folder ', icon='', layout=Layout(width='18%', margin="2px 0px 0px 20px"), ) self.save_path = Text( value=str(Path("path", "to", "directory")), description='Save path:', disabled=False, ) self.num_pictures = FloatText( value=12, description='#pictures:', disabled=False, layout=Layout(width='20%'), ) self.use_augmentation = Checkbox( value=False, description='Augmentation', disabled=False, tooltip='Use Augmentation', icon='check', ) self.progress_text = Label( value='', layout=Layout(margin="5px 0px 5px 20px"), ) self.widget_box = VBox(( HBox( (self.num_pictures, self.use_augmentation, self.start_button), layout=Layout(align_content="flex-start"), ), HBox( (self.progress_text, ), layout=Layout(align_content="flex-start"), ), HBox( (self.save_path, self.save_button, self.load_button), layout=Layout(align_content="flex-start"), ), )) self.start_button.on_click(self._start_video) self.save_button.on_click(self._save_images) self.load_button.on_click(self._load_images) self.collector = ImageCollector()
class SoundDemo1Dashboard2: def __init__(self): """ :param int i: """ self.test_sound = None self.test_data = None self.select_test_data_options = None self.select_test_data = Dropdown( options={'Record test data': 0, 'Load test data files': 1 }, value=0, description='Choose training data:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_test_data.observe(self.on_change) self.select_nseconds = Dropdown( options={ '2s': 2, '3s': 3, '5s': 5 }, value=2, description='Choose recording length:', disabled=False, layout=Layout(width='400px', display='block'), style={'description_width': '160px'}, ) self.container = Output( value="", ) self.submit_button = Button( value=False, description='Get test data', disabled=False, button_style='success', # 'success', 'info', 'warning', 'danger' or '' tooltip='TEST: use recorded sounds or record new', icon='' ) self.play_button = Button( value=False, description='Play the recording', disabled=False, button_style='info', # 'success', 'info', 'warning', 'danger' or '' tooltip='TEST: play recorded or loaded sounds', icon='' ) self.widget_box = VBox( ( HBox( ( VBox(( self.select_test_data, self.select_nseconds ), ), VBox(( self.submit_button, self.play_button ), ) ), ), self.container ), ) self.submit_button.on_click(self._run) self.play_button.on_click(self._playback) @property def start(self): return self.widget_box def on_change(self, change): if change['type'] == 'change' and change['name'] == 'value': if change["new"] != 0: self.select_nseconds.layout.display = 'none' self.select_nseconds.disabled = True else: self.select_nseconds.layout.display = 'block' self.select_nseconds.disabled = False # print("changed to {}".format(change['new'])) def _playback(self, v): if self.test_sound is not None: # Instantiate miniRecorder self.rec = miniRecorder(seconds=1.5) self.rec.data = self.test_data self.rec.sound = self.test_sound if self.test_data is None: self.rec.playback(format=pyaudio.paFloat32) else: self.rec.playback() else: print("No test sound has been loaded. Please load the test sound first.") def _run(self, v): self.prefix = "test" self.test_filename = "test.wav" self.test_sound = None self.test_data = None # If we choose to record the data, then we record it here if self.select_test_data.value == 0: # Remove the data with the same prefix file_ext = self.prefix + "*.wav" files = glob.glob(os.path.join(WAV_DIR, file_ext)) for file in files: if self.prefix in file: os.remove(file) # Instantiate miniRecorder self.rec = miniRecorder(seconds=1.5) # Start recording _ = self.rec.record() self.test_sound = self.rec.sound self.test_data = self.rec.data self.rec.write2file(fname=os.path.join(WAV_DIR, self.test_filename)) else: file = glob.glob(os.path.join(WAV_DIR, self.test_filename)) if file: try: (sound_clip, fs) = librosa.load(os.path.join(WAV_DIR, self.test_filename), sr=22050) self.test_sound = np.array(sound_clip) print("File loaded successfully.") except Exception: print("File loading has not succeeded.") else: print("No file named {} has been found in {}".format(self.test_filename, WAV_DIR)) return
def __init__(self): self.afinn = Afinn(language="da") self.speeches_names = [('2018 (Lars Løkke Rasmussen)', '2018'), ('2017 (Lars Løkke Rasmussen)', '2017'), ('2016 (Lars Løkke Rasmussen)', '2016'), ('2015 (Lars Løkke Rasmussen)', '2015'), ('2014 (Helle Thorning-Schmidt)', '2014'), ('2013 (Helle Thorning-Schmidt)', '2013'), ('2012 (Helle Thorning-Schmidt)', '2012'), ('2011 (Helle Thorning-Schmidt)', '2011'), ('2010 (Lars Løkke Rasmussen)', '2010'), ('2009 (Lars Løkke Rasmussen)', '2009'), ('2008 (Anders Fogh Rasmussen)', '2008'), ('2007 (Anders Fogh Rasmussen)', '2007'), ('2006 (Anders Fogh Rasmussen)', '2006'), ('2005 (Anders Fogh Rasmussen)', '2005'), ('2004 (Anders Fogh Rasmussen)', '2004'), ('2003 (Anders Fogh Rasmussen)', '2003'), ('2002 (Anders Fogh Rasmussen)', '2002'), ('2001 (Poul Nyrup Rasmussen)', '2001'), ('2000 (Poul Nyrup Rasmussen)', '2000'), ('1999 (Poul Nyrup Rasmussen)', '1999'), ('1998 (Poul Nyrup Rasmussen)', '1998'), ('1997 (Poul Nyrup Rasmussen)', '1997')] self.speeches = {} self.speeches_sentiments = {} self.select = Dropdown( options={ '2018 (Lars Løkke Rasmussen)': 0, '2017 (Lars Løkke Rasmussen)': 1, '2016 (Lars Løkke Rasmussen)': 2, '2015 (Lars Løkke Rasmussen)': 3, '2014 (Helle Thorning-Schmidt)': 4, '2013 (Helle Thorning-Schmidt)': 5, '2012 (Helle Thorning-Schmidt)': 6, '2011 (Helle Thorning-Schmidt)': 7, '2010 (Lars Løkke Rasmussen)': 8, '2009 (Lars Løkke Rasmussen)': 9, '2008 (Anders Fogh Rasmussen)': 10, '2007 (Anders Fogh Rasmussen)': 11, '2006 (Anders Fogh Rasmussen)': 12, '2005 (Anders Fogh Rasmussen)': 13, '2004 (Anders Fogh Rasmussen)': 14, '2003 (Anders Fogh Rasmussen)': 15, '2002 (Anders Fogh Rasmussen)': 16, '2001 (Poul Nyrup Rasmussen)': 17, '2000 (Poul Nyrup Rasmussen)': 18, '1999 (Poul Nyrup Rasmussen)': 19, '1998 (Poul Nyrup Rasmussen)': 20, '1997 (Poul Nyrup Rasmussen)': 21 }, value=0, description='Vælg talen:', disabled=False, layout=Layout(width='400px'), style={'description_width': '100px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs talen', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Indlæs statsministerens tale og lav sentiment-analyse', icon='') self.widget_box = VBox( (self.select, self.submit_button, self.container), ) self.submit_button.on_click(self._do_sentiment_analysis)
from ipywidgets.widgets import Dropdown, Text, Button, HBox, VBox from IPython.display import display, clear_output import pandas as pd from FileValidator.app import TheValidator def get_clicked(b): clear_output() #resets output every time you click the button csv_to_dataframe = pd.read_csv(FILE_PATH.value) validate_app = TheValidator(csv_to_dataframe, FUNCTION.value) output = validate_app.main() output.to_csv(FILE_PATH.value.replace('.csv', '_validated.csv'), sep=',') print('Validated! Please verify the output') FILE_PATH = Text(placeholder='Path to file') VALIDATE_BUTTON = Button(description="Validate!", button_style="primary") FUNCTION = Dropdown(description="Select a File Type", options=['FileType1']) VALIDATE_BUTTON.on_click(get_clicked) FILE_PATH.layout.width = '75%' display(FILE_PATH, FUNCTION) display(VALIDATE_BUTTON)
style=style) params = interact(set_params, nn=nn_widget, np=np_widget, D=D_widget, p=p_widget, P=P_widget, A=A_widget) #button_init = Button(description="Init", on_click = init) #button_init.on_click(init) #button_init ########## """ Button( description='Click me', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Click me', icon='check' # (FontAwesome names without the `fa-` prefix) ) """ #button_init = Button(description="Init") #button_init.on_click(init) #p = interact(plot_data, rho_0=FloatSlider(min=0.01, max=0.2, step=0.01, value=0.01), f=IntSlider(0,0,10)) """ # Erdos Renyi network
class RSSWikiDashboard: def __init__(self, wikipedia: Wikipedia, rsspediainit: RsspediaInit): """ :param Wikipedia wikipedia: wikipedia class object initialized with correct language :param RsspediaInit rsspediainit: rsspedia init object - prepares the data and embeddings """ self.data_titles = [] self.data_results = [] self.rsspediainit = rsspediainit self.wikipedia = wikipedia self.select_feed = Dropdown( options={ 'Politiken.dk': 0, 'DR.dk': 1, 'BT.dk': 2, 'Information.dk': 3, 'Børsen.dk': 4, 'Ekstrabladet.dk': 5 }, value=0, description='Vælg nyhedskilde:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_search = Dropdown( options={ 'Okapi BM-25': 0, 'Explicit Semantic Analysis': 1, 'FTN-a': 2, 'FTN-b': 3 }, value=0, description='Vælg søgnings-algorytme:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_nmatches = Dropdown( options={ '3': 3, '5': 5, '10': 10 }, value=3, description='Vælg antal matches:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.container = Output(value="", ) self.submit_button = Button( value=False, description='Indlæs nyheder', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Få nyheder fra RSS-feed og find Wikipedia matches', icon='') self.widget_box = VBox( (self.select_feed, self.select_search, self.select_nmatches, self.submit_button, self.container), ) self.submit_button.on_click(self._run) @property def start(self): return self.widget_box def display_beautifully(self): content_items = [] for i in range(len(self.data_results)): news, titles, texts, urls, scores = self.data_results[i] #content_items.append(self.rsspedia_search.display_beautifully(titles, texts, urls, scores)) content_items.append( HTML(value="{}".format( self.rsspedia_search.display_beautifully( titles, texts, urls, scores)))) accordion = Accordion(children=(content_items), ) for i in range(len(self.data_titles)): accordion.set_title(i, "{}. {}".format(i + 1, self.data_titles[i])) display(accordion) def _run(self, *args, **kwargs): selected_value = False self.data_results = [] self.data_titles = [] RSS_feeds = [ ('Politiken.dk', 'http://politiken.dk/rss/senestenyt.rss'), ('DR.dk', 'http://www.dr.dk/Forms/Published/rssNewsFeed.aspx?config=6b82610d-b898-49b2-80ef-85c5642519c3&rss=Yes&rssTitle=DR+Nyheder+Online&overskrift=Politik+-+seneste+20&Url=%2fnyheder%2f' ), ('BT.dk', 'https://www.bt.dk/bt/seneste/rss'), ('Information.dk', 'https://www.information.dk/feed'), ('Børsen.dk', 'https://borsen.dk/rss/'), ('Ekstrabladet.dk', 'http://ekstrabladet.dk/seneste10.rss') ] search_algorithms = [("okapibm25"), ("esa_relatedness"), ("fasttext_a"), ("fasttext_b")] search_algorithm = search_algorithms[self.select_search.value] self.rsspedia_search = RsspediaSearch(rsspediainit=self.rsspediainit, wikipedia=self.wikipedia) if selected_value: feed = feedparser.parse(RSS_feeds[selected_value][1]) else: feed = feedparser.parse(RSS_feeds[self.select_feed.value][1]) # Get relevant objects from RSS feed ans store titles and scores for i in range(len(feed["entries"])): self.data_titles.append(feed["entries"][i]["title"]) titles, texts, urls, scores = self.rsspedia_search.search_wiki( search_texts=[feed["entries"][i]["title"]], n_matches=self.select_nmatches.value, search_type=search_algorithm) self.data_results.append( [feed["entries"][i]["title"], titles, texts, urls, scores]) return self.display_beautifully()