I'm trying to write a python script to label multiple columns of an attribute table based on certain parameters. For example, if Benzene is over 79, I want it to be red. I've gotten that part fine, but for some reason I can't get the labels to stack, it only displays the first one the meets the if/then statement. Here's what I have so far:
def FindLabel ( [NAME], [Acetone], [Benzene], [1_3_Butadi] ): if int( [Benzene] ) > 79: message = "<bol>" "<clr red = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" return message + "\n" if int( [Benzene] ) > 0: message = "<bol>" "<clr green = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" return message + "\n" if int( [Acetone] ) > 0: message = "<bol>" "<clr green = '255'>"+"Acetone:" + [Acetone] + "</clr>" + "</bol>" + "\n" return message + "\n" if int( [1_3_Butadi] ) > 0: message = "<bol>" "<clr green = '255'>"+"1,3-Butadiene:" + [1_3_Butadi] + "</clr>" + "</bol>"+ "\n" return message + "\n"
I essentially want it to display like this
Benzene: 79
Acetone: 12
1,3-Butadiene:13
I tried using:
return "{0}\n{1}".format([Acetone], [Benzene], [1_3_Butadi])
That allowed me to stack the labels but then I lost the formatting I wanted.
Solved! Go to Solution.
You will need to build your message. I didn't test the code below but your code should be similar.
def FindLabel ( [NAME], [Acetone], [Benzene], [1_3_Butadi] ): message = '' if int( [Benzene] ) > 79: message = message + "<bol>" "<clr red = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" if int( [Benzene] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" if int( [Acetone] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Acetone:" + [Acetone] + "</clr>" + "</bol>" + "\n" message = message + "\n" if int( [1_3_Butadi] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"1,3-Butadiene:" + [1_3_Butadi] + "</clr>" + "</bol>"+ "\n" message = message + "\n" return message
You will need to build your message. I didn't test the code below but your code should be similar.
def FindLabel ( [NAME], [Acetone], [Benzene], [1_3_Butadi] ): message = '' if int( [Benzene] ) > 79: message = message + "<bol>" "<clr red = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" if int( [Benzene] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" if int( [Acetone] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Acetone:" + [Acetone] + "</clr>" + "</bol>" + "\n" message = message + "\n" if int( [1_3_Butadi] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"1,3-Butadiene:" + [1_3_Butadi] + "</clr>" + "</bol>"+ "\n" message = message + "\n" return message
I see you've got your answer, however I think you can benefit from an 'elif' between the Benzene comparisons. Without 'elif', a value of 80 will get a red label (>79) followed by a green label (>0). With 'elif', a value of 80 will get a red label, then skip the >0 comparison:
def FindLabel ( [NAME], [Acetone], [Benzene], [1_3_Butadi] ): message = '' if int( [Benzene] ) > 79: message = message + "<bol>" "<clr red = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" elif int( [Benzene] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Benzene:" + [Benzene] + "</clr>" + "</bol>"+ "\n" message = message + "\n" if int( [Acetone] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"Acetone:" + [Acetone] + "</clr>" + "</bol>" + "\n" message = message + "\n" if int( [1_3_Butadi] ) > 0: message = message + "<bol>" "<clr green = '255'>"+"1,3-Butadiene:" + [1_3_Butadi] + "</clr>" + "</bol>"+ "\n" message = message + "\n" return message
My friends, here are some ideas on how to take advantage of Python's very handy string abilities...
def report(clr, lbl, val): return "<bol><clr {} = '255'>{}: {}</clr></bol>\n".format( clr, lbl, val) def FindLabel([NAME], [Acetone], [Benzene], [1_3_Butadi]): message = "" if int([Benzene]) > 79: message += report("red", "Benzene", [Benzene]) elif int([Benzene]) > 0: message += report("green", "Benzene", [Benzene]) if [Acetone] > 0: message += report("green", "Acetone", [Acetone]) if int([1_3_Butadi]) > 0: message += report("green", "1,3-Butadiene", [1_3_Butadi]) return message
(If you start a table, dataset, or field name with a number, you may regret it. You have been warned.)
I have the same question.
Could you give me an example of how this is executed in the Python script?
It looks as if your function is returning the message, but how exactly is that put into the feature class or shapefile you are trying to label?
Thanks for the help!
Do you mean, how to build a label expression, or something different?
Darren,
Yeah, that's it! Here is a sample of the script:
Notice, the expression is in VBscript.
I haven't tried running this yet... but I'm 99% sure it's not going to work.
All I'm trying to do is label one feature class with stack text as noted above.
Please excuse my sloppy code, and please let me know if I need to post this as a separate post.