Home > other >  Setting function if cell contains string
Setting function if cell contains string

Time:11-22

I had a function that worked fine - it checks the title of a cell for a ipy.datagrid and then sets the color of the cell based on the header

def header_bg_color(cell):
    if cell.value in ['Portfolio -30%','Change -30%']:
        return '#f3722c'
    elif cell.value in ['Portfolio -20%','Change -20%']:
        return '#f8961e'
    elif cell.value in ['Portfolio -10%','Change -10%']:
        return '#f9844a'

I have changed the name of my 'Portfolio -10%' column to 'Portfolio -10%' igwd_change ...where igwd_change is a variable I define earlier.

I thought that simply changing the line from

elif cell.value in ['Portfolio -10%','Change -10%']:
    return '#f9844a'

to

elif cell.value in ['Portfolio -10%'   igwd_change,'Change -10%']:
    return '#f9844a'

Would work, but I get an error

Py2VegaNameError: name 'igwd_change' is not defined, available variables are ['cell', 'default_value', 'index'], note that only a subset of Python is supported

However igwd_change is defined (cell above this one has definitely been run) and I can call the variable in the cell after to check.

Edited to show cell working as desired (Portfolio -10%) yet cell Portfolio 0% (-3.2%) which is Portfolio 0% igwd_change not having the required vega function applied

CodePudding user response:

you need to pass igwd_change variable inside the header_bg_color function as a parameter

def header_bg_color(cell):

should be

def header_bg_color(cell, igwd_change):

Now when calling this function, make sure you pass the same variable

header_bg_color(cell, igwd_change)

or

header_bg_color(cell, "any custom parameter you want here")
  • Related