0

I am trying to use jupyter notebooks ipywidgets interactive to make a selection from a dictionary, within a dictionary, within a dictionary. An example of my dictionary would be as follows:

my_exercise_dic={
'Core':{'Plank':['Plank','Side Plank','Weighted Plank'],'Deadbug':['Deadbug','Weighted Deadbug']},
'Upper Body':{'Pull Ups':['Pull Ups','Chin Ups','Wide Pull Ups],'Push Ups':['Push Ups','Clapping Push Ups']}
}

Basically it's an exercise dictionary, with keys that a broken down into sections of the body, and then each of those sections has a dictionary, which has a key which is the base exercise, and a value that is a list of all of the variations.

What I am trying to do is create a series of dropdown menus, where you select the value of the overall body part, then select the base exercise, then select the variation, and it returns the result as a value. This works in Jupyter if I put the function in an individual cell, and run them each - but this method is very clunky. It looks like this:

from ipywidgets import interactive
from IPython.display import display

# Define any function
def f(select):
    return select
x=interactive(f, body_part=exercises.keys())

display(x)

and then I run the next cell

y = interactive(g,pattern=exercises[x.result].keys())
display(y)

and finally the last one

z= interactive(e,exercise=exercises[x.result][y.result])
display(z)

and I get the value I am looking for with

z.result

Is there a way I can make this one continuous function?

Thank you @Wayne for the provided links - here is what I was able to come up with:

#### This needs to be wrapped in a select new exercise

import ipywidgets as widgets
from IPython.display import display

def dropdown_interaction(option1, option2, option3):
    return option3

# Define the options for each dropdown
options1 = body_part_list
options2 = movement_dic
options3 = sub_movement_dic

# Create the dropdowns
dropdown1 = widgets.Dropdown(options=body_part_list, description='Body Part:')
dropdown2 = widgets.Dropdown(options=movement_dic[dropdown1.value], description='Movement:')
dropdown3 = widgets.Dropdown(options=sub_movement_dic[dropdown2.value], description='Variation:')

# Define the interaction between dropdowns
def update_dropdown2(*args):
    dropdown2.options = options2[dropdown1.value]
    dropdown2.value = dropdown2.options[0]

def update_dropdown3(*args):
    dropdown3.options = options3[dropdown2.value]
    dropdown3.value = dropdown3.options[0]

dropdown1.observe(update_dropdown2, 'value')
dropdown2.observe(update_dropdown3, 'value')

# Create the interaction
x = widgets.interact(dropdown_interaction, option1=dropdown1, option2=dropdown2, option3=dropdown3)

And it returns the result, however I then want to take that result and add it to a Pandas dataframe. What next steps can/should I take?

Here is the code for options 1-3

option 1 = ['Core', 'Lower Push', 'Lower Pull', 'Upper Push', 'Upper Pull']

option 2 = {'Core': ['Plank', 'Deadbug', 'Ab Wheel', 'Leg Raise', 'Tuck'], 'Lower Push': ['Squat', 'Step Up', 'Lunge', 'Split Squat', 'Leg Extension', 'Sled', 'Jump'], 'Lower Pull': ['Deadlift', 'Swings', 'Bridge', 'Leg Curl', 'Clean', 'Snatch'], 'Upper Push': ['Bench Press', 'Push Ups', 'Floor Press', 'Chest Fly', 'Raise', 'OHP', 'Tricep'], 'Upper Pull': ['Pull Up', 'Lat Pull Down', 'Row', 'Shrug', 'Reverse Fly', 'Prone Raise', 'Band Pull', 'Pull Over', 'Curl']}
 
option 3 = {'Plank': ['Plank', 'Side  Plank', 'Push Up to Plank', 'Saw Plank', 'Side with Vert Row Plank'], 'Deadbug': ['Deadbug', 'Heel Tap Deadbug', 'Lateral Hold Deadbug'], 'Ab Wheel': ['Ab Wheel', 'Cart Ab Wheel', 'TRX Ab Wheel'], 'Leg Raise': ['Leg Raise', 'Arcing Leg Raise', 'Eccentric Leg Raise', 'V-Up Leg Raise', 'Hanging  Leg Raise', 'Hanging Knee Raise Leg Raise'], 'Tuck': ['Tuck', 'Pike Tuck'], 'Squat': ['Squat', 'Box Squat', 'Front Squat', 'Front Box Squat', 'High Bar Squat', 'Low Bar Squat', 'Pause Squat', 'Pause Front Squat', 'Goblet Squat', 'Jump Squat', 'Zercher Squat', 'Sumo Squat', 'Single Leg Box Squat'], 'Step Up': ['Step Up', 'Knee Drive Step Up', 'Lateral Step Up', 'Explosive Step Up'], 'Lunge': ['Lunge', 'Reverse Lunge', 'Walking Lunge', 'With Knee Drive Lunge', 'Post Lunge', 'Forward Lunge', 'Elevated Reverse Lunge', 'Explosive Lunge', 'Lateral Lunge Lunge'], 'Split Squat': ['Split Squat', 'Bulgarian Split Squat', 'Front Foot Elevated Split Squat'], 'Leg Extension': ['Leg Extension', 'Single Leg Leg Extension'], 'Sled': ['Sled', 'Push Sled', 'Drag Sled', 'Backwards Drag Sled', 'Rope Drag Sled'], 'Jump': ['Jump', 'Box Jump', 'Depth Jump', 'Broad Jump', 'Single Leg Box Jump'], 'Deadlift': ['Deadlift', 'Romanian Deadlift', 'Stiff Leg Deadlift', 'Split Stance Romainian Deadlift', 'Single Leg Deadlift', 'Sumo Deadlift', 'Pause Deadlift'], 'Swings': ['Swings', 'Single Arm Swings'], 'Bridge': ['Bridge', 'Bench Bridge', 'Single Leg Bridge'], 'Leg Curl': ['Leg Curl', 'Machine Leg Curl', 'Cart Leg Curl'], 'Clean': ['Clean', 'Single Clean', 'Double Clean'], 'Snatch': ['Snatch', 'Single Snatch', 'Double Snatch'], 'Bench Press': ['Bench Press', 'Incline  Bench Press', 'Incline Close Grip Bench Press', 'Incline Neutral Grip Bench Press', 'Close Grip Bench Press', 'Neutral Grip Bench Press', 'Pause Bench Press', 'Incline Pause Bench Press', 'Neutral Pause Bench Press', 'Wide Grip Bench Press', 'Wide Pause Bench Press', 'Pin Bench Press'], 'Push Ups': ['Push Ups', 'Incline  Push Ups', 'Decline Push Ups', 'Hand Release Push Ups', 'Explosive Push Ups'], 'Floor Press': ['Floor Press', 'Neutral Grip Floor Press', 'Close Grip Floor Press', 'Alternating Floor Press'], 'Chest Fly': ['Chest Fly', 'Decline Chest Fly', 'Incline Chest Fly'], 'Raise': ['Raise', 'Lateral Raise', 'Front Raise', 'Front to Lateral Raise'], 'OHP': ['OHP', 'Seated OHP', 'Kneeling OHP', 'Kneeling Lunge OHP', 'Push Press OHP', 'Jerk OHP', 'Single Arm OHP', 'Single Arm Kneeling OHP', 'Single Arm Kneeling Lunge OHP', 'Single Arm Push Press OHP', 'Viking OHP', 'Single Arm Viking OHP'], 'Tricep': ['Tricep', 'Overhead Extension Tricep', 'Skull Crusher Tricep', 'Rope Pull Down Tricep', 'Bar Pull Down Tricep', 'Reverse Grp Pull Down Tricep', 'Single Arm Pull Down Tricep', 'Reverse Grip Single Arm Pull Down Tricep'], 'Pull Up': ['Pull Up', 'Chin Pull Up', 'Neutral Grip Pull Up', 'Wide Pull Up', 'Negative Pull Up', 'Assisted Pull Up'], 'Lat Pull Down': ['Lat Pull Down', 'Wide Lat Pull Down', 'Neutral Grip Lat Pull Down', 'Chin Lat Pull Down', 'Single Arm Lat Pull Down', 'Dual Wide Lat Pull Down'], 'Row': ['Row', 'Bench Row', 'Bench with Pause Row', 'Chest Supported Row', 'Chest Supported with Pause Row', 'Renegade Row', 'Seated Cable Row', 'Bent Over Row', 'Inverted Row', 'Inverted with Pause Row'], 'Shrug': ['Shrug', 'Shrug with Pause Shrug'], 'Reverse Fly': ['Reverse Fly', 'on bench Reverse Fly'], 'Prone Raise': ['Prone Raise', 'Cactus Press Prone Raise', "T's Prone Raise", "Y's Prone Raise", "I's Prone Raise"], 'Band Pull': ['Band Pull', 'Apart Band Pull', "W's Band Pull"], 'Pull Over': ['Pull Over', 'Incline  Pull Over', 'Medball  Pull Over'], 'Curl': ['Curl', 'Hammer Curl', 'Rotational  Curl', 'EZ  Curl']}
  • You could have them all in an hbox and only make the successive ones visible after you selected the first (see [here](https://stackoverflow.com/a/73978799/8508004) about revealing after action)? Or simpler options like https://stackoverflow.com/a/49187073/8508004 and https://stackoverflow.com/q/48407059/8508004 and https://stackoverflow.com/q/45754356/8508004? Let me know if that helps or not. I may take a stab at it if you are struggling. – Wayne Jun 06 '23 at 20:48
  • Thank you @Wayne, I will toy around with this a little bit and see if I can make it work and report my results (or my struggles). The most important aspect is that I need to final result to return a value – pedro flores Jun 07 '23 at 15:30
  • "The most important aspect is that I need to final result to return a value" <-- That really isn't an issue. Each one will have a value. You can handle and combine the value of each however you want and tie it to a single variable if you want. For example, you can make a function or a class attribure that combines them if you want, or just store each in a combined a tuple, like: `final_result =(x.value, y.value, z.value)`. – Wayne Jun 07 '23 at 17:26
  • Ok great, so I have been able to successfully create the dropdown menu based on the links you provided - however, what I am still struggling with is using the returned value - ultimately I would like the selection to be added to a pd DF. Added the code to the original post – pedro flores Jun 08 '23 at 19:38
  • Do you mean you want each selection combination to generate a row in a Pandas dataframe with three columns corresponding to overall body part, base exercise, and the variation? The update is not really giving me an idea of what you want. And pandas dataframes aren't meant to keep updating. They are inherently inefficient for that. See [here](https://stackoverflow.com/a/57001947/8508004), and [here](https://stackoverflow.com/a/48287388/8508004) & [here](https://stackoverflow.com/questions/27929472/improve-row-append-performance-on-pandas-dataframes#comment44256204_27929472). – Wayne Jun 08 '23 at 19:49
  • Sorry for my late reply - it doesn't necessarily have to be a Pandas df - essentially what I was hoping to do would be to select and option from the first dropdown, which returns the sub options in the second dropdown, and finally the 3rd sub set in the last dropdown, and I would like the selection from the final dropdown to be returned as a variable, that I could add to a list or dataframe or access in general. I'm seeing that with widgets I am only able to really print out the answer, but I cannot access it elswhere – pedro flores Jun 22 '23 at 21:02

0 Answers0