def update(self, one_state_change=False): stfulComp = group( html.DIV(), [html.H2(self.state["tick"]), html.H2(self.state["tock"])]) def increment(): self.setState({ "tick": self.state["tick"] + 1, "tock": self.state["tock"] - 1 }) if not one_state_change: print("set time out") timer.set_timeout(increment, 1000) return stfulComp
def formatted(inputstring): output = [] stringlist = inputstring.split("\n\n") for s in stringlist: s = s.strip() if s[:3] == "## ": output.append(html.H2(s[3:])) else: while " http" in s: start = s.index(" http") end1 = s.find(" ", start+1) if end1 < 0: end1 = 1e12 end2 = s.find("\n", start+1) end = min(end1, end2) if end < 0: end = None link = s[start+1:end] s = s.replace(link, f" <a href='{link}'>{link}</a>") markup = [("`", "code"), ("**", "bold"), ("*", "italic")] for (code, className) in markup: while code in s: L = len(code) start = s.index(code) end = s.find(code, start+L) text = s[start:end+L] s = s.replace(text, f" <span class='{className}'>{text[L:-L]}</span>") s = s.replace("\n", "<br />\n") output.append(html.P(s)) return output
def load(sheet_name=None): global current_cell_info, menu_file if sheet_name is None: sheet_name = 'New document' panel = document['panel'] title = html.DIV(style=dict(width='auto')) title <= html.H2(sheet_name, id="sheet_name") panel <= title menu = ui.Menu() menu_file = menu.add('File') menu_file.add('New', None) menu_file.add('Open...', select_sheet) menu_file.add('Save as...', save_as) panel <= html.SPAN(menu) panel <= html.BR() cell_editor = html.INPUT(style=dict(width="200px"), Id="current") cell_editor.bind('click', enter_editor) cell_editor.bind('keydown', editor_keydown) cell_editor.bind('keyup', update_from_editor) panel <= cell_editor t = html.TABLE(Id="sheet_table") srow = -1 rows, cols = 20, 20 col_widths = [100 for i in range(rows)] line = html.TR() line <= html.TH() for i in range(cols): col_name = chr(65 + i) line <= html.TH(col_name, style={'min-width': '%spx' % col_widths[i]}) t <= line for i in range(rows * cols): row, column = divmod(i, cols) if row > srow: line = html.TR() line <= html.TH(row + 1) t <= line srow = row cell = html.TD('', id='c%s_%s' % (row, column), style=dict(padding='2px')) cell.bind('click', select) cell.bind('dblclick', entry) cell.info = {'entry': ''} line <= cell panel <= html.DIV(t, style=dict(float='left')) mark_selected(t.get(selector='TD')[0])
def render(self): app = group(html.DIV(), [ html.H1("HI THERE!"), html.H2("GOOD TO SEE YOU!"), html.P("Some things about me:"), html.UL([html.LI("I am"), html.LI("Does this work")]), html.H2("here is a nice little ticker"), TickerWithDescription().render(), TextField("here is a prop being passed").render() ]) return app # app = html.DIV(id="hithere") # # app <= html.H1("hi there", id="what") # thing = group(app, [WithGroups().render()]) # # thing = app # document["root"] <= thing # thid = document["hithere"] # thid.innerHTML = "hedy" # print(document.getElementById("hithere").innerHTML) # document["what"] <= html.H1("buy")
def __init__(self, tela=DOC_PYDIV): self.tela = tela self.optou = "" self.foi = None self.popup = html.DIV(Id="__popup__", Class="overlay") self.div = div = html.DIV(Class="popup") self.tit = html.H2() self.a = html.A("×", Class="close", href="#") self.go = html.A(Id="txt_button", Class="button", href="#__popup__") self.go.onclick = self._open self.a.onclick = self._close self.alt = html.DIV(Class="content") self.popup <= div self.popup.style = {"visibility": "hidden", "opacity": 0} self.inicia()
def _scorer_(cls): Elemento._scorer_ = lambda *_: None Elemento._score = scr = Elemento(SCORE) scr.score_ = html.H2("0") scr.elt <= scr.score_ scr.entra(INVENTARIO)
import math from browser import document, html # 設定 Python 執行結果的容器 container = document['python'] # Python 執行結果 <div id="python"></div> for c in "w3-container w3-half w3-margin-top".split(' '): # 設定基本版型 container.classList.add(c) # 小海龜繪圖模組 import turtle import myTurtle container <= html.DIV(html.H2('小海龜繪圖模組'), Class="w3-container w3-blue w3-margin-top") turtleDiv = html.DIV(Class="w3-border") turtle.set_defaults( turtle_canvas_wrapper=turtleDiv ) screen = turtle.Screen() # screen.setup(40, 40) turtle.tracer(1) # Turns off screen updates grid = turtle.Turtle() grid.penup() grid.width(1) grid.color('lightgrey') grid.speed(0) STEP = 30 OFFSET = 15 LENGTH = 300
self._playSounds = True def storePlaySoundsSetting(self): self._storage['playSounds'] = str(self._playSounds) # Use brython to create the canvas. real_canvas = html.CANVAS(width=CANVAS_WIDTH, height=CANVAS_HEIGHT, id='c') document <= real_canvas def windowChange(ev): print(window.innerWidth, window.innerHeight) window.bind('resize', windowChange) # Use the canvas in fabric canvas = fabric.Canvas.new('c', { 'width': CANVAS_WIDTH, 'height': CANVAS_HEIGHT, 'selectable': False, 'backgroundColor': 'darkgreen' }) app = App(document, canvas) document <= html.H2( html.A("Instructions", href="instructions.html", Class="right-edge")) document <= html.H5('Version: 1.1', Class="right-edge")
from browser import document from browser import html from browser.template import Template pageName = "Python 🐍" articleContent = open('news_file.txt').read() articleTitle = articleContent.split('\n', 1)[0] articleText = articleContent.split('\n', 1)[1] article = [ html.H1('{title}', Id='art_title'), html.P('{text}', Id='art_content') ] nav = html.DIV([html.H1('{title}', Id='title'), html.H2('☰', Id='navbtn')], Class="nav") cnt = html.DIV(html.DIV(article, Class="article"), Class="content") ftr = html.DIV( [html.P('Copyright {} 2018 - All rights reserved.'.format(pageName))], Class="footer") document <= [nav, cnt, ftr] Template(document['title']).render(title=pageName) Template(document['art_title']).render(title=articleTitle) Template(document['art_content']).render(text=articleText)
from browser import window, document, html import javascript # 設定 Python 執行結果的容器 container = document['python'] # Python 執行結果 <div id="python"></div> for c in "w3-margin-top w3-margin".split(' '): # 設定基本版型 container.classList.add(c) container.style = {"width": "800px"} container <= html.DIV(html.H2('Phaser 遊戲設計'), Class="w3-container w3-blue") phaserContainer = html.DIV(id="phaser") container <= phaserContainer Phaser = window.Phaser class Game(object): def __init__(self): self.game = Phaser.Game.new({ 'type': Phaser.AUTO, 'width': 800, 'height': 400, 'parent': 'phaser', 'physics': { 'default': 'arcade', 'arcade': { 'gravity': { 'y': 200 } } },
# Text input text_area = html.TEXTAREA() @text_area.bind("keydown") def print_keystrokes(event): print(text_area.value) main <= text_area # query selector all buttons = document.querySelectorAll('button') print(len(buttons)) # ajax main <= html.H2("BACON") bacon_div = html.DIV() main <= bacon_div def make_some_bacon(request): print(request.responseText) bacon_div.textContent = request.responseText req = ajax.ajax() req.open('GET', "https://baconipsum.com/api/?type=meat-and-filler", True) req.bind('complete', make_some_bacon) req.send()
# Create initial layout from browser import html from browser import document header = html.HEADER(Class="text-center") main = html.MAIN() footer = html.FOOTER() headingOne = html.H1('Python101') subtitleOne = html.SMALL('Learning how to use python') header.append(headingOne) header.append(subtitleOne) main.append(html.H2('Main Section', Class="text-center")) footer.append(html.H3('Footer Section', Class="text-center")) document.body.append(header) document.body.append(main) document.body.append(footer)