Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form on_change handle not registering for dynamic number of forms #1131

Closed
mrdobalina2k opened this issue Apr 15, 2024 · 2 comments · Fixed by #1357
Closed

Form on_change handle not registering for dynamic number of forms #1131

mrdobalina2k opened this issue Apr 15, 2024 · 2 comments · Fixed by #1357
Assignees
Labels
bug Something isn't working

Comments

@mrdobalina2k
Copy link

mrdobalina2k commented Apr 15, 2024

Describe the bug

I want to create a large number of forms that contain the same fields, which I want to add to some collection. I would expect to be able to register on_change handlers for each form, but this seems like it cannot be done.

Environment

{
"marimo": "0.3.10",
"OS": "Windows",
"OS Version": "11",
"Processor": "Intel64 Family 6 Model 126 Stepping 5, GenuineIntel",
"Python Version": "3.12.2",
"Binaries": {
"Browser": "116.0.5845.188",
"Node": "v14.16.0"
},
"Requirements": {
"click": "8.1.7",
"importlib-resources": "missing",
"jedi": "0.19.1",
"markdown": "3.6",
"pymdown-extensions": "10.7.1",
"pygments": "2.17.2",
"tomlkit": "0.12.4",
"uvicorn": "0.29.0",
"starlette": "0.37.2",
"websocket": "missing",
"typing-extensions": "4.9.0",
"black": "24.3.0"
}
}

Code to reproduce

 
import marimo
 
__generated_with = "0.3.10"
app = marimo.App()
 
 
@app.cell
def __():
    import marimo as mo
    return mo,
 
 
@app.cell
def __(mo):
    get_state1, set_state1 = mo.state(None)
    get_state2, set_state2 = mo.state(None)
    get_state3, set_state3 = mo.state(None)
    return (
        get_state1,
        get_state2,
        get_state3,
        set_state1,
        set_state2,
        set_state3,
    )
 
 
@app.cell
def __(mo):
    def new_form(form_name: str, on_change):
        form_ = mo.md("""
        Form
 
        {field}
 
        """).batch(
            field=mo.ui.text("input")
        ).form(
            on_change=on_change
        )
 
        return form_
    return new_form,
 
 
@app.cell
def __(new_form, set_state1):
    set_state1("state 1 not set")
    def on_change_callback_works(*args):
        set_state1("state 1 set")
 
    new_form_ = new_form("test", on_change=on_change_callback_works)
    new_form_
    return new_form_, on_change_callback_works
 
 
@app.cell
def __(get_state1):
    print(get_state1())
    return
 
 
@app.cell
def __(new_form, set_state2):
    set_state2("state 2 not set")
    def on_change_callback_doesnt_work(*args):
        set_state2("state2 set")
 
    new_form("test2", on_change=on_change_callback_doesnt_work)
    return on_change_callback_doesnt_work,
 
 
@app.cell
def __(get_state2):
    print(get_state2())
    return
 
 
@app.cell
def __(mo, new_form, set_state, set_state3):
    # Following https://docs.marimo.io/recipes.html#create-a-table-column-of-buttons-with-on-change-handlers
 
    set_state3("state3 not set")
    def on_change_callback_doesnt_work_too(*args):
        set_state3("state3 set")
 
    arr = mo.ui.array([new_form("test3", on_change=on_change_callback_doesnt_work_too)])
    return arr, on_change_callback_doesnt_work_too
 
 
@app.cell
def __(arr):
    arr[0]
    return
 
 
@app.cell
def __(get_state3):
    print(get_state3())
    return
 
 
if __name__ == "__main__":
    app.run()
@mrdobalina2k mrdobalina2k added the bug Something isn't working label Apr 15, 2024
@akshayka akshayka self-assigned this Apr 15, 2024
@akshayka
Copy link
Contributor

@mrdobalina2k , just wanted to say thanks for opening this bug report. We haven't gotten a chance to fix it yet, but hopefully will get to it this week.

@mrdobalina2k
Copy link
Author

You're very welcome. It's nice to be able to contribute even if it is only with bug reports. Keep up the good work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants