Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Syntax inside the Custom Attributes section
-
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 aWarning: Could not load custom attributes of view "button_view_legacy": invalid syntax (<string>, line 1)
run time error. Thanks! -
You have the first curly bracket inside the string.
-
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 theui
module (underStandard Library/site-packages
) to find out how thepyui
files are loaded.@omz, seeing that you're parsing the custom attributes using
eval
, I have to mentionast.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. -
@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. -
You must use double quotes for JSON, i.e.:
{"background_color": "#f4dbff"}
-
@ywangd Thanks for your advice, but this does not work for me. Same error.
-
@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 moduledefaults
in the module reading the pyui file. -
@ywangd Custom attributes are not JSON, they're interpreted as a Python dictionary literal.
-
@omz What is the reason that single quotes are not excepted? Thanks!
-
@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 ofast.literal_eval
is intentional. This allows you to calculate attribute values dynamically. -
@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
-
@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...
-
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.
-
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.)