def __plot_cell(description, bold=False): if bold: return Box([Label(description)], layout=Layout(border='solid 2px', width='100%')) else: return Box([Label(description)], layout=Layout(border='solid 1px', width='100%'))
def create_rubrics_feedback(self, answer_sample_object, grade_button): def on_feedback_change(name, change): answer_sample_object.update(name, change) grade_button.description = 'grade : ' \ + str(answer_sample_object.grade) def on_feedback_text_change(name, change): answer_sample_object.feedback_update(name, change) items_layout = Layout(width='auto') box_layout = Layout(display='flex', flex_flow='column', align_items='stretch') row_layout = Layout(display='flex', flex_flow='row', align_items='stretch') combined_list = [] for point in range(answer_sample_object.points): c = \ widgets.Checkbox(name=answer_sample_object.feedback_checkbox[point], value=False, description='-1', disabled=False, layout=items_layout) c.observe(lambda x, point=point: on_feedback_change(point, x), names='value') d = widgets.Text(name=point, value=answer_sample_object.feedback[point], placeholder='Type something', layout=items_layout) d.observe(lambda x, point=point: \ on_feedback_text_change(point, x), names='value') combined_list.append(Box([c, d], layout=row_layout)) return Box(combined_list, layout=box_layout)
def build(fragments): result = [] for fragment in fragments: (nType, node) = fragment[0] children = fragment[1:] ws = [HTML(value=nType, layout=layoutA)] feats = featsFromType[nType] for feat in featOrder: if feat != "_" and feat not in feats: continue wf = widgets.get(feat, {}).get(node, None) if wf is not None: ws.append(wf) nodeResult = Box(ws, layout=layoutV) if len(ws) > 1 else ws[0] childrenResult = build(children) if childrenResult: thisResult = Box([nodeResult, childrenResult], layout=layoutV2) else: thisResult = nodeResult result.append(thisResult) if len(result) == 0: return None if len(result) == 1: return result[0] return Box(result, layout=layoutH)
def make_box_for_grid(image_widget, title, size=(250, 250)): """ Make a VBox to hold caption/image for demonstrating option_fit values. """ modes = ['oblique', 'X', 'Y', 'Z'] # make layout box_layout = Layout() box_layout.width = f'{size[0]}px' box_layout.height = f'{size[1]}px' box_layout.border = '0px' # Make the caption if title is not None: title_str = "'{}'".format(title) else: title_str = str(title) h = HTML(value='' + str(title_str) + '') # Make the box with the image widget inside it boxb = Box() boxb.layout = box_layout boxb.children = [image_widget] # Compose into a vertical box vb = VBox() vb.layout.align_items = 'center' vb.children = [h, boxb] return vb
def carousel(children=(), **layout): "A horizontally scrolling carousel" def_layout = dict(overflow='scroll hidden', flex_flow='row', display='flex') res = Box([], layout=merge(def_layout, layout)) res.observe(_update_children, names='children') res.children = children return res
def __init__(self, tool, rkwargs=None, ipyclient=None, show_cluster=True, quiet=False, auto=False): # if no kwargs then empty dict if rkwargs is None: rkwargs = {} self._quiet = quiet # the tool with a ._run() func and its run kwargs to be parallelized self.tool = tool self.rkwargs = rkwargs # parallel client connect or launch params self.ipyclient = ipyclient self.show_cluster = show_cluster self.auto = auto # setup the widget message to be passed on to _run self.message = HTML(layout={"height": "25px", "margin": "0px"}) self.widget = Box(children=[self.message], layout={"margin": "5px 0px 5px 0px"}) self.update_message("Establishing parallel connection: ...") if not self._quiet: # show the widget message display(self.widget)
def run_colab(population_size, network_pkl): G = load_network(network_pkl) population = genetic_algo.Population(G, population_size) phenotypes = _generate_phenotypes(population, G) board = Box(children=phenotypes, layout=Layout(display="flex", flex_flow="row wrap")) mutationInput = widgets.BoundedFloatText(value=0.01,min=0.0,max=1.0,step=0.01, layout=Layout(width="100px")) button = widgets.Button(description="Update") button.on_click(lambda _: _update(population, mutationInput, G, board, loading, button)) mutationInputBox = widgets.VBox([Label("Taxa de mutação:"), mutationInput]) loading = widgets.FloatProgress( value=100, min=0, max=100, bar_style='info', style={'bar_color': '#008080'}, orientation='horizontal' ) bottomBox = Box( children=[mutationInputBox, loading, button], layout=Layout( display="flex", width="100%", justify_content="space-between", align_items="flex-end", padding="0 15% 20px 0", ), ) app = widgets.VBox([board, bottomBox]) display(app)
def widget1(): # True values # w1 = np.array([0.64, 0.41]) # w2 = np.array([0.053, -0.71]) # w3 = np.array([-0.67, 0.33]) w11 = FloatText(value=0.64, description=r'\(w_{11}^{(1)} \)') w12 = FloatText(value=0.41, description=r'\(w_{12}^{(1)} \)') w10 = FloatText(value=-1.5, description=r'\(w_{10}^{(1)} \)') W1 = [w11, w12, w10] w21 = FloatText(value=0.053, description=r'\(w_{21}^{(1)} \)') w22 = FloatText(value=-0.71, description=r'\(w_{22}^{(1)} \)') w20 = FloatText(value=-1.5, description=r'\(w_{20}^{(1)} \)') W2 = [w21, w22, w20] w31 = FloatText(value=-0.67, description=r'\(w_{31}^{(1)} \)') w32 = FloatText(value=0.33, description=r'\(w_{32}^{(1)} \)') w30 = FloatText(value=-1.5, description=r'\(w_{30}^{(1)} \)') W3 = [w31, w32, w30] box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='100%') box_1 = Box(children=W1, layout=box_layout) box_2 = Box(children=W2, layout=box_layout) box_3 = Box(children=W3, layout=box_layout) display(VBox([box_1, box_2, box_3])) return [W1, W2, W3]
def __init__(self, a_pid): """通过进程pid初始化ui组件""" self.progress_widget = FloatProgress(value=0, min=0, max=100) self.text_widget = Text('pid={} begin work'.format(a_pid)) # 通过box容器都放到一个里面 self.progress_box = Box([self.text_widget, self.progress_widget]) display(self.progress_box)
def getBox(model): if model == 'Arbol' or model == 'Bosque': main_box = Box(children=[boxGeneral, boxArboles], layout=main_box_layout) elif model == 'Bayes': main_box = Box(children=[boxGeneral, boxBayes], layout=main_box_layout) else: # KNN main_box = Box(children=[boxGeneral, boxKNN], layout=main_box_layout) return main_box
def _relayout_master(box, width='20%'): """make nicer layout for box. This method will take the `description` of each child to make corresponding Label The `description` will be cleared. """ old_children = box.children[:] form_items = _relayout(box, make_form_item_layout()) form = Box(form_items, layout=_make_box_layout(width=width)) form._ngl_children = old_children return form
def box(self): jslink((self.play, 'value'), (self.step_slider, 'value')) jslink((self.play, 'interval'), (self.fps_slider, 'value')) self.play.observe(self.replot, 'value') return VBox([ Box([self.play, self.step_slider, self.fps_slider]), Box( children=tuple(self.plots.values()), layout=Layout(display='flex', flex_flow='column') ) ])
def carousel( children: (tuple, list) = (), # `Box` objects to display in a carousel **layout ) -> Box: # An `ipywidget`'s carousel "A horizontally scrolling carousel" def_layout = dict(overflow='scroll hidden', flex_flow='row', display='flex') res = Box([], layout=merge(def_layout, layout)) res.observe(_update_children, names='children') res.children = children return res
def __init__(self): micron_units = Label('micron') # use "option m" (Mac, for micro symbol) constWidth = '180px' tab_height = '500px' stepsize = 10 #style = {'description_width': '250px'} style = {'description_width': '25%'} layout = {'width': '400px'} name_button_layout={'width':'25%'} widget_layout = {'width': '15%'} units_button_layout ={'width':'15%'} desc_button_layout={'width':'45%'} param_name1 = Button(description='tumor_radius', disabled=True, layout=name_button_layout) param_name1.style.button_color = 'lightgreen' self.tumor_radius = FloatText( value=750, step=10, style=style, layout=widget_layout) param_name2 = Button(description='enable_pH_effects', disabled=True, layout=name_button_layout) param_name2.style.button_color = 'tan' self.enable_pH_effects = Checkbox( value=False, disabled=True, style=style, layout=widget_layout) units_button1 = Button(description='micron', disabled=True, layout=units_button_layout) units_button1.style.button_color = 'lightgreen' units_button2 = Button(description='', disabled=True, layout=units_button_layout) units_button2.style.button_color = 'tan' desc_button1 = Button(description='', disabled=True, layout=desc_button_layout) desc_button1.style.button_color = 'lightgreen' desc_button2 = Button(description='(disabled in this version)', disabled=True, layout=desc_button_layout) desc_button2.style.button_color = 'tan' row1 = [param_name1, self.tumor_radius, units_button1, desc_button1] row2 = [param_name2, self.enable_pH_effects, units_button2, desc_button2] box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='100%') box1 = Box(children=row1, layout=box_layout) box2 = Box(children=row2, layout=box_layout) self.tab = VBox([ box1, box2, ])
def dashboard_grading(self): single_question_output = \ widgets.Output(description='Single Question') if self.check_folder_structure(self.nbgrader_folder_path, self.exam_file_name): self.parse_jupyter_notebooks(self.nbgrader_folder_path, self.exam_file_name) else: print('Please select the proper folder') pass clear_output() row_layout = Layout(display='Flex', flex_flow='row', align_items='stretch', width='100%') col_layout = Layout(display='flex', flex_flow='column', align_items='stretch', border='solid', width='100%') previous_question_button = \ Button(description='Previous Question', layout=Layout(width='50%')) next_question_button = Button(description='Next Question', layout=Layout(width='50%')) with single_question_output: clear_output() question_view = self.single_question_grading_view() display(question_view) row_2 = Box([previous_question_button, next_question_button], layout=row_layout) complete = Box([row_2, single_question_output], layout=col_layout) display(complete) def on_next_question_button(change): with single_question_output: clear_output() question_view = self.single_question_grading_view( 'next_question') display(question_view) def on_previous_question_button(change): with single_question_output: clear_output() question_view = self.single_question_grading_view( 'previous_question') display(question_view) next_question_button.on_click(on_next_question_button) previous_question_button.on_click(on_previous_question_button)
def display(self, gui=False, use_box=False): if gui: if use_box: box = Box([self, self.player._display()]) box._gui_style = 'row' return box else: display(self) display(self.player._display()) return None else: return self
def contentsViewer(allDict, topics): topicIdMap = getTopicIdDict( allDict) # Initialize one time to map topics to ids intro = widgets.Label( value= r'\(\textbf{Pre-select a topic and choose an article to view its contents}\)' ) topicdd = widgets.Dropdown(options=unzipLeftSide(topics), description='Topics:', layout=Layout(flex='1 1 auto', width='30%'), disabled=False) docs = widgets.Dropdown(description='RSS Entries:', layout=Layout(flex='1 1 auto', width='70%'), disabled=False) ta = widgets.Textarea(description='Content:', rows=10, layout=Layout(flex='1 1 100%', width='auto'), disabled=True) items_auto = [topicdd, docs] items_0 = [ta] box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='80%') box_auto = Box(children=items_auto, layout=box_layout) box_0 = Box(children=items_0, layout=box_layout) def updateDoclist(b): newDocs = getNewRSSEntryList(allDict, topicIdMap, topicdd.value) if bool(newDocs): docs.options = getNewRSSEntryList(allDict, topicIdMap, topicdd.value) def get_and_plot(b): dispTxt = headerSentiText(allDict[docs.value]) + "\n" + allDict[ docs.value]["collatedContents"] ta.value = dispTxt # Initialize dropdown docs.options = getNewRSSEntryList(allDict, topicIdMap, topicdd.value) topicdd.observe(updateDoclist, names='value') docs.observe(get_and_plot, names='value') display(VBox([intro, box_auto, box_0])) updateDoclist(None) # just for initialization get_and_plot(None) # just for initialization return
def reinit(self, products): self.products = products self.product_select.options = [(f"{val.description} [{val.unit}]", key) for key, val in products.items() if len(val.shape) == 2] r_bins = phys.radius(volume=self.setup.v_bins) const.convert_to(r_bins, const.si.micrometres) self.spectrumOutput = Output() with self.spectrumOutput: self.spectrumPlot = _SpectrumPlot(r_bins) clear_output() self.plots = {} self.outputs = {} for key, product in products.items(): if len(product.shape) == 2: self.outputs[key] = Output() with self.outputs[key]: self.plots[key] = _ImagePlot(self.setup.grid, self.setup.size, product) clear_output() self.plot_box = Box() if len(products.keys()) > 0: layout_flex_end = Layout(display='flex', justify_content='flex-end') save_map = Button(icon='save') save_map.on_click(self.handle_save_map) save_spe = Button(icon='save') save_spe.on_click(self.handle_save_spe) self.plots_box.children = (HBox(children=( VBox(children=( Box(layout=layout_flex_end, children=(save_map, )), HBox((self.slider['Z'], self.plot_box)), HBox((self.slider['X'], ), layout=layout_flex_end))), VBox(layout=Layout(), children=(save_spe, self.spectrumOutput)))), ) for widget in (self.step_slider, self.play): widget.value = 0 widget.max = len(self.setup.steps) - 1 for j, xz in enumerate(('X', 'Z')): slider = self.slider[xz] mx = self.setup.grid[j] slider.max = mx slider.value = (0, mx) self.replot()
def __create_exp_progress_box(name, exp_progress, rep_progress, show_full_progress=False): exp_progress_layout = Layout(display='flex', flex_flow='column', align_items='stretch', width='100%') exp_progress_bar = HBox([FloatProgress(value=exp_progress, min=.0, max=1., bar_style='info'), Label(name)]) if show_full_progress: rep_progress_layout = Layout(display='flex', flex_flow='column', align_items='stretch', align_self='flex-end', width='80%') items = [FloatProgress(value=p, min=.0, max=1., description=str(i)) for i, p in enumerate(rep_progress)] rep_progress_box = Box(children=items, layout=rep_progress_layout) return Box(children=[exp_progress_bar, rep_progress_box], layout=exp_progress_layout) else: return exp_progress_bar
def initialize_widget(self): '''Setup layout of the widget''' #define span and size of widgets title_layout = Layout( display='flex', flex_flow='column', justify_content='flex-end', width='100%', height='70', ) fullspan_layout = Layout( display='flex', flex_flow='row', align_items='stretch', justify_content='center', width='100%', height='70', ) eighty_span_layout = Layout( display='flex', flex_flow='row', justify_content='flex-start', width='80%', height='280px', ) twenty_span_layout = Layout( display='flex', flex_flow='column', justify_content='center', width='20%', height='280px', ) #create widgets using layout self.set_title('') self.title_box = w.Box(children=self.title_items, layout=title_layout) self.filter_box = w.Box(children=self.filter_items, layout=fullspan_layout) self.lightcurve = self.lightcurve_widget() self.scatter_box = Box(children=[self.lightcurve], layout=eighty_span_layout) self.image_box = Box(children=[], layout=twenty_span_layout) #self.fold_radiobutton() self.plot_box = w.HBox([self.scatter_box, self.image_box]) #position widgets using widget size self.widget = w.VBox([self.title_box, self.filter_box, self.plot_box], layout=w.Layout(height='350px'))
def __init__(self, obj=None): """ TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer sage: S = StandardTableaux(15) sage: t = S.random_element() sage: widget = SageExplorer(t) """ super(SageExplorer, self).__init__() self.title = Title() self.propsbox = VBox( ) # Will be a VBox full of HBoxes, one for each property self.titlebox = VBox() self.titlebox.add_class('titlebox') self.titlebox.children = [self.title, self.propsbox] self.visualbox = Box() self.visualtext = Textarea('', rows=8) self.visualwidget = None self.visualbox.add_class('visualbox') self.visualbox.children = [self.visualtext] self.top = HBox([self.titlebox, self.visualbox], layout=justified_h_layout) self.menus = Accordion(selected_index=None) self.menusbox = VBox([Title("Menus", 2), self.menus]) self.inputs = HBox() self.gobutton = Button( description='Run!', tooltip='Run the function or method, with specified arguments') self.output = HTML() self.worktab = VBox((self.inputs, self.gobutton, self.output)) self.doc = HTML() self.doctab = HTML() # For the method docstring self.tabs = Tab( (self.worktab, self.doctab)) # Will be used when a method is selected self.tabs.add_class('tabs') self.tabs.set_title(0, 'Call') self.tabs.set_title(1, 'Help') self.main = Box((self.doc, self.tabs)) self.tabs.add_class('invisible') # Hide tabs at first display self.bottom = HBox((self.menusbox, self.main), layout=main_h_layout) self.menusbox.add_class('lightborder') self.main.add_class('lightborder') self.titlebox.add_class('lightborder') self.children = (self.top, self.bottom) self.history = [] self.set_value(obj)
def _init_ui(self) -> VBox: "Initialize the widget UI and return the UI." self._search_input = Text(placeholder="What images to search for?") self._count_input = BoundedIntText(placeholder="How many pics?", value=10, min=1, max=5000, step=1, layout=Layout(width='60px')) self._size_input = Dropdown(options=_img_sizes.keys(), value='>400*300', layout=Layout(width='120px')) self._download_button = Button(description="Search & Download", icon="download", layout=Layout(width='200px')) self._download_button.on_click(self.on_download_button_click) self._output = Output() self._controls_pane = HBox([ self._search_input, self._count_input, self._size_input, self._download_button ], layout=Layout(width='auto', height='40px')) self._heading = "" self._download_complete_heading = "<h3>Download complete. Here are a few images</h3>" self._preview_header = widgets.HTML(self._heading, layout=Layout(height='60px')) self._img_pane = Box(layout=Layout(display='inline')) return VBox( [self._controls_pane, self._preview_header, self._img_pane])
def _make_delay_tab(box_factory, selected_index=0): """ Parameters ---------- box_factory : list of (func, tab_name) Example of box_factory: [(_make_gen_box, 'General'), (_make_repr_box, 'Representation')] """ tab = Tab([Box() for box, _ in box_factory]) [tab.set_title(i, title) for i, (_, title) in enumerate(box_factory)] # trick if not tab.children[selected_index].children: tab.selected_index = 1 def on_update_selected_index(change): index = change['new'] if not tab.children[index].children: # make widget on demand tab.children[index].children = [ box_factory[index][0](), ] tab.observe(on_update_selected_index, names='selected_index') # trigger tab.selected_index = selected_index return tab
def nested_dict_browser( nested_dict: Dict[Any, Any], nested_keys: Sequence[str] = () ) -> Box: """Returns a widget to interactive browse a nested dictionary.""" box = Box([]) _update_nested_dict_browser(nested_keys, nested_dict, box)(None) return box
def _make_auth(self) -> VBox: self.authtype_box = RadioButtons( options=[ "No Authorization", "OAuth2", "QueryParam", "Bearer", "Header" ], layout={"width": "max-content"}, # If the items' names are long description="", style={"description_width": "initial"}, disabled=False, ) auth_label = HTML( value= ("""<h3 style="background-color:#E8E8E8; background-size: 100px; ">""" """<span style="color: #ff0000">3.</span> Authorization """ """<span style="font-size: 14px">""" """<i>(some APIs require authorization)</i> </span></span></h3>""" ), layout=self.label_layout, ) self.authparams_box = Textarea( placeholder= ("Please separate authtication key and corresponding value by ':' ; " "while each key-value pair needs to be separated by ',' " "(e.g. name:abcdefg, date:2019-12-12)"), layout=self.textarea_layout, ) carousel_3 = Box(children=[self.authparams_box], layout=self.box_layout) auth_box = VBox([auth_label, self.authtype_box, carousel_3]) return auth_box
def build_stats_widget(self): from ipywidgets import FloatProgress, HBox, VBox, HTML, Layout, Button, Box loss_text = HTML('Loss', width='140px') self.loss_bar = FloatProgress(min=0.0, max=1.0, description='', height='10px') loss_widget = HBox([loss_text, self.loss_bar], width='100%') acc_text = HTML('Accuracy', width='140px') self.acc_bar = FloatProgress(min=0, max=1.0, description='', height='10px') acc_widget = HBox([acc_text, self.acc_bar], width='100%') box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', justify_content='space-around', border='1px solid #48A7F2', width='100%') return Box(children=[acc_widget, loss_widget], layout=box_layout, box_style='info')
def _make_pag(self) -> VBox: self.pagtype_box = RadioButtons( options=["No Pagination", "offset", "seek", "page", "token"], layout={"width": "max-content"}, # If the items' names are long description="", style={"description_width": "initial"}, disabled=False, ) pag_label = HTML( value= ("""<h3 style="background-color:#E8E8E8; background-size: 100px; "> <span style="color: #ff0000">4.</span> Pagination</h3>"""), layout=self.label_layout, ) self.pagparams_box = Textarea( placeholder= ("Please separate pagination key and corresponding value by ':' ;" " while each key-value pair needs to be separated by ',' " "(e.g. name:abcdefg, date:2019-12-12)"), layout=self.textarea_layout, ) carousel_4 = Box(children=[self.pagparams_box], layout=self.box_layout) pag_box = VBox([pag_label, self.pagtype_box, carousel_4]) return pag_box
def VBox(*pargs, **kwargs): """Displays multiple widgets vertically using the flexible box model.""" box = Box(*pargs, **kwargs) box.layout.display = 'flex' box.layout.flex_flow = 'column' box.layout.align_items = 'stretch' return box
def paramSlider(self): for k, v in self.model.p.items(): crude = ipyw.IntSlider(min=-5, max=10, description=k, value=floor(log10(v)), continuous_update=False) fine = ipyw.FloatSlider(min=10**floor(log10(v)), max=10**(floor(log10(v)) + 1), description=k, value=v, continuous_update=False) #txt = ipyw.FloatText(value=v, description=k, layout=Layout(width='80px')) #ipyw.jslink((fine, 'value'), (txt, 'value')) crude.observe(self._updateRange, names='value') fine.observe(self._paramUpdate, names='value') p = [crude, fine] #, txt] self.param.update({k: p}) box_layout = Layout(display='flex-start', flex_flow='row', align_items='flex-start', align_content='flex-start', width='100%') for k, v in self.param.items(): box = Box(children=v, layout=box_layout) display(box, layout=Layout(align_items='flex-start'))
def __init__(self, update_interval=0.5): self.update_interval = update_interval self.num_cpus = psutil.cpu_count() self.perf_thread = threading.Thread(target=self.update_data, daemon=True) self.keep_running = True self.perf_thread.start() self.bars = [] for i in range(self.num_cpus): widget = widgets.IntProgress( value=0, min=0, max=100, step=1, description='CPU #{}'.format(i), bar_style= 'success', # 'success', 'info', 'warning', 'danger' or '' orientation='vertical', readout=True, readout_format='d') self.bars.append(widget) items_layout = Layout(width='auto') box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', border='solid', width='auto') box = Box(children=self.bars, layout=box_layout) display(box)
class UIProgress(object): """多进程socket通信下的进度显示类""" def __init__(self, a_pid): """通过进程pid初始化ui组件""" self.progress_widget = FloatProgress(value=0, min=0, max=100) self.text_widget = Text('pid={} begin work'.format(a_pid)) # 通过box容器都放到一个里面 self.progress_box = Box([self.text_widget, self.progress_widget]) display(self.progress_box) def update(self, p_progress, p_progress_text): """进度条更新以及对应文字更新""" self.progress_widget.value = p_progress self.text_widget.value = p_progress_text def close(self): """关闭ui显示""" self.progress_box.close()
def _interactive(interact_f, kwargs_widgets): # this is a modified version of interactive() in ipywidget.widgets.interaction container = Box(_dom_classes=['widget-interact']) container.children = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] def call_f(name=None, old=None, new=None): kwargs = dict((widget._kwarg, widget.value) for widget in kwargs_widgets) try: interact_f(**kwargs) except Exception as e: container.log.warn("Exception in interact callback: %s", e, exc_info=True) for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def init_ui_progress(self): """初始化ui进度条""" if not self.show_progress: return if not ABuEnv.g_is_ipython or self._total < 2: return if ABuEnv.g_main_pid == os.getpid(): # 如果是在主进程下显示那就直接来 self.progress_widget = FloatProgress(value=0, min=0, max=100) self.text_widget = Text('pid={} begin work'.format(os.getpid())) self.progress_box = Box([self.text_widget, self.progress_widget]) display(self.progress_box) else: if g_show_ui_progress and g_socket_fn is not None: # 子进程下通过socket通信将pid给到主进程,主进程创建ui进度条 ABuOsUtil.socket_send_msg(g_socket_fn, '{}|init'.format(os.getpid()))
class AbuMulPidProgress(object): """多进程进度显示控制类""" def __init__(self, total, label, show_progress=True): """ 外部使用eg: with AbuMulPidProgress(len(self.choice_symbols), 'pick stocks complete') as progress: for epoch, target_symbol in enumerate(self.choice_symbols): progress.show(epoch + 1) :param total: 总任务数量 :param label: 进度显示label """ self._total = total self._label = label self.epoch = 0 self.display_step = 1 self.progress_widget = None self.text_widget = None self.progress_box = None self.show_progress = show_progress # 不管ui进度条有什么问题,也不能影响任务工作的进度执行,反正有文字进度会始终显示 @catch_error(log=False) def init_ui_progress(self): """初始化ui进度条""" if not self.show_progress: return if not ABuEnv.g_is_ipython or self._total < 2: return if ABuEnv.g_main_pid == os.getpid(): # 如果是在主进程下显示那就直接来 self.progress_widget = FloatProgress(value=0, min=0, max=100) self.text_widget = Text('pid={} begin work'.format(os.getpid())) self.progress_box = Box([self.text_widget, self.progress_widget]) display(self.progress_box) else: if g_show_ui_progress and g_socket_fn is not None: # 子进程下通过socket通信将pid给到主进程,主进程创建ui进度条 ABuOsUtil.socket_send_msg(g_socket_fn, '{}|init'.format(os.getpid())) # 不管ui进度条有什么问题,也不能影响任务工作的进度执行,反正有文字进度会始终显示 @catch_error(log=False) def update_ui_progress(self, ps, ps_text): """更新文字进度条""" if not self.show_progress: return if not ABuEnv.g_is_ipython or self._total < 2: return if ABuEnv.g_main_pid == os.getpid(): # 如果是在主进程下显示那就直接来 if self.progress_widget is not None: self.progress_widget.value = ps if self.text_widget is not None: self.text_widget.value = ps_text else: if g_show_ui_progress and g_socket_fn is not None: # 子进程下通过socket通信将pid给到主进程,主进程通过pid查找对应的进度条对象后更新进度 ABuOsUtil.socket_send_msg(g_socket_fn, '{}|{}|{}'.format(os.getpid(), ps, ps_text)) # 不管ui进度条有什么问题,也不能影响任务工作的进度执行,反正有文字进度会始终显示 @catch_error(log=False) def close_ui_progress(self): """关闭ui进度条显示""" if not self.show_progress: return if not ABuEnv.g_is_ipython or self._total < 2: return if ABuEnv.g_main_pid == os.getpid(): # 如果是在主进程下显示那就直接来 if self.progress_box is not None: self.progress_box.close() else: if g_show_ui_progress and g_socket_fn is not None: # 子进程下通过socket通信将pid给到主进程,主进程通过pid查找对应的进度条对象后关闭对象,且弹出 ABuOsUtil.socket_send_msg(g_socket_fn, '{}|close'.format(os.getpid())) def __enter__(self): """ 以上下文管理器类方式实现__enter__,针对self._total分配self.display_step """ if self.show_progress: self.display_step = 1 if self._total >= 5000: self.display_step = 50 elif self._total >= 3000: self.display_step = 30 elif self._total >= 2000: self.display_step = 20 elif self._total > 1000: self.display_step = 10 elif self._total >= 600: self.display_step = 6 elif self._total >= 300: self.display_step = 3 elif self._total >= 100: self.display_step = 2 elif self._total >= 20: self.display_step = 2 self.epoch = 0 self.init_ui_progress() return self def show(self, epoch=None, clear=True): """ 进行进度控制显示主方法 :param epoch: 默认None, 即使用类内部计算的迭代次数进行进度显示 :param clear: 默认True, 子进程显示新的进度前,先do_clear_output所有输出 :return: """ if not self.show_progress: return self.epoch = epoch if epoch is not None else self.epoch + 1 if self.epoch % self.display_step == 0: ps = round(self.epoch / self._total * 100, 2) ps = 100 if ps > 100 else ps ps_text = "pid:{} {}:{}%".format(os.getpid(), self._label, ps) if not ABuEnv.g_is_ipython or self._total < 2: if clear: do_clear_output() # clear_std_output() print(ps_text) self.update_ui_progress(ps, ps_text) def __exit__(self, exc_type, exc_val, exc_tb): """ 以上下文管理器类方式实现__exit__,针对在子进城中的输出显示进度进行do_clear_output扫尾工作 """ if not self.show_progress: return clear = False if clear: # clear在mac上应该打开, 由于windows某些版本浏览器wait=True会有阻塞情况,如果wait=False, 有clear之后的风险, do_clear_output(wait=True) # wait 需要同步否则会延迟clear else: # print("pid:{} done!".format(os.getpid())) pass self.close_ui_progress()
def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True, clear_after_finish=False): self.verbose = verbose if self.verbose: self.model = model self.iteration = current_iteration self.p_iter = self.iteration self.maxiters = maxiters self.len_maxiters = len(str(maxiters)) self.opt_name = opt.opt_name self.model.add_observer(self, self.print_status) self.status = 'running' self.clear = clear_after_finish self.update() try: from IPython.display import display from ipywidgets import IntProgress, HTML, Box, VBox, HBox, FlexBox self.text = HTML(width='100%') self.progress = IntProgress(min=0, max=maxiters) #self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters)) self.model_show = HTML() self.ipython_notebook = ipython_notebook except: # Not in Ipython notebook self.ipython_notebook = False if self.ipython_notebook: left_col = VBox(children=[self.progress, self.text], padding=2, width='40%') right_col = Box(children=[self.model_show], padding=2, width='60%') self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal') display(self.hor_align) try: self.text.set_css('width', '100%') left_col.set_css({ 'padding': '2px', 'width': "100%", }) right_col.set_css({ 'padding': '2px', }) self.hor_align.set_css({ 'width': "100%", }) self.hor_align.remove_class('vbox') self.hor_align.add_class('hbox') left_col.add_class("box-flex1") right_col.add_class('box-flex0') except: pass #self.text.add_class('box-flex2') #self.progress.add_class('box-flex1') else: self.exps = exponents(self.fnow, self.current_gradient) print('Running {} Code:'.format(self.opt_name)) print(' {3:7s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "runtime", mi=self.len_maxiters))