germaniumhq / mopyx

MoPyX is a MobX / Vue inspired reactive model driven UI library for Python. UI Toolkit independent.
BSD 3-Clause "New" or "Revised" License
37 stars 3 forks source link

Reactivity with Console App #11

Open jbritton-iti opened 2 months ago

jbritton-iti commented 2 months ago

I'm trying this for the first time. This does not call @render function. Am I missing something? Arg, I cannot figure out how to preserve indentation.

from mopyx import model, render

@model class PointModel: def init(self): self.x = 0

@render def render_x(model): print(f"x: {model.x}")

model = PointModel() while True: model.x = input("Enter x: ")

bmustiata commented 2 months ago

You need to change the variable inside an @.***` annotated function.

See here: https://github.com/germaniumhq/mopyx/blob/master/test/test_render.py#L106-L110 or: https://github.com/germaniumhq/mopyx/blob/master/test/test_action_on_model.py

On Sat, Aug 31, 2024 at 2:29 AM jbritton-iti @.***> wrote:

I'm trying this for the first time. This does not call @render function. Am I missing something?

from mopyx import model, render

@model https://github.com/model class PointModel: def init(self): self.x = 0

@render def render_x(model): print(f"x: {model.x}")

model = PointModel() while True: model.x = input("Enter x: ")

— Reply to this email directly, view it on GitHub https://github.com/germaniumhq/mopyx/issues/11, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADPZCQNGEGXA2LYOIPZHF3ZUEE5RAVCNFSM6AAAAABNNKXKVCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ4TQNBWGMZTEMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jbritton-iti commented 2 months ago

I was able to get the following to work. As you noted, I had to modify the model under an action decorator. I also had to make an initial call to the render function in order to get the reactivity wired up. This is not a bug and can be closed. However, I missed the detail of needing to the initial render call to get things wired up. In the README.md under @render is the following: "You decorate your UI rendering functions with @render, or invoke them with render_call. MoPyX will map what render method used what properties in the model. The parameters for the function will be also recorded and sent to the renderer function."

This hints at that wiring up, but I think it could be clearer that the initial render call is necessary.

from mopyx import render, model, action

@model
class PointModel:
    def __init__(self):
        self.x = 0

@action
def set_x(x):
    model.x = x

@render
def render_x():
    print(f"x: {model.x}")

model = PointModel()
render_x()
while True: 
    x = input("Enter x: ")
    set_x(x)