Variable Expansion on Multiple Variables in Python Notebook

matixsnow
matixsnow Registered Posts: 4 ✭✭✭

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!

Best Answer

  • Ignacio_Toledo
    Ignacio_Toledo Dataiku DSS Core Designer, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 415 Neuron
    edited July 17 Answer ✓

    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!

Answers

  • matixsnow
    matixsnow Registered Posts: 4 ✭✭✭

    Ahhh, thanks for confirming my suspicions. Thank you so much for the alternative method!

    Have a good day!

    -Jeff

  • matixsnow
    matixsnow Registered Posts: 4 ✭✭✭

    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

  • Ignacio_Toledo
    Ignacio_Toledo Dataiku DSS Core Designer, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 415 Neuron
    edited July 17

    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:

    "['a', 'b']"

    and print out

    "%s" % ['a', 'b']

  • matixsnow
    matixsnow Registered Posts: 4 ✭✭✭

    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

Setup Info
    Tags
      Help me…