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

Cell grouping for conditional cell set execution #3587

Open
mimansajaiswal opened this issue Jan 27, 2025 · 3 comments
Open

Cell grouping for conditional cell set execution #3587

mimansajaiswal opened this issue Jan 27, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@mimansajaiswal
Copy link

Description

For context, I am developing a web application that processes CSV files for statistical analysis. The application follows multiple decision paths depending on data characteristics. For example, if the data is normalized, specific tests are applied; if not, different tests are implemented or user input is requested before proceeding. Each test may generate multiple outputs and might require linked inputs requiring separate cells, which may also reference each other.

I want to be able implement conditional logic for running a groups of cells. Currently, adding conditions to individual cells is unsystematic and makes navigation difficult. While mo.output.append enables multiple outputs in one cell, I still run into issues when creating interactive features like user input for selecting columns in a t-test, because they require separate cells.

Suggested solution

I have two potential solutions in mind. One involves implementing cell levels using nested functions in Python, which would probably work well. The other approach would be to create a dictionary of functions and call them as needed. Both methods could probably solve this.

Alternative

No response

Additional context

No response

@mimansajaiswal mimansajaiswal added the enhancement New feature or request label Jan 27, 2025
@mscolnick
Copy link
Contributor

Im not sure I follow completely. Are you able to do this today? If so, can you share what part is unsystematic and makes navigation difficult? Breaking into re-usable functions sounds like the right approach.

@mimansajaiswal
Copy link
Author

Sorry about that! Here is simplified example that hopefully makes it clearer.
This is how it currently looks like:

# Cell 1
dropdown = mo.ui.dropdown(
    options=["fuit", "vegetable", "meat"], label="choose one"
)
dropdown


#Cell 2
selected_food_group = dropdown.value

#Cell 3
mo.md("## Fruit Cell Set")

#Cell 4
if selected_food_group=="fruit":
     mo.output.append(mo.md("Yay! You like fruits"))
     fruit_dropdown = mo.ui.dropdown(
    options=["apple", "banana", "mango"], value="apple", label="choose one"
)
    mo.output.append(fruit_dropdown)

#Cell 5
if selected_food_group=="fruit" and fruit_dropdown=="apple":
    ask_apple_fruit = mo.ui.text(value="", label="Do you think apple a day keeps doctor away?")
    mo.output.append(ask_apple_fruit)

#Cell 6
if selected_food_group=="fruit" and fruit_dropdown=="apple" and ask_apple_fruit.value:
        mo.output.append(mo.md(ask_apple_fruit.value+" to keeping the doctor away"))

#Cell 7
mo.md("Vegetable Cell Set")
"""
multiple cells here all that have the conditional selected_food_group=="vegetable"
"""

#Cell 10
mo.md("Meat Cell Set")
"""
multiple cells here all that have the conditional selected_food_group=="meat"
"""

I will love to be able to do something like this

# Cell 1
dropdown = mo.ui.dropdown(
    options=["fuit", "vegetable", "meat"], label="choose one"
)
dropdown


#Cell 2
selected_food_group = dropdown.value

#Cell Group 1 (conditional of selected_food_group=="fruit")
#Cell Group 1 - Cell 1 (original cell 3)
mo.md("## Fruit Cell Set")

#Cell Group 1 - Cell 1 (original cell 4)
mo.output.append(mo.md("Yay! You like fruits"))
fruit_dropdown = mo.ui.dropdown(
options=["apple", "banana", "mango"], value="apple", label="choose one"
)
mo.output.append(fruit_dropdown)

#Cell Group 1 - Cell 2 (original cell 5)
if fruit_dropdown=="apple":
ask_apple_fruit = mo.ui.text(value="", label="Do you think apple a day keeps doctor away?")
mo.output.append(ask_apple_fruit)

#Cell Group 1 - Cell 3 (original cell 6)
if ask_apple_fruit.value:
        mo.output.append(mo.md(ask_apple_fruit.value+" to keeping the doctor away"))

#Cell Group 2 (conditional of selected_food_group=="vegetable")
#Cell Group 2 - Cell 1 (original cell 7)
mo.md("Vegetable Cell Set")
"""
multiple cells here all that have the conditional selected_food_group=="vegetable" but that doesn't need to be specified for each cell
"""

#Cell Group 3 (conditional of selected_food_group=="meat")
#Cell Group 3 - Cell 1 (original cell 10)
mo.md("Meat Cell Set")
"""
multiple cells here all that have the conditional selected_food_group=="meat" but that doesn't need to be specified for each cell
"""

@mscolnick
Copy link
Contributor

I would either split it up into different functions or use mo.stop:

Functions

def render_condition_1():
   pass
def render_condition_2():
   pass
def render_condition_3():
   pass

render_condition_1() is selected == 'fruit' else None

mo.stop

mo.stop(selected != 'fruit')

# render all the fruit views

You might need to include mo.stop( in more than one cell per group, but it won't be all since it stops transitively, so just the "root" cells of each group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants