def test_reducer(self): def name_reducer(state, payload): return {**state, **{ "name": payload }} store = create_store({ "NAME_REDUCER" : name_reducer }, {"name": "Test"}) store.dispatch("NAME_REDUCER", "Test2") assert store.state == {"name": "Test2"}
def test_simple(self): observer = unittest.mock.MagicMock() store= create_store({}, {"name": "Test"}) assert store.state == {"name": "Test"} store.subscribe(observer.subscribe) assert len(observer.method_calls) == 1 store.dispatch("SET_VALUE", {"path": ["name"], "value": "Test2"}) assert len(observer.method_calls) == 2 assert store.state == {"name": "Test2"}
def test_dispose(self): store= create_store({}, {"name": "Test"}) mock = unittest.mock.MagicMock() observer = store.subscribe(mock) assert len(mock.method_calls) == 1 observer.dispose() store.dispatch("SET_VALUE", {"path": ["name"], "value": "Test2"}) assert len(mock.method_calls) == 1
def _create_view(self): self.demoStore = create_store( eval(self._get_state["store"]), ast.literal_eval(self._get_state["data"])) view_class = create_view(self._get_state["template"], self._get_state["view"]) self.demoView = app.add_view(view_class(store=self.demoStore)).widget if len(self.demoView.logger.handlers) == 0: handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) self.demoView.logger.addHandler(handler) self.demoView.show()
def test_select(self): store = create_store({},{ "hosts": [{ "url": "1" },{ "url": "2" }] }) mock = unittest.mock.MagicMock() observer = store.select_by_path("hosts.1.url") observer.subscribe(mock) assert len(mock.method_calls ) == 1 assert mock.method_calls[0][0] == "on_next" assert mock.method_calls[0][1][0] == "2" store.dispatch("SET_VALUE", {"path": ["hosts", "0", "url"], "value": "3"}) assert len(mock.method_calls ) == 1 assert store.state == { "hosts": [{ "url": "3" },{ "url": "2" }] }
from layoutx import app from layoutx.store import create_store from layoutx.view import View, ResizeOption store = create_store( { "SET_IMAGE": lambda state, payload: { **state, **{ "data": payload } } }, {"data": ""}) class ImageShowcase(View): geometry = "800x600+200+200" title = "ImageViewer" resizable = ResizeOption.NONE template = """\ Box Label(weight="0") Image Viewer ImageViewer(name="image" background="black" imagedata="{data}") Button(weight="0" height="20" command="{load_image}") New Image """ async def load_image(self): # Find view child widget api not yet finalized imageViewer = self._widget.find_first("image") # Get tkinter attributes
from layoutx import app from layoutx.store import create_store from layoutx.view import View, ResizeOption store = create_store({}, {"facts": [], "loading": False}) class LoadFacts(View): geometry = "800x600+200+200" title = "FactLoader" resizable = ResizeOption.BOTH template = """\ ScrollFrame Label(if="{loading}") Loading, please wait ... Button(command="{load_facts}") load facts Box(for="{fact in facts if fact.type == 'cat'}") Box(orient="horizontal") Label(background="{'grey' if fact.deleted else 'green'}") {fact.text} """ async def load_facts(self): self.store.dispatch("SET_VALUE", {"path": ["loading"], "value": True}) import aiohttp session = aiohttp.ClientSession() facts_list = [] async with session.get( "https://cat-fact.herokuapp.com/facts/random?animal_type=horse&amount=5" ) as facts: facts_list += await facts.json() async with session.get(
store = create_store({}, { "data": """\ { "name": "news", "counter": 0, "isBool": True, "code": "import antigravity" }""", "template": """\ ScrollFrame Button(command="{partial(print_hello, name)}") asyncio Button(command="{reducer}") | Hello {name} Label(:Button-1:="{partial(print_hello, 'label clicked')}") {name} Label hello {getter()} """, "view": """\ async def print_hello(self, txt, *args): import asyncio await asyncio.sleep(1) print("tkEvent", args) print(txt) def getter(self): return 'dynamic getter' def on_drop(self, path): print(path) def reducer(self): self.store.dispatch("SET_NAME", "from reducer") """, "store": """\ { "SET_NAME": lambda state, payload: {**state, **{"name": payload}} } """ })