Sign up to take part
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
How do I expand multiple Dataiku variables in a Python notebook? All of the examples I can find only expand 1 variables.
For example,
"The sun is %s and %s" % dataiku.get_custom_variables()['sun_color', 'sun_shape']
Hoping to resolve this to read "The sun is red and round.".
As a work-around, I can load each Dataiku variable into a Python string and then resolve at execution:
sun_color=dataiku_variables['user_sun_color']
sun_shape=dataiku_variables['user_sun_shape']
test = f"The sun is {sun_color} and {sun_shape}"
Thanks!
Hi @matixsnow,
dataiku.get_custom_variables() returns a python dictionary, so you need to follow the same element access methods that you can use with any python dictionary. The python API doesn't allows you to get multiple elements by their key in one single call, as you are using in your example.
Your workaround is actually the appropriate way to access those variables, so you can use them later in the print statement, but you could use other methods to keep everything in one line. For example:
"The sun is %s and %s" % tuple([dataiku.get_custom_variables().get(key) for key in ['sun_color', 'sun_shape']])
Hope this helps!
Hi @matixsnow,
dataiku.get_custom_variables() returns a python dictionary, so you need to follow the same element access methods that you can use with any python dictionary. The python API doesn't allows you to get multiple elements by their key in one single call, as you are using in your example.
Your workaround is actually the appropriate way to access those variables, so you can use them later in the print statement, but you could use other methods to keep everything in one line. For example:
"The sun is %s and %s" % tuple([dataiku.get_custom_variables().get(key) for key in ['sun_color', 'sun_shape']])
Hope this helps!
I was curious, based on the alternative solution you provided, why do I have to pass a tuple and why a list doesn't work instead? Is that just what is required with the get_custom_variables function?
Thanks.
-Jeff
Hi Jeff,
No, it is what requested when using the "%s, %s" % (a, b) method. If you give a list of variables, python complains with the message:
TypeError: not enough arguments for format string
I wouldn't know how to explain it fully, but it has to do with how the % formatting treats lists, as this would work:
"%s" % ['a', 'b']
and print out
"['a', 'b']"
Yeah, that is what I noticed too. I can pass through a list of elements and it runs just fine. It's when you use your method (i.e. "for key in []...") that only a "tuple" works. If you resolve to a "list" the error you mentioned gets displayed. This will be good future reference for anyone who reads this post.
Thank you again for your help and solution!
-Jeff
Ahhh, thanks for confirming my suspicions. Thank you so much for the alternative method!
Have a good day!
-Jeff