def html_card(todo, do_action, cancel_action=None): """ Gera um card html usando o todo como referência. Args: - todo: {'name': 'name', 'id': 'id', 'description': 'description'} - do_action: {'text': 'Fazer!', 'action': bind} - cancel_action: {'text': 'Cancelar!', 'action': bind} Note: *_acions = Dict[str, Union[str, Callable]] """ div = html.DIV(Class='terminal-card') div <= html.HEADER(f'{todo["name"]} #{todo["id"]}') div <= html.DIV(f'{todo["description"]}') buts = html.DIV(Class='buttons') do_button = html.BUTTON(do_action['text'], Class='btn btn-primary btn-ghost do') buts <= do_button do_button.bind('click', do_action['action']) if cancel_action: cancel_button = html.BUTTON(cancel_action['text'], Class='btn btn-error btn-ghost cancel') cancel_button.bind('click', cancel_action['action']) buts <= cancel_button div <= buts return div
def delete(evt, elt): dialog_window.style.display = "block" dialog.clear() dialog_title = dialog_window.select_one(".dialog_title") dialog_title.clear() dialog_title <= html.SPAN("Remove script") dialog <= f"Do you really want to delete script {current} ?" dialog <= html.P() dialog <= html.BUTTON("Ok") + html.BUTTON("Cancel") @dialog.select("button")[0].bind("click") def confirm_delete(evt): db = request.result tx = db.transaction("scripts", "readwrite") store = tx.objectStore("scripts") cursor = store.delete(current) dialog_window.style.display = "none" # when record is added, show message def ok(evt): open_scripts.remove(current) editor.text = "" print_line_nums() draw_file_browser() cursor.bind('success', ok) @dialog.select("button")[1].bind("click") def cancel_delete(evt): dialog_window.style.display = "none"
def __init__(self, title="", style={}, top=0, left=0, ok_cancel=False): for key in style: for item in styles: styles[item][key] = style[key] html.DIV.__init__(self, style=styles["dialog"]) self.left = left self.top = top self._title = html.DIV(html.SPAN(title), style=styles["title"]) self <= self._title btn = html.SPAN("×", style=styles["close"]) self._title <= btn btn.bind("click", self.close) self.panel = html.DIV(style=styles["panel"]) self <= self.panel if ok_cancel: ok_cancel_zone = html.DIV(style={"text-align": "center"}) self.ok_button = html.BUTTON("Ok") self.cancel_button = html.BUTTON("Cancel") self.cancel_button.bind("click", self.close) ok_cancel_zone <= self.ok_button + self.cancel_button self <= ok_cancel_zone document <= self self._title.bind("mousedown", self.mousedown) document.bind("mousemove", self.mousemove) self._title.bind("mouseup", self.mouseup) self.bind("leave", self.mouseup) self.is_moving = False
def add_ok_cancel(self, ok=None, cancel=None): """Add Ok and Cancel buttons ok is the callback function if user clicks "Ok". It is a function that takes the Dialog instance as single argument. Same for cancel """ self.ok = ok self.cancel = cancel self.footer.style.height = '1.5em' left = html.DIV(style=dict(width="50%"), Class='ui-footer-ok') right = html.DIV(style=dict(width="50%"), Class='ui-footer-cancel') self.footer <= left + right ok_button = html.BUTTON('Ok', Class="ui-footer-button") if ok is None: ok_button.bind('click', lambda ev: self.close()) else: ok_button.bind('click', lambda ev: self.callback(ok)) left <= ok_button cancel_button = html.BUTTON('Annuler', Class="ui-footer-button") if cancel is None: cancel_button.bind('click', lambda ev: self.close()) else: cancel_button.bind('click', lambda ev: self.callback(cancel)) right <= cancel_button
def __init__(self, title="", *, top=None, left=None, ok_cancel=False, default_css=True): if default_css: for stylesheet in document.styleSheets: if stylesheet.ownerNode.id == "brython-dialog": break else: document <= html.STYLE(style_sheet, id="brython-dialog") html.DIV.__init__(self, style=dict(position="absolute"), Class="brython-dialog-main") #set_style(self, "dialog-main") self.title_bar = html.DIV(html.SPAN(title), Class="brython-dialog-title") self <= self.title_bar self.close_button = html.SPAN("×", Class="brython-dialog-close") self.title_bar <= self.close_button self.close_button.bind("click", self.close) self.panel = html.DIV(Class="brython-dialog-panel") self <= self.panel if ok_cancel: ok_cancel_zone = html.DIV(style={"text-align": "center"}) ok, cancel = "Ok", "Cancel" if isinstance(ok_cancel, (list, tuple)): if not len(ok_cancel) == 2: raise ValueError( f"ok_cancel expects 2 elements, got {len(ok_cancel)}") ok, cancel = ok_cancel self.ok_button = html.BUTTON(ok, Class="brython-dialog-button") self.cancel_button = html.BUTTON(cancel, Class="brython-dialog-button") self.cancel_button.bind("click", self.close) ok_cancel_zone <= self.ok_button + self.cancel_button self <= ok_cancel_zone document <= self cstyle = window.getComputedStyle(self) # Center horizontally and vertically if left is None: width = round(float(cstyle.width[:-2]) + 0.5) left = int((window.innerWidth - width) / 2) self.left = left self.style.left = f'{left}px' if top is None: height = round(float(cstyle.height[:-2]) + 0.5) top = int((window.innerHeight - height) / 2) # top is relative to document scrollTop top += document.scrollingElement.scrollTop self.top = top self.style.top = f'{top}px' self.title_bar.bind("mousedown", self.mousedown) self.title_bar.bind("touchstart", self.mousedown) self.title_bar.bind("mouseup", self.mouseup) self.title_bar.bind("touchend", self.mouseup) self.bind("leave", self.mouseup) self.is_moving = False
def get_ctp_all(ev): document['console'].clear() document['console'] <= "帐户信息获取成功..." cache['ctp'] = ev['data'] if 'zmq' not in cache: for one in cache['ctp'].values(): if 'zmqserver' in one and len(one['zmqserver'])>0: cache['zmq'] = one['zmqserver'] break for acc,one in ev['data'].items(): if one['usezmq']=='0': _text = "账号:%(userid)s 行情服务器:%(mdfront)s 交易服务器:%(tdfront)s 柜台ID:%(brokerid)s 订阅合约:%(instrument)s 自动交易服务器:%(zmqserver)s ○不使用"%one else: _text = "账号:%(userid)s 行情服务器:%(mdfront)s 交易服务器:%(tdfront)s 柜台ID:%(brokerid)s 订阅合约:%(instrument)s 自动交易服务器:%(zmqserver)s ●使用"%one _btn1 = html.BUTTON("更新",id=one['userid']) _btn1.bind('click',updatectp) _btn2 = html.BUTTON("删除",id=one['userid']) _btn2.bind('click',delctp) _div = html.DIV(id=one['userid']) _div <= _btn1+_btn2+_text+html.HR() document['ctp'] <= _div newer = html.DIV() nmd = html.INPUT(id="mdfront") nmd.value="" newer<= nmd+"行情前置服务器(mdfront)"+html.BR() nmd = html.INPUT(id="tdfront") nmd.value="" newer<= nmd+"交易前置服务器(tdfront)"+html.BR() nmd = html.INPUT(id="brokerid") nmd.value="" newer<= nmd+"柜台ID"+html.BR() nmd = html.INPUT(id="userid") nmd.value="" newer<= nmd+"账号"+html.BR() nmd = html.INPUT(id="password") nmd.value="" newer<= nmd+"密码"+html.BR() nmd = html.INPUT(id="instrument") nmd.value="" newer<= nmd+"订阅合约(多个合约中间用+分隔,使用主力合约加=,如IF=)"+html.BR() nmd = html.INPUT(id="usezmq") nmd.value="0" newer<= nmd+"使用自动交易信号(0:不使用 1:使用)"+html.BR() nmd = html.INPUT(id="zmqserver") nmd.value=cache.get('zmqserver','') newer<= nmd+"自动交易服务器地址"+html.BR() nbtn = html.BUTTON("添加",id="new") nbtn.bind('click',addnew) newer<= nbtn document['ctp'] <= newer
def __init__(self, title, prompt, action, _id=None): Dialog.__init__(self, _id) self.set_title(title) self.action = action d_prompt = html.DIV(prompt, Class="ui-widget", style=dict(float="left", paddingRight="10px")) self.entry = html.INPUT() body = html.DIV(d_prompt + self.entry, style={'padding': '15px'}) b_ok = html.BUTTON("Ok") b_ok.bind('click', self.ok) b_cancel = html.BUTTON("Cancel") b_cancel.bind('click', self.cancel) body += html.DIV(b_ok + b_cancel, style={'padding': '15px'}) self._div_dialog <= body
def get_type(e): global tree global treeType if e.target.value == 'avl': tree = avl.Tree() del document['nav'] n = create_nav() document['container'] <= n document['container'] <= html.DIV(id='output') document['container'] <= html.DIV(id='tree') document['h1'].text = 'AVL tree' treeType = 'AVL' elif e.target.value == 'bst': tree = bst.Tree() del document['nav'] n = create_nav() n <= html.BUTTON('DSW', id='dsw-btn', Class='btn') document['container'] <= n document['container'] <= html.DIV(id='output') document['container'] <= html.DIV(id='tree') document['dsw-btn'].bind('click', dsw_event) document['h1'].text = 'BST tree' treeType = 'BST' else: alert("Something went wrong. Please refresh this page.") document['insert-btn'].bind('click', insert_event) document['delete-btn'].bind('click', delete_event) document['min-btn'].bind('click', min_event) document['max-btn'].bind('click', max_event) document['inorder-btn'].bind('click', inorder_event) document['preorder-btn'].bind('click', preorder_event) document['postorder-btn'].bind('click', postorder_event)
def __init__(self, title, message, *, top=None, left=None, default_css=True, remove_after=None, ok=False): """If remove_after is set, number of seconds after which the dialog is removed.""" Dialog.__init__(self, title, top=top, left=left, default_css=default_css) self.panel <= html.DIV(message) if ok: ok = ok if isinstance(ok, str) else "Ok" self.ok_button = html.BUTTON(ok, Class="brython-dialog-button") self.panel <= html.P() self.panel <= html.DIV(self.ok_button, style={"text-align": "center"}) self.ok_button.bind("click", lambda ev: self.remove()) if remove_after: if not isinstance(remove_after, (int, float)): raise TypeError("remove_after should be a number, not " + str(remove_after.__class__.__name__)) window.setTimeout(self.close, remove_after * 1000)
def create_start_game_panel(self): start_game_panel = html.DIV() button = html.BUTTON("Commencer !", Id="start_game_button") def button_action(event): self.start_observable.value = True button.bind("click", button_action) def get_scene_callback(button): def callback(observable): if observable.value: if "disabled" in button.attrs: del button.attrs["disabled"] else: button.attrs["disabled"] = None return callback self.scene_observable.subscribe(get_scene_callback(button)) start_game_panel <= button return start_game_panel
def create_character_selection_panel(self): character_selection_panel = html.DIV(Id="char_sel") for character in characters: button = html.BUTTON(character, Class="char_button") def get_button_action(character): def button_action(event): self.character_observable.value = character self.scene_observable.value = None # print(self.character_observable.value) return button_action button.bind("click", get_button_action(character)) def get_character_callback(button, character): def callback(observable): if character == observable.value: if "char_button_selected" not in button.classList: button.classList.add("char_button_selected") else: if "char_button_selected" in button.classList: button.classList.remove("char_button_selected") return callback self.character_observable.subscribe( get_character_callback(button, character)) character_selection_panel <= html.DIV(button) return character_selection_panel
def tiler(wd, tile): nomes = NOME[:] shuffle(nomes) tile <= html.BUTTON( wd.upper(), Class="tile is-child is-dark is-outlined is-inverted") [tile <= button(hora, nome) for hora, nome in zip(ihour, nomes)]
def __init__(self, title, prompt, action_if_yes, action_if_no, _id=None): Dialog.__init__(self, _id) self.set_title(title) self.action_if_yes = action_if_yes self.action_if_no = action_if_no d_prompt = html.DIV(prompt, Class="ui-widget", style=dict(float="left",paddingRight="10px")) body = html.DIV(d_prompt, style={'padding':'15px'}) b_ok = html.BUTTON("Yes") b_ok.bind('click', self.yes) b_cancel = html.BUTTON("No") b_cancel.bind('click', self.no) body += html.DIV(b_ok+b_cancel, style={'padding':'15px'}) self._div_dialog <= body
def setup(self, container): def _make_handler(building): def _handle(e): self.game.buildings[building].on_buy() return _handle self.divs = {} self.buybuttons = {} for key, value in self.game.buildings.items(): self.divs[key] = [] self.buybuttons[key] = [] for ele in container.get(selector=".building-holder-holder"): if value.in_world(ele.parent.parent.id.split("_")[1]): parent = html.DIV( html.SPAN(value.name), Class="building-holder building-holder_" + key) div = html.SPAN("Loading...", Class="building-count") parent <= div desc = html.DIV(value.desc + "\n" + value.buy_desc, Class="building-desc") button = html.BUTTON("Buy!", disabled=True) self.buybuttons[key].append(button) button.bind("click", _make_handler(key)) desc <= html.BR() desc <= button parent <= desc self.divs[key].append(div) ele <= parent
def share_code(ev): src = editor.getValue() if len(src) > 2048: d = dialog.InfoDialog("Copy url", f"code length is {len(src)}, must be < 2048", style={"zIndex": 10}, ok=True) else: href = window.location.href.rsplit("?", 1)[0] query = doc.query query["code"] = src url = f"{href}{query}" url = url.replace("(", "%28").replace(")", "%29") d = dialog.Dialog("Copy url") area = html.TEXTAREA(rows=0, cols=0) d.panel <= area area.value = url # copy to clipboard area.focus() area.select() doc.execCommand("copy") d.remove() d = dialog.Dialog("Copy url") d.panel <= html.DIV("url copied in the clipboard<br>Send it to share the code") buttons = html.DIV() ok = html.BUTTON("Ok") buttons <= html.DIV(ok, style={"text-align": "center"}) d.panel <= html.BR() + buttons @bind(ok, "click") def click(evt): d.remove()
def setup_top_panel(): top_panel = document["about_panel"] table = html.TABLE() tableBody = html.TBODY() row = html.TR() # html style properties: # http://www.w3schools.com/jsref/dom_obj_style.asp global buttonHome buttonHome = html.BUTTON('Data Analysis') button_style_default(buttonHome) buttonHome.bind('click', click_home) buttonHome.bind('mouseout', mouse_out_home) buttonHome.bind('mouseover', mouse_over_home) global buttonAbout buttonAbout = html.BUTTON('About') button_style_default(buttonAbout) buttonAbout.bind('click', click_about) buttonAbout.bind('mouseout', mouse_out_about) buttonAbout.bind('mouseover', mouse_over_about) global buttonGuide buttonGuide = html.BUTTON('User Guide') button_style_default(buttonGuide) buttonGuide.bind('click', click_guide) buttonGuide.bind('mouseout', mouse_out_guide) buttonGuide.bind('mouseover', mouse_over_guide) global buttonContact buttonContact = html.BUTTON('Contact') button_style_default(buttonContact) buttonContact.bind('click', click_contact) buttonContact.bind('mouseout', mouse_out_contact) buttonContact.bind('mouseover', mouse_over_contact) row <= html.IMG(src="genexpresso.png", width="100") row <= buttonHome row <= buttonGuide row <= buttonAbout row <= buttonContact tableBody <= row table <= tableBody top_panel <= table
def create_error_message(): gui = document["main-display"] errorMessage = html.DIV(id="error-message", Class="alert alert-warning alert-dismissible") closeButton = html.BUTTON(Class="close", data_dismiss="alert") errorMessage <= closeButton errorMessage.text = "Case vide ou votre coup affame l'adversaire, choisissez une autre case." gui <= errorMessage
def __init__(self, title="", style={}, top=None, left=None, ok_cancel=False): for key in style: for item in styles: styles[item][key] = style[key] html.DIV.__init__(self, style=styles["dialog"]) self._title = html.DIV(html.SPAN(title), style=styles["title"]) self <= self._title btn = html.SPAN("×", style=styles["close"]) self._title <= btn btn.bind("click", self.close) self.panel = html.DIV(style=styles["panel"]) self <= self.panel if ok_cancel: ok_cancel_zone = html.DIV(style={"text-align": "center"}) self.ok_button = html.BUTTON("Ok") self.cancel_button = html.BUTTON("Cancel") self.cancel_button.bind("click", self.close) ok_cancel_zone <= self.ok_button + self.cancel_button self <= ok_cancel_zone document <= self cstyle = window.getComputedStyle(self) # Center horizontally and vertically if left is None: width = round(float(cstyle.width[:-2]) + 0.5) left = int((window.innerWidth - width) / 2) self.left = left self.style.left = f'{left}px' if top is None: height = round(float(cstyle.height[:-2]) + 0.5) top = int((window.innerHeight - height) / 2) self.top = top self.style.top = f'{top}px' self._title.bind("mousedown", self.mousedown) document.bind("mousemove", self.mousemove) self._title.bind("mouseup", self.mouseup) self.bind("leave", self.mouseup) self.is_moving = False
def create_end_message(rslt): # div pour montrer qui a gagné gui = document["main-display"] endMessage = html.DIV(id="end-message", Class="alert alert-danger alert-dismissible") if isinstance(rslt, Human): endMessage.text = "Vous avez gagné ! " + "IA : " + str(t.game.player1.loft) + " Vous: " + str(t.game.player0.loft) elif isinstance(rslt, AI): endMessage.text = "L'ordinateur gagne ! " + "IA : " + str(t.game.player1.loft) + " Vous: " + str(t.game.player0.loft) closeBtn = html.BUTTON(Class="close", data_dismiss="alert", aria_label="close") span = html.SPAN(aria_hidden="true") x = html.I(Class="fa fa-times") newGameBtn = html.BUTTON(Class="btn ml-3 mr-auto", id="newgame", onclick="showSettings()", data_dismiss="alert") newGameBtn.text = "Nouvelle partie" span <= x closeBtn <= span endMessage <= closeBtn endMessage <= newGameBtn gui <= endMessage
def __init__(self, title, prompt, options, action, _id=None): Dialog.__init__(self, _id) self.set_title(title) self.options = options self.action = action d_prompt = html.DIV(prompt, Class="ui-widget", style=dict(float="left", paddingRight="10px")) self.select = html.SELECT() for option in options: self.select <= html.OPTION(option) body = html.DIV(d_prompt + self.select, style={'padding': '15px'}) b_ok = html.BUTTON("Ok") b_ok.bind('click', self.ok) b_cancel = html.BUTTON("Cancel") b_cancel.bind('click', self.cancel) body += html.DIV(b_ok + b_cancel, style={'padding': '15px'}) self._div_dialog <= body
def button(nome): nick = nome[0] _button = html.BUTTON( f"{nick}", Class="tile is-child is-dark is-outlined is-inverted", Id=f"b_{nome}") _button.style.backgroundColor = NOMECOR[nome] _button.onclick = self.seleciona_pessoa return _button
def __init__(self): self.shadow = self.attachShadow({'mode': 'open'}) self.locals = {} self.globals = {} ta = html.TEXTAREA() self.shadow <= ta bind(ta, 'change')(self.onChange(ta)) button = html.BUTTON('Evaluate') self.shadow <= button bind(button, 'click')(self.eval)
def __init__(self, title): html.DIV.__init__( self, Class= "ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable", style={ 'position': 'absolute', 'height': 'auto', 'width': '300px', 'top': '98px', 'left': '140px', 'display': 'block', 'zIndex': 0 }) widget.DraggableWidget.__init__(self, self, 'dialog', id) titlebar = html.DIV(Id="titlebar", Class="ui-dialog-titlebar") self._title = html.DIV(title, Class="ui-dialog-title") titlebar <= self._title title_button = html.BUTTON('X', Title="close", Class="ui-close-button") def dialog_close(e): #del document[self._div_shell.id] document.remove(self) title_button.bind('click', dialog_close) titlebar <= title_button self.body = html.DIV(Class="ui-dialog-body", style={ 'width': 'auto', 'min-height': '105px', 'max-height': 'none', 'height': 'auto' }) self <= titlebar self <= self.body for _i in ['n', 'e', 's', 'w', 'se', 'sw', 'ne', 'nw']: if _i == 'se': _class = "ui-resizable-handle ui-resizable-%s ui-icon ui-icon-gripsmall-diagonal-%s" % ( _i, _i) else: _class = "ui-resizable-handle ui-resizable-%s" % _i self <= html.DIV(Class=_class, style={'z-index': '90'}) self.footer = html.DIV(Class='ui-footer') self <= self.footer
def create_error_message(): gui = document["main-display"] errorMessage = html.DIV(id="error-message", Class="alert alert-warning alert-dismissible") errorMessage.text = "Case vide ou votre coup affame l'adversaire, choisissez une autre case." closeBtn = html.BUTTON(Class="close", data_dismiss="alert", aria_label="close") span = html.SPAN(aria_hidden="true") x = html.I(Class="fa fa-times") span <= x closeBtn <= span errorMessage <= closeBtn gui <= errorMessage
def create_question_panel(message, button_contents, callback): panel = html.DIV() panel <= message panel <= html.HR() for button_index, button_content in enumerate(button_contents): button = html.BUTTON(button_content) button.bind("click", get_question_button_action(panel, button_index, callback)) panel <= button return panel
def redo(event): card, primary, error = card_elements(event) iternal_div = card.select('div')[1] error = html.BUTTON('Cancelar', Class='btn btn-error btn-ghost cancel') error.bind('click', cancel_card) iternal_div <= error primary.text = 'Fazer' primary.unbind('click', redo) primary.bind('click', doing_card) document.select_one('#todo') <= card
def create_nav(): n = html.NAV(id='nav') n <= html.INPUT(id='insert') n <= html.BUTTON("Insert", id='insert-btn', Class='btn') n <= html.INPUT(id='delete') n <= html.BUTTON('Delete', id='delete-btn', Class='btn') n <= html.BUTTON('Min', id='min-btn', Class='btn') n <= html.BUTTON('Max', id='max-btn', Class='btn') n <= html.BUTTON('Inorder', id='inorder-btn', Class='btn') n <= html.BUTTON('Preorder', id='preorder-btn', Class='btn') n <= html.BUTTON('Postorder', id='postorder-btn', Class='btn') return n
def create_card(event): name, desc, checked, valid = read_form() if valid: todo = document.select_one('#todo') card = html.DIV(Class='terminal-card') card <= html.HEADER(name) card <= html.DIV(desc) buttons = html.DIV(Class='buttons') do = html.BUTTON('Fazer', Class='btn btn-primary btn-ghost do') cancel = html.BUTTON( 'Cancelar', Class='btn btn-error btn-ghost cancel' ) do.bind('click', doing_card) cancel.bind('click', cancel_card) buttons <= do buttons <= cancel card <= buttons timer.set_timeout( partial(create_card_timer, checked, todo, card), randint(1, 3000) )
def create_card(event): name, desc, checked, valid = read_form() if valid: todo = document.select_one('#todo') card = html.DIV(Class='terminal-card') card <= html.HEADER(name, Class='name') card <= html.DIV(desc, Class='description') buttons = html.DIV(Class='buttons') do = html.BUTTON('Fazer', Class='btn btn-primary btn-ghost do') cancel = html.BUTTON('Cancelar', Class='btn btn-error btn-ghost cancel') do.bind('click', doing_card) cancel.bind('click', cancel_card) buttons <= do buttons <= cancel card <= buttons if checked: todo.insertBefore(card, todo.firstChild) else: todo <= card
def create_scene_selection_panel(self): scene_selection_panel = html.DIV() for scene_id in scene_ids: scene_pretty_name = scene_pretty_names[scene_id] if scene_id in self.transcriptions: button = html.BUTTON(scene_pretty_name, Class="scene_button") def get_button_action(scene_id): def button_action(event): self.scene_observable.value = scene_id return button_action button.bind("click", get_button_action(scene_id)) def get_character_callback(button, scene_id): def callback(observable): if observable.value in self.transcriptions[scene_id][ "characters"]: if "disabled" in button.attrs: del button.attrs["disabled"] else: button.attrs["disabled"] = None return callback self.character_observable.subscribe( get_character_callback(button, scene_id)) def get_scene_callback(button, scene_id): def callback(observable): if scene_id == observable.value: if "scene_button_selected" not in button.classList: button.classList.add("scene_button_selected") else: if "scene_button_selected" in button.classList: button.classList.remove( "scene_button_selected") return callback self.scene_observable.subscribe( get_scene_callback(button, scene_id)) scene_selection_panel <= html.DIV(button) else: scene_selection_panel <= html.DIV( f"({scene_pretty_name} - manquant)", Class="missing_scene") return scene_selection_panel