1

I'm working on a dashboard with information about tennis professional players.

I got this for loop and I'm getting the desired output:

for ind in df.index:
    if df['Winner'][ind] == 'Nadal R.' and df['Loser'][ind] == 'Djokovic N.':
        print(df.iloc[ind]['Location'], ': ', df.iloc[ind]['Wsets'], '-', df.iloc[ind]['Lsets'])

OUTPUT:

Madrid :  2.0 - 0.0
Rome :  2.0 - 0.0
Rome :  2.0 - 1.0
Paris :  3.0 - 0.0
Rome :  2.0 - 1.0

But when I try to pass it into my dash app I have to create a function for the callback to work.

So I defined the following function (notice that input_value1 and input_value_2 are the same that in the code above by default):

def update_output_div(input_value1, input_value2):

    for ind in df.index:
        if df['Winner'][ind] == input_value1 and df['Loser'][ind] == input_value2:
            return df.iloc[ind]['Location'], ': ', df.iloc[ind]['Wsets'], '-', df.iloc[ind ['Lsets']

OUTPUT:

Madrid :  2.0 - 0.0

What I want to do is to display on my dash app the same output as the code on top.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • (btw the reason for the single line output from your function is that you use `return` which promptly terminates the function. Compare this to `print()` in your first snippet which allows the loop to continue.) – quamrana Jun 07 '22 at 20:28

1 Answers1

2

return closes your function

def update_output_div(input_value1, input_value2):
    res = []
    for ind in df.index:
        if df['Winner'][ind] == input_value1 and df['Loser'][ind] == input_value2:
            res.append(f"{df.iloc[ind]['Location']}: {df.iloc[ind]['Wsets']}-{df.iloc[ind ['Lsets']}")
    return res
dimay
  • 2,768
  • 1
  • 13
  • 22