✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Color formatting depending on value returned from a function

Can someone help me understand how to do conditional formatting with a function?

This is my function, and it will return the variance for a stock.

def current_change(stock_id='FB'):
	price_url = f'https://financialmodelingprep.com/api/v3/quote/{stock_id}'
	current_price = get_jsonparsed_data(price_url)
	change = "{0:.2f}".format(current_price[0]['changesPercentage'])
	return change

So I have a Led Display with the number (it’s actually a str) the function will return and would like to make it red if it’s negative and green if it’s positive.

color = '#fc7d7b' if '-' in current_change() else '#88fc4a'

						daq.LEDDisplay(
									id='current_variance',
									label = 'Variance (%)',
									value= current_change(),
									backgroundColor='#272727',
									color=color
									)

@app.callback(
		Output(component_id='current_variance', component_property='color'),
		[Input(component_id='dropdown select', component_property='value')]
			 )

def update_color(current_change):
	color = '#fc7d7b' if '-' in current_change() else '#88fc4a'
	return color

This is the error that I get TypeError: ‘str’ object is not callable - and it’s related to the callback.

Does it have to do with your Dropdown? How is that defined?

I don’t think the problem is with the drop-down because the numbers are updating when a new stock is selected. Maybe I’m not calling the function in the correct way here?

def update_color(current_change):
	color = '#fc7d7b' if '-' in current_change() else '#88fc4a'
	return color

What happens is that the number keeps on showing red even if it’s positive.

Figured out what the problem was…

def update_color(stock_id=stock_options):
	color = '#fc7d7b' if '-' in current_change(stock_id) else '#88fc4a'
	return color

I was forgeting to include the stock_id in the function.