Forum Archive

Syntax inside the Custom Attributes section

marcus67

Does anybody have experience with the new Custom Attributes section of the UI editor in version 2.0? Judging from the description I would expect e.g. '{background_color' : '#f4dbff'} to work but I'm getting a Warning: Could not load custom attributes of view "button_view_legacy": invalid syntax (<string>, line 1) run time error. Thanks!

Webmaster4o

You have the first curly bracket inside the string.

dgelessus

Is this the exact text from your file? If so, you need to swap the first two characters - it has to be {' and not '{. Though in general you're right. You can check the source of the ui module (under Standard Library/site-packages) to find out how the pyui files are loaded.

@omz, seeing that you're parsing the custom attributes using eval, I have to mention ast.literal_eval, which allows only literals instead of arbitrary code. Unless it's intentional that you can do that - there are probably use cases for non-literals, like calling global functions that were defined before.

marcus67

@dgelessus @Webmaster4o No, this is actually not my string. I made a mistake copying it completely the first time and I manually added the missing first character afterwards (in the wrong place). So, I really have {'background_color' : '#f4dbff'} in my view definition. Sorry about that. Still doesn't work.

ywangd

You must use double quotes for JSON, i.e.:

{"background_color": "#f4dbff"}
marcus67

@ywangd Thanks for your advice, but this does not work for me. Same error.

marcus67

@ywangd Sorry, mea maxima culpa. This time I forgot the curly braces altogether. It even works like this: { "background_color" : defaults.COLOR_LIGHT_PURPLE }. :-) I just have to make sure I import my module defaults in the module reading the pyui file.

omz

@ywangd Custom attributes are not JSON, they're interpreted as a Python dictionary literal.

marcus67

@omz What is the reason that single quotes are not excepted? Thanks!

omz

@marcus67 Single quotes should work fine. In your example, it looks like the first quotation mark is misplaced though, it should be after the opening brace ({'background_color': ...} not '{background_color': ...})

Edit: Just noticed that this was already pointed out by @Webmaster4o and @dgelessus

@dgelessus The use of eval instead of ast.literal_eval is intentional. This allows you to calculate attribute values dynamically.

Phuket2

@marcus67 , I have seen this error before. To me it seems intermittent. Assuming your the syntax is correct. But if you call a function or method, it appears you always get that message unless you return an INT.
Eg
{
custom_attr : func_work_it_out(),
}

func_work_it_out() needs to return a INT from what I can see, otherwise you get the warnings. I know it's not 100% the same issue, but guessing it's connected

marcus67

@omz You are right: it also works with single quotes. So, I guess it must have been due to the switched characters. Sorry about the confusion...

rmcmichael

I found that my iPad Pro with the Apple external keyboard case was inserting slanted quotes instead of the UTF-8 quote characters expected by the Python interpreter for string literals. This was causing me to get the error:

Warning: could not load custom attributes of view "xyz": invalid character in identifier

I had to detach the keyboard and use the on-screen keyboard to get the correct string literal characters inserted. Note this applies to the IOS keyboard that pops up when editing custom attributes in the UI designer.

dgelessus

This could be because of iOS 11's "smart punctuation" feature, which messes up quotes, dashes, and other punctuation. (Well, in normal text it's nice, but when programming it's annoying.) Maybe you've already disabled it for the onscreen keyboard, but you may have to disable it again for the hardware keyboard. (Since iOS 10, some keyboard settings are tracked separately for onscreen and hardware keyboards. The "smart punctuation" setting might be one of them.)