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.
[Share] .pyui viewer / class insertion
-
I am just sharing this because I wanted to make a snippet for myself to quickly view pyui files.
The code looks to simple and you would ask why share it. But it's reading a .pyui file into a custom ui.View class. Not as an object, but as the root view (I think that's how you say it). Anyway, for a long time this was not possible. It's been available for some time now, but still many may not know about it.
If you don't know about it, it is really worth looking at. It's super useful. If you do find it overwhelming as I do many things, don't let it. You don't really need to know the nuts and bolts of it, I don't. You just need know how it set it up and call it. Is really simple now. And it just works.
@JonB come up with this after @omz added bindings params to the ui.load_view and ui_load_str functions. He also refined a lot. There is a link to the forum post in the source
The gist Is here.
-
Hmmm, I really think this is a nice update to what I posted here. I understand some may not think its good. Not sure.
But by using the ViewWalker Class I posted the other day here
And adding self.dict.update(ViewWalker(self).as_dict()) to the PYUIViewer class so you can access all your objects with dot notation rather than self['switch1'] for example.
There is a limitation and that is your instance attrs and all your pyui objects have to have unique names. It's explained in the ViewWalker link. Hmmm, I am not sure it's really such a limitation. In a real app you would want this anyway.
But if you load a pyui file in this manner and all your names are unique you have dot notation access to the objects as well as they are all accessible from the root level.
I guess it would be nice to offer the same functionality (dot notation) in a hierarchical way instead of the flattened out way I am doing here.
Hmmm, not sure I can figure that out just yet, @jonB, maybe that's something for you 😬😬
Still nicer to do self.firstView.SecondView.ThirdView.switch1 = True
Then ...
self['firstView']...['switch1'].value = TrueI am not entirely sure why @omz didn't add this in somehow.
Ok, I think it's exciting....maybe I am clapping with one hand 😱🎉🎉🎉🎉
class PYUIViewer(ui.View): # this acts as a normal Custom ui.View class # the root view of the class is the pyui file read in def __init__(self, pyui_fn, *args, **kwargs): ui.load_view(pyui_fn, bindings={'MyClass': WrapInstance(self), 'self': self}) # call after so our kwargs modify attrs super().__init__(*args, **kwargs) # use the ViewWalker to get a dict of all the view controls/etc self.__dict__.update(ViewWalker(self).as_dict()) # i have a ui.Switch named switch1 in pyui file. # now i can refer to it directly, rather than use string # subscripts ie. self['switch1'] # but if could be self['v1']['sv1']['sv2']['switch1'] self['switch1'].value = False # conventional way self.switch1.value = False # after updating our dict