def __init__(self): self.currentConnection = None self.currentDB = '' self.tableOut = Output(layout=Layout(max_width='30%')) self.schemaOut = Output() self.tableSelect = Dropdown( options=[''], value='', description='Table:', disabled=False, ) self.databaseSelect = Dropdown( options=['sqlite-sakila', 'chinook'], value='sqlite-sakila', description='Database:', disabled=False, ) self.tableSelect.observe(self.tableChange, names='value') self.databaseSelect.observe(self.databaseChange, names='value') self.displayDatabases = VBox([ HBox( [self.tableOut, VBox([self.databaseSelect, self.tableSelect])]), self.schemaOut ]) self.changeDatabase()
def time_series_plot_widget(data, left="weight", right="composition_ratio"): left_widget = Dropdown(options=data.columns, value=left, description='Left Axis', disabled=False) right_widget = Dropdown(options=data.columns, value=right, description='Right Axis', disabled=False) return interactive(_time_series_plot_view, df=fixed(data), left=left_widget, right=right_widget)
def bind_dropdown( # type: ignore options: state.protocols.Model[typing.List[state.T]], target: state.protocols.Model[state.T]) -> Dropdown: dropdown = Dropdown() def update_options(options: typing.List[state.T]) -> None: dropdown.options = [str(s) for s in options] options.observe(update_options) def update_target(newvalue: typing.Any) -> None: target.restore(options.state[newvalue['new']]) dropdown.observe(update_target, 'index') return dropdown
def __init__(self, fig_size=(10, 6)): self.fig_size = fig_size files = list(sorted(list(storage_dir.glob("*.npy")))) file_names = [val.name for val in files] self.start_button = Button( value=False, description='Show!', disabled=False, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' icon='') self.w_dropdown = Dropdown(options=file_names, value=file_names[0], description='File:', disabled=False, layout={"margin": "10px"}) style = {'description_width': 'initial'} self.w_camera_position = RadioButtons( options=['xyz', 'x', 'y', "z"], description='Camera viewpoint:', disabled=False, style=style, ) self.w_show_mean = Checkbox( value=False, description='Show Averages', disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # icon='check', layout={ "justify_content": "space-around", 'width': '250px', "margin": "10px" }) self.fig = None self.ax = None self.container = VBox(( HBox([self.w_dropdown, self.start_button]), HBox( [self.w_show_mean, self.w_camera_position], layout=Layout(justify_content="space-around", width="100%"), ), ), ) # Assign viewer to events self.start_button.on_click(self.update)
def displayEnv(self): ''' 显示环境组件 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') else: self.envcode = self.cf.readConf(self.default['interface'], 'env') # 环境代码 self.testercode = self.cf.readConf(self.default['interface'], 'tester') # 测试员代码 self.catagoryname = self.cf.readConf(self.default['interface'], 'catagoryname') # 接口类别名称 self.server = servers[self.envcode] if self.envcode is not None else None # 服务器地址 self.tester = testers[self.testercode] if self.envcode is not None else None # 测试信息 # 组件初始化 self.dpEnv = Dropdown( options = [key for key, value in servers.items()], value = self.envcode if self.envcode is not None else None, description = '环境:' ) self.dpTester = Dropdown( options = [key for key, value in testers.items() if value['envcode'] == self.dpEnv.value], value = self.testercode if self.testercode is not None else None, description = '账号:' ) self.dpEnv.observe(self.on_env_change) self.dpTester.observe(self.on_tester_change) self.htmEnv = HTML(value = self.choiceResult()) debug.out('display from env object=%s' % self) display(self.dpEnv) display(self.dpTester) acRef = Accordion(children = [self.htmEnv]) acRef.set_title(0, '环境参考') toggleRefDisplay(acRef) display(acRef) ref.append(acRef)
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): 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 __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 RGB_colourspace_models_chromatically_adapted_primaries_widget(): title_Label = Label( 'RGB Colourspace Models Chromatically Adapted Primaries') title_Label.add_class('widget-title') default_layout = {'flex': '1 1 auto', 'width': 'auto'} input_colourspace_Dropdown = Dropdown( options=sorted(colour.RGB_COLOURSPACES), layout=default_layout) illuminant_Dropdown = Dropdown( options=sorted( colour.ILLUMINANTS['CIE 1931 2 Degree Standard Observer']), layout=default_layout) chromatic_adaptation_transforms_Dropdown = Dropdown( options=sorted(colour.CHROMATIC_ADAPTATION_TRANSFORMS), layout=default_layout) formatter_Dropdown = Dropdown( options=['str', 'repr'], layout=default_layout) decimals_IntSlider = IntSlider(value=10, max=15, layout=default_layout) primaries_Textarea = Textarea(rows=3, layout=default_layout) primaries_Textarea.add_class('widget-output-textarea') def set_primaries_Textarea(): input_colourspace = colour.RGB_COLOURSPACES[ input_colourspace_Dropdown.value] P = colour.chromatically_adapted_primaries( input_colourspace.primaries, input_colourspace.whitepoint, colour.ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][ illuminant_Dropdown.value], chromatic_adaptation_transforms_Dropdown.value) with colour.utilities.numpy_print_options( formatter={ 'float': ('{{:0.{0}f}}'.format(decimals_IntSlider.value)).format }, threshold=np.nan): if formatter_Dropdown.value == 'str': P = str(P) elif formatter_Dropdown.value == 'repr': P = repr(P) primaries_Textarea.rows = len(P.split('\n')) primaries_Textarea.value = P def on_input_change(change): if change['type'] == 'change' and change['name'] == 'value': set_primaries_Textarea() input_colourspace_Dropdown.observe(on_input_change) illuminant_Dropdown.observe(on_input_change) chromatic_adaptation_transforms_Dropdown.observe(on_input_change) formatter_Dropdown.observe(on_input_change) decimals_IntSlider.observe(on_input_change) set_primaries_Textarea() widget = Box([ VBox( [ title_Label, Textarea( 'This widget computes the Chromatically Adapted Primaries ' 'of the given RGB Colourspace Model to the given ' 'Illuminant using the ' 'given Chromatic Adaptation Transform.', rows=3, layout=default_layout), Label('Input RGB Colourspace'), input_colourspace_Dropdown, Label('Illuminant'), illuminant_Dropdown, Label('Chromatic Adaptation Transform'), chromatic_adaptation_transforms_Dropdown, Label('Formatter'), formatter_Dropdown, HBox([Label('Decimals:'), decimals_IntSlider]), Label('RGB Transformation Matrix'), primaries_Textarea, ], layout={ 'border': 'solid 2px', 'width': '55%' }) ]) return widget
# Landsat 7 SLC error option: slc_option = ToggleButtons( options=['Exclude', 'Include'], description='', disabled=False, button_style='', tooltips=[ 'Include those Landsat 7 passes with the slc failure (stripes going through these passes)', 'Exclude those Landsat 7 passes with the slc failure (leads to some data gaps)' ], style={'description_width': 'initial'}) # Which band to export band_option = Dropdown( options=['MNDWI', 'Fisher', 'Cloud', 'RGB', 'False_colour'], value='RGB', description='Band:', disabled=False, ) # Which band to export summary_band_option = Dropdown(options=['MNDWI', 'Fisher'], value='MNDWI', description='Band:', disabled=False, style={'width': 'max-content'}) # What water threshold water_threshold = BoundedFloatText( value=0.1, min=-1, max=1,
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)
class Display: def __init__(self): self.currentConnection = None self.currentDB = '' self.tableOut = Output(layout=Layout(max_width='30%')) self.schemaOut = Output() self.tableSelect = Dropdown( options=[''], value='', description='Table:', disabled=False, ) self.databaseSelect = Dropdown( options=['sqlite-sakila', 'chinook'], value='sqlite-sakila', description='Database:', disabled=False, ) self.tableSelect.observe(self.tableChange, names='value') self.databaseSelect.observe(self.databaseChange, names='value') self.displayDatabases = VBox([ HBox( [self.tableOut, VBox([self.databaseSelect, self.tableSelect])]), self.schemaOut ]) self.changeDatabase() @property def displayDatabases(self): return self._displayDatabases @displayDatabases.setter def displayDatabases(self, value): self._displayDatabases = value def changeDatabase(self): selectedDatabase = self.databaseSelect.value if self.currentConnection is None or self.currentDB != selectedDatabase: if self.currentConnection is not None: self.currentConnection.close() self.currentConnection = sqlite3.connect( f'databases/{selectedDatabase}.db') # lite.connect('folder_A/my_database.db') # sqlite:/// self.currentDB = f'{selectedDatabase}' tables = pd.read_sql_query( "SELECT name FROM sqlite_master WHERE type='table';", self.currentConnection) self.tableSelect.options = tables.name.values self.tableSelect.value = tables.name.values[0] def displayTable(self): query = f"select * from {self.tableSelect.value} limit 5" query2 = f"pragma table_info('{self.tableSelect.value}');" data = pd.read_sql_query(query, self.currentConnection) data2 = pd.read_sql_query( query2, self.currentConnection)[['name', 'type', 'pk']] self.tableOut.clear_output() self.schemaOut.clear_output() with self.tableOut: display(data) with self.schemaOut: display(data2) def tableChange(self, change): self.displayTable() def databaseChange(self, change): self.changeDatabase()
class Env(): ''' 环境类 ''' cf = None def __init__(self, default): self.default = default # 默认配置{catagory,interface} self.cf = Config(self.default['interface']) # 配置对象 def __repr__(self): return '[Env Object: envcode = %s , testercode = %s]' % (self.envcode, self.testercode) def on_env_change(self, change): ''' 更改环境 :param change: 更改内容 :return: ''' if change['type'] == 'change' and change['name'] == 'value': debug.out('on_env_change event') self.envcode = change['new'] self.testercode = None # 清空测试员代码 self.server = servers[self.envcode] if self.envcode is not None else None self.tester = None # 清空测试信息 # 写入配置项 debug.out('on_env_change before write conf\nenvcode=%s\ntestercodee=%s' % ( self.envcode, self.testercode)) self.cf.writeConf(self.default['interface'], 'env', self.envcode) self.cf.writeConf(self.default['interface'], 'tester', self.testercode) # 修改界面 debug.out('on_env_change before modify UI') self.htmEnv.value = self.choiceResult() self.dpTester.options = [key for key, value in testers.items() if value['envcode'] == self.dpEnv.value] def on_tester_change(self, change): ''' 更改测试员 :param change: 更改内容 :return: ''' if change['type'] == 'change' and change['name'] == 'value': debug.out('on_tester_change event') self.testercode = change['new'] self.tester = testers[self.testercode] if self.testercode is not None else None # 写入配置项 debug.out('on_tester_change before write conf\ntestercode=%s' % self.testercode) self.cf.writeConf(self.default['interface'], 'tester', self.testercode) # 修改界面 debug.out('on_tester_change before modify UI') self.htmEnv.value = self.choiceResult() def choiceResult(self): ''' 获取环境选择结果 :return: 选择结果 ''' try: return '<span style="color:#208FFB">当前选择的环境是: %s --> %s<br>当前选择的账号是: %s<br>'\ 'api_key = %s<br>api_secret = %s</span>' % ( self.envcode if self.envcode is not None else '', self.server if self.server is not None else '', self.testercode if self.testercode is not None else '', testers[self.testercode]['api_key'][self.catagoryname] if self.testercode is not None else '', testers[self.testercode]['api_secret'][self.catagoryname] if self.testercode is not None else '') except: return '' def displayEnv(self): ''' 显示环境组件 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') else: self.envcode = self.cf.readConf(self.default['interface'], 'env') # 环境代码 self.testercode = self.cf.readConf(self.default['interface'], 'tester') # 测试员代码 self.catagoryname = self.cf.readConf(self.default['interface'], 'catagoryname') # 接口类别名称 self.server = servers[self.envcode] if self.envcode is not None else None # 服务器地址 self.tester = testers[self.testercode] if self.envcode is not None else None # 测试信息 # 组件初始化 self.dpEnv = Dropdown( options = [key for key, value in servers.items()], value = self.envcode if self.envcode is not None else None, description = '环境:' ) self.dpTester = Dropdown( options = [key for key, value in testers.items() if value['envcode'] == self.dpEnv.value], value = self.testercode if self.testercode is not None else None, description = '账号:' ) self.dpEnv.observe(self.on_env_change) self.dpTester.observe(self.on_tester_change) self.htmEnv = HTML(value = self.choiceResult()) debug.out('display from env object=%s' % self) display(self.dpEnv) display(self.dpTester) acRef = Accordion(children = [self.htmEnv]) acRef.set_title(0, '环境参考') toggleRefDisplay(acRef) display(acRef) ref.append(acRef)
def displayInterface(self): ''' 显示组件 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') self.btnInit = Button(description = '立即初始化', button_style = 'danger') self.btnInit.on_click(self.on_init_clicked) display(self.btnInit) else: self.catagoryname = self.cf.readConf(self.default['interface'], 'catagoryname') # 类别名称 self.catagoryname = self.default[ 'catagory'] if self.catagoryname is None else self.catagoryname # 未设置时使用默认配置 self.interfacename = self.cf.readConf(self.default['interface'], 'interfacename') # 接口名称 self.interfacename = self.default[ 'interface'] if self.interfacename is None else self.interfacename # 未设置时使用默认配置 self.catagory = interfaces.get(self.catagoryname, None) if self.catagoryname is not None else None # 类别信息 self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息 self.interface = self.interfaces.get(self.interfacename, None) if self.interfaces is not None and self.interfacename is not None else None # 接口信息 # 组件初始化 self.dpCatagory = Dropdown( options = [key for key in interfaces.keys()], value = self.catagoryname if self.catagoryname is not None else None, description = '类别:' ) debug.out('interfacename = %s' % self.interfacename) tmpOptions = [key for key in interfaces[self.dpCatagory.value]['interface']] if self.catagory is not None else [] self.dpInterface = Dropdown( options = tmpOptions, value = self.interfacename if self.interfacename in tmpOptions else None, description = '接口:' ) self.dpCatagory.observe(self.on_catagory_change) self.dpInterface.observe(self.on_interface_change) self.htmInterface = HTML(value = self.choiceResult()) self.gdParam = qgrid.show_grid(pd.DataFrame(None)) self.gdBody = qgrid.show_grid(pd.DataFrame(None)) if self.interface is not None: self.dfParams = pd.DataFrame(self.interface.get('params', ['无']), index = [0]).T self.dfParams.columns = ['请求参数类型'] self.gdParam = qgrid.show_grid(self.dfParams, grid_options = {'filterable': False, 'editable': False}) self.gdParam.layout = Layout(width = '90%') self.dfBody = pd.DataFrame(self.interface.get('body', ['无'])) self.dfBody.columns = ['请求体参数名称'] self.gdBody = qgrid.show_grid(self.dfBody, grid_options = {'filterable': False, 'editable': False}) self.gdBody.layout = Layout(width = '90%') debug.out('display from interface object=%s' % self) display(self.dpCatagory) display(self.dpInterface) boxRequest = Box([VBox([Label(value = '请求参数:'), self.gdParam], layout = Layout(flex = '1 1 0%', width = 'auto')), VBox([Label(value = '请求体参数:'), self.gdBody], layout = Layout(flex = '1 1 0%', width = 'auto'))], layout = Layout(flex_flow = 'row', display = 'flex')) boxRef = VBox([self.htmInterface, boxRequest]) acRef = Accordion(children = [boxRef]) acRef.set_title(0, '接口参考') toggleRefDisplay(acRef) display(acRef) ref.append(acRef)
class Interface(): ''' 接口类 ''' def __init__(self, default): self.default = default # 默认配置{catagory,interface} self.cf = Config(self.default['interface']) # 配置对象 def __repr__(self): return '[Interface Object: catagoryname = %s , interfacename = %s]' % (self.catagoryname, self.interfacename) def on_catagory_change(self, change): ''' 更改类别 :param change: 更改内容 :return: ''' if change['type'] == 'change' and change['name'] == 'value': debug.out('on_catagory_change event') self.catagoryname = change['new'] self.interfacename = None # 清空接口名称 self.catagory = interfaces.get(self.catagoryname, None) if self.catagoryname is not None else None # 类别信息 self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息 self.interface = None # 清空接口信息 # 写入配置项 debug.out('on_catagory_change before write conf\ncatagoryname=%s\ninterfacename=%s' % ( self.catagoryname, self.interfacename)) self.cf.writeConf(self.default['interface'], 'catagoryname', self.catagoryname) self.cf.writeConf(self.default['interface'], 'interfacename', self.interfacename) # 修改界面 debug.out('on_catagory_change before modify UI') self.htmInterface.value = self.choiceResult() self.dpInterface.options = [key for key in self.interfaces.keys()] def on_interface_change(self, change): ''' 更改接口 :param change: 更改内容 :return: ''' if change['type'] == 'change' and change['name'] == 'value': debug.out('on_interface_change event') self.interfacename = change['new'] self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息 self.interface = self.interfaces.get(self.interfacename, None) if self.interfaces is not None and self.interfacename is not None else None # 接口信息 # 写入配置项 debug.out('on_interface_change before write conf\ninterfacename=%s' % self.interfacename) self.cf.writeConf(self.default['interface'], 'interfacename', self.interfacename) # 修改界面 debug.out('on_interface_change before modify UI') self.htmInterface.value = self.choiceResult() self.dfParams = pd.DataFrame(self.interface['params'], index = [0]).T self.dfParams.columns = ['请求参数类型'] self.gdParam.df = self.dfParams self.dfBody = pd.DataFrame(self.interface.get('body', ['无'])) self.dfBody.columns = ['请求体参数名称'] self.gdBody.df = self.dfBody self.packageWidgets() self.packageParams() def on_init_clicked(self, b): ''' 点击初始化按钮 :param b: 按钮 :return: ''' debug.out('on_init_clicked event') self.cf.initConf(self.default['interface']) self.cf.writeConf(self.default['interface'], 'catagoryname', self.default['catagory']) self.cf.writeConf(self.default['interface'], 'interfacename', self.default['interface']) def on_param_change(self, change): ''' 参数输入变化 :param change: 新的参数值 :return: ''' if change['type'] == 'change' and change['name'] == 'value': self.packageParams() def on_execute_clicked(self, b): ''' 点击执行请求按钮 :param b: 按钮 :return: ''' debug.out('on_execute_clicked event') import requests if self.interface['method'] == 'GET': self.r = requests.get('http://' + self.env.server + self.interface['url'], params = self.params) self.htmExecute.value = '<span style="color:#208FFB">[%s请求执行完成] --> %s</span>' % ( self.interface['method'], self.r.url) elif self.interface['method'] == 'POST': if self.interface.get('body', False) != False: self.r = requests.post('http://' + self.env.server + self.interface['url'], params = self.params, data = self.data) else: self.r = requests.post('http://' + self.env.server + self.interface['url'], params = self.params) self.htmExecute.value = '<span style="color:#208FFB">[%s请求执行完成] --> %s</span>' % ( self.interface['method'], self.r.url) else: self.htmExecute.value = '<span style="color:red">[%s接口方法填写错误,请求未执行]</span>' % self.interface['method'] if self.gdResult is not None: import json output = json.loads(self.r.text) resultCode = output['resultCode'] debug.out('resultCode = %s' % resultCode) if resultCode == 1: debug.out('response output: %s' % output) self.htmResult.value = '<span style="color:#208FFB">[请求成功] 返回数据:</span>' for field in self.interface['output']: output = output[field] df = pd.DataFrame(output) self.gdResult.df = df self.gdResult.layout = Layout() else: self.htmResult.value = output['<span style="color:red">请求失败:%s</span>' % output['resultMsg']] self.gdResult.layout = Layout(display = 'none') def on_copy_clicked(self, b): # pyperclip.copy(self.r.url) pass def choiceResult(self): ''' 获取环境选择结果 :return: 选择结果 ''' try: return '<span style="color:#208FFB">当前选择的接口是: [%s]<br>url = %s<br>method = %s<br>output = %s </span>' % ( self.interface.get('desc', ''), self.interface.get('url', ''), self.interface.get('method', ''), self.interface.get('output', '')) except: return '' def packageWidgets(self): ''' 打包组件 :return: ''' debug.out('package request widgets') self.paramWidgets = {} self.txtBody = None for param, ptype in self.interface['params'].items(): value = self.cf.readConf(self.default['interface'], param) if ptype == 'int': pw = IntText(description = param + ':', value = value if value is not None else 0, layout = Layout(width = '90%')) elif ptype == 'date': pw = DatePicker(description = param + ':', value = value if value is not None else '', layout = Layout(width = '90%')) elif ptype == 'companyId': pw = Text(description = param + ':', value = value if value is not None else self.env.tester['companyId'], layout = Layout(width = '90%')) elif ptype == 'openId': pw = Text(description = param + ':', value = value if value is not None else self.env.tester['openId'], layout = Layout(width = '90%')) else: pw = Text(description = param + ':', value = value if value is not None else '', layout = Layout(width = '90%')) pw.observe(self.on_param_change) self.paramWidgets[param] = pw self.txtBody = Textarea(description = 'body data:', value = '') self.txtBody.layout = Layout(display = 'none') debug.out(self.interface.get('body', False)) if self.interface.get('body', False) != False: debug.out('interface have body data to package widget') value = self.cf.readConf(self.default['interface'], 'body') self.txtBody.value = value if value is not None else '' self.txtBody.layout = Layout(width = '90%', height = '200px') tmpWds = [wd for wd in self.paramWidgets.values()] tmpWds.append(self.txtBody) self.boxParams.children = tmpWds def packageParams(self): ''' 打包数据 :return: ''' debug.out('package request params') self.params = {} for param, widget in self.paramWidgets.items(): self.params[param] = str(widget.value) self.cf.writeConf(self.env.default['interface'], param, str(widget.value)) self.params['appKey'] = self.env.tester['api_key'][self.catagoryname] self.params['nonce'] = Tool.generate_nonce() self.params['time'] = Tool.generate_time() self.params['sign'] = Tool.generate_sign(self.params, self.env.tester['api_secret'][self.catagoryname]) df = pd.DataFrame(self.params, index = [0]).T df.columns = ['参数值'] self.gdParamValue.df = df debug.out(self.interface.get('body', False)) if self.interface.get('body', False) != False: debug.out('interface have body data to package params') self.data = self.txtBody.value self.cf.writeConf(self.env.default['interface'], 'body', self.txtBody.value) else: self.data = None self.cf.writeConf(self.env.default['interface'], 'body', '') def displayInterface(self): ''' 显示组件 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') self.btnInit = Button(description = '立即初始化', button_style = 'danger') self.btnInit.on_click(self.on_init_clicked) display(self.btnInit) else: self.catagoryname = self.cf.readConf(self.default['interface'], 'catagoryname') # 类别名称 self.catagoryname = self.default[ 'catagory'] if self.catagoryname is None else self.catagoryname # 未设置时使用默认配置 self.interfacename = self.cf.readConf(self.default['interface'], 'interfacename') # 接口名称 self.interfacename = self.default[ 'interface'] if self.interfacename is None else self.interfacename # 未设置时使用默认配置 self.catagory = interfaces.get(self.catagoryname, None) if self.catagoryname is not None else None # 类别信息 self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息 self.interface = self.interfaces.get(self.interfacename, None) if self.interfaces is not None and self.interfacename is not None else None # 接口信息 # 组件初始化 self.dpCatagory = Dropdown( options = [key for key in interfaces.keys()], value = self.catagoryname if self.catagoryname is not None else None, description = '类别:' ) debug.out('interfacename = %s' % self.interfacename) tmpOptions = [key for key in interfaces[self.dpCatagory.value]['interface']] if self.catagory is not None else [] self.dpInterface = Dropdown( options = tmpOptions, value = self.interfacename if self.interfacename in tmpOptions else None, description = '接口:' ) self.dpCatagory.observe(self.on_catagory_change) self.dpInterface.observe(self.on_interface_change) self.htmInterface = HTML(value = self.choiceResult()) self.gdParam = qgrid.show_grid(pd.DataFrame(None)) self.gdBody = qgrid.show_grid(pd.DataFrame(None)) if self.interface is not None: self.dfParams = pd.DataFrame(self.interface.get('params', ['无']), index = [0]).T self.dfParams.columns = ['请求参数类型'] self.gdParam = qgrid.show_grid(self.dfParams, grid_options = {'filterable': False, 'editable': False}) self.gdParam.layout = Layout(width = '90%') self.dfBody = pd.DataFrame(self.interface.get('body', ['无'])) self.dfBody.columns = ['请求体参数名称'] self.gdBody = qgrid.show_grid(self.dfBody, grid_options = {'filterable': False, 'editable': False}) self.gdBody.layout = Layout(width = '90%') debug.out('display from interface object=%s' % self) display(self.dpCatagory) display(self.dpInterface) boxRequest = Box([VBox([Label(value = '请求参数:'), self.gdParam], layout = Layout(flex = '1 1 0%', width = 'auto')), VBox([Label(value = '请求体参数:'), self.gdBody], layout = Layout(flex = '1 1 0%', width = 'auto'))], layout = Layout(flex_flow = 'row', display = 'flex')) boxRef = VBox([self.htmInterface, boxRequest]) acRef = Accordion(children = [boxRef]) acRef.set_title(0, '接口参考') toggleRefDisplay(acRef) display(acRef) ref.append(acRef) def displayPrepare(self, env): ''' 准备数据 :param env: 当前环境 :return: ''' self.env = env if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') else: self.boxParams = VBox() self.boxParams.layout = Layout(flex_flow = 'column', display = 'flex') self.packageWidgets() display(self.boxParams) self.gdParamValue = qgrid.show_grid(pd.DataFrame([]), grid_options = {'filterable': False, 'autoHeight': True, 'editable': False}) self.gdParamValue.layout = Layout(width = '90%') self.packageParams() self.txtBodyValue = Textarea(value = self.data if self.data is not None else '') self.txtBodyValue.layout = Layout(width = '90%', height = '200px', margin = '6px 2px 2px 2px') boxRequest = Box([ VBox([Label(value = '请求参数值:'), self.gdParamValue], layout = Layout(flex = '1 1 0%', width = 'auto')), VBox([Label(value = '请求体参数值:'), self.txtBodyValue], layout = Layout(flex = '1 1 0%', width = 'auto'))], layout = Layout(flex_flow = 'row', display = 'flex')) acRef = Accordion(children = [boxRequest]) acRef.set_title(0, '输入参考') toggleRefDisplay(acRef) display(acRef) ref.append(acRef) 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) def displayResponse(self): ''' 查看结果 :return: ''' if not self.cf.isReady(): print(Fore.RED + '配置项尚未初始化!') else: self.htmResult = HTML(value = '') self.gdResult = qgrid.show_grid(pd.DataFrame([])) boxResult = VBox([self.htmResult, self.gdResult]) display(boxResult)
def run(self): data=self.data #set layout and buttoms style = {'description_width': '100px'} layout=Layout(width="30%") car_bt=Dropdown(options=self.car_name.sort_values().values, description="Choose your car", style=style, layout=Layout(width="50%")) MPG_bt=Dropdown(options=["All","Low","Medium","High"], description="MPG", style=style, layout=layout) Cylinders_bt=Dropdown(options=["All","Low","Medium","High"], description="Cylinders", style=style, layout=layout) Displacement_bt=Dropdown(options=["All","Low","Medium","High"], description="Displacement", style=style, layout=layout) Horsepower_bt=Dropdown(options=["All","Low","Medium","High"], description="Horsepower", style=style, layout=layout) Weight_bt=Dropdown(options=["All","Low","Medium","High"], description="Weight", style=style, layout=layout) Acceleration_bt=Dropdown(options=["All","Low","Medium","High"], description="Acceleration", style=style, layout=layout) #function that plots the comparissons def compare(car_, MPG, Cylinders, Displacement, Horsepower, Weight, Acceleration): import matplotlib.gridspec as gridspec dict_data={} #set figure fig = plt.figure(figsize=(20,10)) fig.suptitle(car_.title(), fontsize=20) ax1=fig.add_subplot(1, 2, 1) #top and bottom left ax2=fig.add_subplot(2, 2, 2) #top right ax3=fig.add_subplot(2, 2, 4) #bottom right #find car features car=np.where(self.car_name==car_)[0][0] list_var=[MPG, Cylinders, Displacement, Horsepower, Weight, Acceleration] #build boolean: data_boolean=pd.DataFrame() for name, val in zip(data.columns, list_var): data_boolean[name]=data[name+"_class"].apply(lambda x: True if x==val or val=="All" else False) data_boolean=data_boolean.all(axis=1) booleans=(data.index.values!=(car))&data_boolean.values values=((data.loc[(car),data.columns[:6]]-data.loc[booleans,data.columns[:6]].mean())/data.loc[booleans,data.columns[:6]].mean())*100 #plot 1, left side sns.barplot(values.index, values.values, palette="Blues_d", ax=ax1) ax1.set_xticklabels(data.columns.values[:6],rotation=45, fontsize=16) ax1.yaxis.set_ticks(np.arange(-100, 160, 20)) ax1.set_title("Performance over average(%)", fontsize=18) ax1.set_xlabel(r'Features', fontsize=16) ax1.set_ylabel("Performance over average(%)", fontsize=16) ax1.grid(True, axis='y') ax1.set_ylim(-100,150) #plot 2, top-right data_dict={} for name in data.columns[:6]: data_dict[name]=data[name].rank()[(car)]/len(data)*100 sns.barplot(list(data_dict.values()),list(data_dict.keys()), palette="Blues_d", orient="h", ax=ax3) ax3.set_yticklabels(data.columns.values[:6],rotation=45, fontsize=14) ax3.xaxis.set_ticks(np.arange(0, 110, 10)) ax3.set_title(" Ranking Position (%)", fontsize=16) ax3.set_xlabel("Top percent (%)", fontsize=14) ax3.set_xlim(0,100) ax3.set_ylabel("Features", fontsize=14) ax3.grid(True, axis='x') col_labels = ["Your car", "Type"] row_labels = data.columns[:6] table_vals =np.transpose(np.array([data.iloc[car,:6].tolist(), data.iloc[car,6:12].tolist()])) # Draw table the_table = ax2.table(cellText=table_vals, colWidths=[0.1] * 2, rowLabels=row_labels, colLabels=col_labels,loc='center',rowColours=sns.color_palette("Blues_d", 6)) the_table.auto_set_font_size(True) the_table.set_fontsize(15) the_table.scale(2, 3) # Removing ticks and spines enables you to get the figure only with table ax2.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False) ax2.tick_params(axis='y', which='both', right=False, left=False, labelleft=False) ax2.grid(False) ax2.axis('off') for pos in ['right','top','bottom','left']: plt.gca().spines[pos].set_visible(False) plt.show() #iteractive features w=interactive_output(compare, {"car_":car_bt, "MPG":MPG_bt, "Cylinders":Cylinders_bt, "Displacement":Displacement_bt, "Horsepower":Horsepower_bt, "Weight":Weight_bt, "Acceleration":Acceleration_bt}) hbox1 = HBox([car_bt]) hbox2 = HBox([MPG_bt, Cylinders_bt, Displacement_bt]) hbox3 = HBox([Horsepower_bt, Weight_bt, Acceleration_bt]) ui = VBox([hbox1, hbox2, hbox3]) display(ui, w)
class UserInterface: """ A user interface used by the clean_duplication function. """ # pylint: disable=too-many-instance-attributes _clusterer: Clusterer _page_size: int _clustering_method_label: Label _clustering_method_drop: Dropdown _export_code: Checkbox _sel_all: Checkbox _next_button: Button _prev_button: Button _page_pos: int _ngram_text: Text _radius_text: Text _block_chars_text: Text _dropds: HBox _reprs: List[Text] _checks: List[Checkbox] _cluster_vbox: VBox _box: Box _loading_label: Label _invalid_param_label: Label def __init__(self, df: pd.DataFrame, col_name: str, df_name: str, page_size: int): self._clusterer = Clusterer(df, col_name, df_name) self._clusterer.cluster("fingerprint") self._page_size = page_size # clustering dropdown and export code checkbox, used in the top row self._clustering_method_label = Label( " Clustering Method: ", layout=Layout(margin="2px 0 0 20px")) self._clustering_method_drop = Dropdown( options=[ "fingerprint", "ngram-fingerprint", "phonetic-fingerprint", "levenshtein" ], layout=Layout(width="150px", margin="0 0 0 10px"), ) self._clustering_method_drop.observe(self._cluster_method_change, names="value") self._export_code = Checkbox( value=True, description="export code", layout=Layout(width="165px", margin="0 0 0 482px"), style={"description_width": "initial"}, ) self._dropds = HBox( [ self._clustering_method_label, self._clustering_method_drop, self._export_code, ], layout=Layout(height="35px", margin="10px 0 0 0"), ) # text boxes for clustering parameters used in the top row self._ngram_text = Text( value=DEFAULT_NGRAM, description="n-gram", layout=Layout(width="130px"), continuous_update=False, ) self._radius_text = Text( value=DEFAULT_RADIUS, description="Radius", layout=Layout(width="130px"), continuous_update=False, ) self._block_chars_text = Text( value=DEFAULT_BLOCK_SIZE, description="Block Chars", layout=Layout(width="130px"), continuous_update=False, ) self._ngram_text.observe(self._param_recluster, names="value") self._radius_text.observe(self._param_recluster, names="value") self._block_chars_text.observe(self._param_recluster, names="value") # create header labels, second row headers = HBox( [ Label("Distinct values", layout=Layout(margin="0 0 0 10px")), Label("Total values", layout=Layout(margin="0 0 0 35px")), Label("Cluster values", layout=Layout(margin="0 0 0 95px")), Label("Merge?", layout=Layout(margin="0 0 0 295px")), Label("Representative value", layout=Layout(margin="0 0 0 50px")), ], layout=Layout(margin="10px"), ) # create buttons for bottom row self._sel_all = Checkbox(description="Select all", layout=Layout(width="165px")) self._sel_all.observe(self._select_all, names="value") merge_and_recluster = Button(description="Merge and Re-Cluster", layout=Layout(margin="0 0 0 466px", width="150px")) merge_and_recluster.on_click(self._execute_merge) finish = Button(description="Finish", layout=Layout(margin="0 0 0 10px")) finish.on_click(self._close) # next and previous page buttons self._next_button = Button(description="Next") self._next_button.on_click(self._next_page) self._prev_button = Button(description="Previous", layout=Layout(margin="0 0 0 20px")) self._prev_button.on_click(self._prev_page) # an index in the clusters Series indicating the start of the current page self._page_pos = 0 # loading label, displayed when re-clustering or next page load self._loading_label = Label("Loading...", layout=Layout(margin="170px 0 0 440px")) # displayed when the user enters a non integer value into a clustering parameter text box self._invalid_param_label = Label( "Invalid clustering parameter, please enter an integer", layout=Layout(margin="170px 0 0 350px"), ) self._reprs = [ Text(layout=Layout(width="200px", margin="0 10px 0 40px")) for _ in range(self._page_size) ] self._checks = [ Checkbox(indent=False, layout=Layout(width="auto", margin="0 0 0 20px")) for _ in range(self._page_size) ] # VBox containing a VBox with all the clusters in the first row and an optional # second row containing next and previous page buttons self._cluster_and_next_prev = VBox() self._cluster_vbox = VBox( layout=Layout(height="450px", flex_flow="row wrap")) footer = HBox([self._sel_all, merge_and_recluster, finish]) box_children = [ self._dropds, headers, self._cluster_and_next_prev, footer ] box_layout = Layout(display="flex", flex_flow="column", align_items="stretch", border="solid") self._box = Box(children=box_children, layout=box_layout) self._update_clusters() def _update_clusters(self) -> None: """ Updates the clusters currently being displayed. """ line = HBox( children=[Label("-" * 186, layout=Layout(margin="0 0 0 18px"))]) self._sel_all.value = False cluster_page = self._clusterer.get_page( self._page_pos, self._page_pos + self._page_size) label_layout = Layout(height="22px", width="360px") box_children = [line] for idx, cluster in enumerate(cluster_page): labels = [] for cluster_val, cnt in cluster: if cnt > 1: cluster_val += f" ({cnt} rows)" labels.append(Label(cluster_val, layout=label_layout)) totals_vals = sum(cnt for _, cnt in cluster) distinct_vals = len(cluster) self._reprs[idx].value = cluster[0][0] self._checks[idx].value = False box_children.append( HBox([ Label(str(distinct_vals), layout=Layout(width="60px", margin="0 0 0 60px")), Label(str(totals_vals), layout=Layout(width="60px", margin="0 0 0 50px")), VBox(children=labels, layout=Layout(margin="0 0 0 80px")), self._checks[idx], self._reprs[idx], ])) box_children.append(line) # no clusters to display if len(cluster_page) == 0: box_children = [ Label( "No clusters, try a different clustering method", layout=Layout(margin="170px 0 0 360px"), ) ] self._cluster_vbox.children = box_children cluster_and_next_prev = [self._cluster_vbox] self._add_next_prev_button_row(cluster_and_next_prev) self._cluster_and_next_prev.children = cluster_and_next_prev def _update_dropds(self, clustering_method: str) -> None: """ Update the dropdowns row of the UI to display the required text boxes for passing parameters needed for the given clustering method. """ if clustering_method in ("fingerprint", "phonetic-fingerprint"): self._export_code.layout.margin = "0 0 0 482px" self._dropds.children = [ self._clustering_method_label, self._clustering_method_drop, self._export_code, ] if clustering_method == "ngram-fingerprint": self._export_code.layout.margin = "0 0 0 348px" self._dropds.children = [ self._clustering_method_label, self._clustering_method_drop, self._ngram_text, self._export_code, ] if clustering_method == "levenshtein": self._export_code.layout.margin = "0 0 0 214px" self._dropds.children = [ self._clustering_method_label, self._clustering_method_drop, self._radius_text, self._block_chars_text, self._export_code, ] def _param_recluster(self, _: Dict[str, Any]) -> None: """ Re-cluster the dataframe with the new clustering parameters. Triggered when the value in a clustering parameter textbox is changed. """ self._display_message(self._loading_label) try: self._clusterer.set_cluster_params(*self._cluster_params()) cluster_method = self._clustering_method_drop.value self._clusterer.cluster(cluster_method) self._page_pos = 0 self._update_clusters() except ValueError: self._display_message(self._invalid_param_label) def _cluster_params(self) -> Tuple[int, int, int]: """ Retrieve clustering parameters from their respective text boxes. """ ngram = self._ngram_text.value if self._ngram_text.value else DEFAULT_NGRAM radius = self._radius_text.value if self._radius_text.value else DEFAULT_RADIUS block_size = self._block_chars_text.value if self._block_chars_text else DEFAULT_BLOCK_SIZE return int(ngram), int(radius), int(block_size) def _select_all(self, change: Dict[str, Any]) -> None: """ Triggered when the select all checkbox is selected or unselected. Changes the value of the cluster checkboxes to match the state of the select all checkbox. """ for check in self._checks: check.value = change["new"] def _cluster_method_change(self, change: Dict[str, Any]) -> None: """ Triggered when the cluster method dropdown state is changed. Re-clusters the DataFrame with the new clustering method. """ self._update_dropds(change["new"]) self._display_message(self._loading_label) cluster_method = self._clustering_method_drop.value self._clusterer.cluster(cluster_method) self._page_pos = 0 self._update_clusters() def _add_next_prev_button_row( self, box_children: List[Union[HBox, VBox]]) -> None: """ Adds a next page or previous page button, if the operation is valid. """ next_prev = [] prev_is_valid = self._page_pos - self._page_size >= 0 next_is_valid = self._page_pos + self._page_size < len( self._clusterer.clusters) if prev_is_valid and next_is_valid: self._next_button.layout.margin = "0 0 0 628px" next_prev.append(self._prev_button) next_prev.append(self._next_button) elif prev_is_valid: next_prev.append(self._prev_button) elif next_is_valid: self._next_button.layout.margin = "0 0 0 795px" next_prev.append(self._next_button) if next_is_valid or prev_is_valid: box_children.append(HBox(next_prev, layout={"height": "50px"})) def _next_page(self, _: Dict[str, Any]) -> None: """ Display the next page of clusters by increasing the page position. """ self._display_message(self._loading_label) self._page_pos += self._page_size self._update_clusters() self._sel_all_on_page() def _prev_page(self, _: Dict[str, Any]) -> None: """ Display the previous page of clusters by decreasing the page position. """ self._display_message(self._loading_label) self._page_pos -= self._page_size self._update_clusters() self._sel_all_on_page() def _display_message(self, message_label: Label) -> None: """ Display a message to the user, used for the loading screen and invalid clustering parameter screen """ self._cluster_vbox.children = [message_label] # don't display next and prev buttons self._cluster_and_next_prev.children = [ self._cluster_and_next_prev.children[0] ] def _sel_all_on_page(self) -> None: """ Select all checkboxes on the current page of clusters if the select all checkbox is selected. """ if self._sel_all.value: for check in self._checks: check.value = True def _close(self, _: Dict[str, Any]) -> None: """ Close the UI and display the final dataframe in the next jupyter notebook cell. """ self._clusterer.final_df() self._box.close() def _execute_merge(self, _: Dict[str, Any]) -> None: """ Merge the selected clusters and re-cluster the dataframe. If the export code checkbox is selected the required replace function calls and add them to the jupyter notebook cell. """ self._display_message(self._loading_label) do_merge = [check.value for check in self._checks] new_values = [text.value for text in self._reprs] cluster_page = self._clusterer.get_page( self._page_pos, self._page_pos + self._page_size) if self._export_code.value: self._clusterer.live_export_code(cluster_page, do_merge, new_values) cluster_method = self._clustering_method_drop.value self._clusterer.execute_merge_code(cluster_page, do_merge, new_values) self._clusterer.cluster(cluster_method) self._update_page_if_empty() self._update_clusters() def _update_page_if_empty(self) -> None: """ Decrease the page if the last page is empty. Needed for when all clusters on the last page are merged. """ if self._page_pos >= len(self._clusterer.clusters): self._page_pos -= self._page_size def display(self) -> Box: """Display the UI.""" return self._box
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, df: pd.DataFrame, col_name: str, df_name: str, page_size: int): self._clusterer = Clusterer(df, col_name, df_name) self._clusterer.cluster("fingerprint") self._page_size = page_size # clustering dropdown and export code checkbox, used in the top row self._clustering_method_label = Label( " Clustering Method: ", layout=Layout(margin="2px 0 0 20px")) self._clustering_method_drop = Dropdown( options=[ "fingerprint", "ngram-fingerprint", "phonetic-fingerprint", "levenshtein" ], layout=Layout(width="150px", margin="0 0 0 10px"), ) self._clustering_method_drop.observe(self._cluster_method_change, names="value") self._export_code = Checkbox( value=True, description="export code", layout=Layout(width="165px", margin="0 0 0 482px"), style={"description_width": "initial"}, ) self._dropds = HBox( [ self._clustering_method_label, self._clustering_method_drop, self._export_code, ], layout=Layout(height="35px", margin="10px 0 0 0"), ) # text boxes for clustering parameters used in the top row self._ngram_text = Text( value=DEFAULT_NGRAM, description="n-gram", layout=Layout(width="130px"), continuous_update=False, ) self._radius_text = Text( value=DEFAULT_RADIUS, description="Radius", layout=Layout(width="130px"), continuous_update=False, ) self._block_chars_text = Text( value=DEFAULT_BLOCK_SIZE, description="Block Chars", layout=Layout(width="130px"), continuous_update=False, ) self._ngram_text.observe(self._param_recluster, names="value") self._radius_text.observe(self._param_recluster, names="value") self._block_chars_text.observe(self._param_recluster, names="value") # create header labels, second row headers = HBox( [ Label("Distinct values", layout=Layout(margin="0 0 0 10px")), Label("Total values", layout=Layout(margin="0 0 0 35px")), Label("Cluster values", layout=Layout(margin="0 0 0 95px")), Label("Merge?", layout=Layout(margin="0 0 0 295px")), Label("Representative value", layout=Layout(margin="0 0 0 50px")), ], layout=Layout(margin="10px"), ) # create buttons for bottom row self._sel_all = Checkbox(description="Select all", layout=Layout(width="165px")) self._sel_all.observe(self._select_all, names="value") merge_and_recluster = Button(description="Merge and Re-Cluster", layout=Layout(margin="0 0 0 466px", width="150px")) merge_and_recluster.on_click(self._execute_merge) finish = Button(description="Finish", layout=Layout(margin="0 0 0 10px")) finish.on_click(self._close) # next and previous page buttons self._next_button = Button(description="Next") self._next_button.on_click(self._next_page) self._prev_button = Button(description="Previous", layout=Layout(margin="0 0 0 20px")) self._prev_button.on_click(self._prev_page) # an index in the clusters Series indicating the start of the current page self._page_pos = 0 # loading label, displayed when re-clustering or next page load self._loading_label = Label("Loading...", layout=Layout(margin="170px 0 0 440px")) # displayed when the user enters a non integer value into a clustering parameter text box self._invalid_param_label = Label( "Invalid clustering parameter, please enter an integer", layout=Layout(margin="170px 0 0 350px"), ) self._reprs = [ Text(layout=Layout(width="200px", margin="0 10px 0 40px")) for _ in range(self._page_size) ] self._checks = [ Checkbox(indent=False, layout=Layout(width="auto", margin="0 0 0 20px")) for _ in range(self._page_size) ] # VBox containing a VBox with all the clusters in the first row and an optional # second row containing next and previous page buttons self._cluster_and_next_prev = VBox() self._cluster_vbox = VBox( layout=Layout(height="450px", flex_flow="row wrap")) footer = HBox([self._sel_all, merge_and_recluster, finish]) box_children = [ self._dropds, headers, self._cluster_and_next_prev, footer ] box_layout = Layout(display="flex", flex_flow="column", align_items="stretch", border="solid") self._box = Box(children=box_children, layout=box_layout) self._update_clusters()
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 SoundDemo1Dashboard1: def __init__(self): """ :param int i: """ self.select_training_data_options = None self.select_training_data = Dropdown( options={'Record training data': 0, 'Load training data files': 1 }, value=0, description='Choose training data:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_training_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.select_nclasses = Dropdown( options={'2': 2, '3': 3, '4': 4 }, value=2, description='Choose number of classes:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.select_nfiles = Dropdown( options={'12': 12, '8': 8, '6': 6, '4': 4 }, value=12, description='Choose number of files:', disabled=False, layout=Layout(width='400px'), style={'description_width': '160px'}, ) self.container = Output( value="", ) self.submit_button = Button( value=False, description='Get data', disabled=False, button_style='success', # 'success', 'info', 'warning', 'danger' or '' tooltip='TRAIN: 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='TRAIN: play recorded or loaded sounds', icon='' ) self.widget_box = VBox( ( HBox( ( VBox(( self.select_training_data, self.select_nseconds, self.select_nclasses, self.select_nfiles ), ), 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.data: # instantiate pyaudio p = pyaudio.PyAudio() i = 0 for d in enumerate(self.data[0]): i = i + 1 # instantiate stream stream = p.open(format=pyaudio.paFloat32, # float32 insteda of paInt16 because librosa loads in float32 channels=1, rate=22050, output=True, ) print("Playing example {}, label {} ...".format(i, self.data[1][i - 1])) time.sleep(1) try: stream.write(self.data[0][i - 1], len(self.data[0][i - 1])) except Exception: print("Error:", sys.exc_info()[0]) stream.stop_stream() stream.close() p.terminate() else: print("No training data has been loaded. Please load the training data first.") def _run(self, v): self.prefix = "training" self.recorder = Recorder(n_classes=self.select_nclasses.value, n_files=self.select_nfiles.value, prefix=self.prefix, wav_dir='tmp') if self.select_training_data.value != 0: # Remove the data with the same prefix print("Loading the recorded data..") try: files = self.recorder.get_files() s = [True if self.prefix in file else False for file in files] except Exception: print("Loading failed. No files were found.") return if True in s: self.data = self.recorder.create_dataset() else: print("Loading failed. No relevant files were found.") return # If we choose to record the data, then we record it here if self.select_training_data.value == 0: print("Recording the training data..") # Remove the data with the same prefix files = self.recorder.get_files() for file in files: if self.prefix in file: os.remove(file) # Start recording self.recorder.record(seconds=self.select_nseconds.value, clear_output=False) self.data = self.recorder.create_dataset() print("Data has been loaded.") 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)
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 RGB_colourspace_models_transformation_matrix_widget(): title_Label = Label('RGB Colourspace Models Transformation Matrix') title_Label.add_class('widget-title') default_layout = {'flex': '1 1 auto', 'width': 'auto'} input_colourspace_Dropdown = Dropdown( options=sorted(colour.RGB_COLOURSPACES), layout=default_layout) output_colourspace_Dropdown = Dropdown( options=sorted(colour.RGB_COLOURSPACES), layout=default_layout) chromatic_adaptation_transforms_Dropdown = Dropdown( options=sorted(colour.CHROMATIC_ADAPTATION_TRANSFORMS), layout=default_layout) formatter_Dropdown = Dropdown( options=['str', 'repr', 'Nuke'], layout=default_layout) decimals_IntSlider = IntSlider(value=10, max=15, layout=default_layout) RGB_to_RGB_matrix_Textarea = Textarea(rows=3, layout=default_layout) RGB_to_RGB_matrix_Textarea.add_class('widget-output-textarea') def set_RGB_to_RGB_matrix_Textarea(): M = colour.RGB_to_RGB_matrix( colour.RGB_COLOURSPACES[input_colourspace_Dropdown.value], colour.RGB_COLOURSPACES[output_colourspace_Dropdown.value], chromatic_adaptation_transforms_Dropdown.value) with colour.utilities.numpy_print_options( formatter={ 'float': ('{{:0.{0}f}}'.format(decimals_IntSlider.value)).format }, threshold=np.nan): if formatter_Dropdown.value == 'str': M = str(M) elif formatter_Dropdown.value == 'repr': M = repr(M) else: M = NUKE_COLORMATRIX_NODE_TEMPLATE.format( nuke_format_matrix(M, decimals_IntSlider.value), '{0}_to_{1}'.format( input_colourspace_Dropdown.value.replace(' ', '_'), output_colourspace_Dropdown.value.replace(' ', '_'))) RGB_to_RGB_matrix_Textarea.rows = len(M.split('\n')) RGB_to_RGB_matrix_Textarea.value = M def on_input_change(change): if change['type'] == 'change' and change['name'] == 'value': set_RGB_to_RGB_matrix_Textarea() input_colourspace_Dropdown.observe(on_input_change) output_colourspace_Dropdown.observe(on_input_change) chromatic_adaptation_transforms_Dropdown.observe(on_input_change) formatter_Dropdown.observe(on_input_change) decimals_IntSlider.observe(on_input_change) set_RGB_to_RGB_matrix_Textarea() widget = Box([ VBox( [ title_Label, Textarea( 'This widget computes the colour transformation ' 'matrix from the Input RGB Colourspace to the ' 'Output RGB Colourspace using the given ' 'Chromatic Adaptation Transform.', rows=2, layout=default_layout), Label('Input RGB Colourspace'), input_colourspace_Dropdown, Label('Output RGB Colourspace'), output_colourspace_Dropdown, Label('Chromatic Adaptation Transform'), chromatic_adaptation_transforms_Dropdown, Label('Formatter'), formatter_Dropdown, HBox([Label('Decimals:'), decimals_IntSlider]), Label('RGB Transformation Matrix'), RGB_to_RGB_matrix_Textarea, ], layout={ 'border': 'solid 2px', 'width': '55%' }) ]) return widget
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.use_recorded = RadioButtons( options=['Webcam', 'MP4'], value='Webcam', description='Input', disabled=False, layout=Layout(width='320px'), style={'description_width': '150px'}, ) self.video_path = Text( value='', placeholder=str(Path("path", "to", "video.mp4")), description='Video path:', disabled=True, layout=Layout(width='320px'), style={'description_width': '150px'}, ) self.select_network = Dropdown( options=KerasDetector.available_models, value=KerasDetector.available_models[0], description='Algorithm:', disabled=False, layout=Layout(width='220px'), style={'description_width': '100px'}, ) self.select_frontend = Dropdown( options=["opencv", "matplotlib"], value="opencv", description='Frontend:', disabled=False, layout=Layout(width='220px', padding='8px 0 0 0'), style={'description_width': '100px'}, ) self.video_length = FloatText( value=12.0, description='Video length [s]:', disabled=False, layout=Layout(width='250px'), style={'description_width': '130px'}, ) self.select_framerate = Dropdown( options=[3, 5, 10, 15, 24], value=15, description='Frame rate:', disabled=False, layout=Layout(width='250px', padding='8px 0 0 0'), style={'description_width': '130px'}, ) self.static_text = Label( value="Working directory: {}".format(Path.cwd()), layout=Layout(margin='30px 0 0 0'), ) self.progress_text = Label(value='', ) self.text_box = VBox((self.static_text, self.progress_text), layout=Layout(justify_content="flex-start")) self.widget_box = VBox( (HBox((VBox((self.start_button, ), layout=Layout(justify_content="space-around")), VBox((self.use_recorded, self.video_path), layout=Layout(justify_content="space-around")), VBox((self.video_length, self.select_framerate), layout=Layout(justify_content="space-around")), VBox((self.select_network, self.select_frontend), layout=Layout(justify_content="space-around"))), ), HBox((self.text_box, ), layout=Layout(justify_content="flex-start"))), ) self.start_button.on_click(self._start_video) self.use_recorded.observe(self.refresh_state)