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']}