Add value from key-value pair into other text within label

Hi all! Okay, I have pulled a dictionary from a server module with the results of administration of an assessment of a user from the Database back onto my client form. I now want to add a combination of text and a value from a key-value pair from this dictionary into a label that will appear in a web page generated by the client side form. So for example, I want to say “Age 5 years” in the label, with text in the label generated by combining "Age “, 5 as the value of key child_age from the dictionary, and " years”.

I believe I know how to generate this using python code, but I’m just not sure where to place this code. It appears to me that the label component that I added to my client-side form will just treat everything dropped into the text field as text. So where can the new Python code go? Perhaps there is a good example available in a tutorial on the Anvil site (but I didn’t see it).

Code Sample:

"age_of_child" = "Age " + (str(report_data.get(child_age))) + " years"

But how do I add “age_of_child” to the label? (Obviously if the number was already in string type I wouldn’t need to retype the variable to string.)

Thank you in advance!

You can just set the text property of your label. Here’s an example:

self.label_1.text = age_of_child
2 Likes

Try something like:

age = report_data["child_age"]
self.your_label.text = f"Age {age} years"

The f in front of the string makes the string capable of replacing expressions enclosed in curly braces with their values.

4 Likes