How do I create an attribute dynamically while using yattag?

398 views Asked by At

This is the part of 'data' in the code below:

{'element': 'background', 'x': 0, 'y': 0, 'width': 800, 'height': 898, 'properties': {'background-color': '#ffffff'}}

The code:

for i in data:
    print(i)
    if i['element'] in comp:
        ta = i['element']
        if i['properties']:
            prop_keys = list(i['properties'].keys())
            background = "green"
            for j in range(len(prop_keys)):
                attri = prop_keys[j]
                print(type(attri))
                val = i['properties'][prop_keys[j]]
                print(type(val))
                doc, tag, text = Doc().tagtext()
                exec('with tag(ta, %s = %s)'%(attri,val))
    break

The error that I am getting:

    Traceback (most recent call last):

  File "/home/harsh/.virtualenvs/py3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-78-69185a93c0dd>", line 19, in <module>
    exec('with tag(ta, %s = %s)'%(attri,val))

  File "<string>", line 1
    with tag(ta, background-color = #ffffff)
                                            ^
SyntaxError: unexpected EOF while parsing

I basically want to use value of 'attri' as the name of attribute in the yattag to make it dynamic. I tried using 'exec' function to put in the values to convert the json data into html using yattag but the yattag library doesn't allow to use expression in place of variable name to make it dynamic.

0

There are 0 answers