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.
pyui to code conversion
-
Has anyone written code to convert a pyui file to python code?
-
I'm trying to understand what the use case would be here. Under what circumstances would you have a .pyui file without the corresponding .py file that would require you to "decompile" the .pyui file or convert the code into its graphical representation?
@wradcliffe, yes, now I see the point and yes that would be useful. I like using the ui editor versus not using it but I don't like having no way to inline the .pyu code to eliminate the need for publishing another file.I haven't tried but does inlining the pyui code in a main .py file, reading it to a file and then passing that file to ui.loadview() first of all work and second of all, provide a useful way to combine both files? Or are some of the posters saying the same basic thing?
Still, is the main usecase that you want a single file or that you want added control over the UI construction code, or both? As I said it should be possible to inline the .pyui code into the main .py but that might not go far enough for what you had in mind.
-
@ltddev: I presume @wradcliffe would like to have a human readable form of the pyui file so that manipulating its parameters in the texteditor is possible. Its a nice idea. Sounds like a project.
-
@ltddev - I would like to use the pyui file to prototype an app and then convert over to a "standalone" single file script. The standalone script will also be able to adjust the ui in ways not possible with a static pyui.
-
pyui_to_clipboard.py and pyui_from_clipboard.py
allow you to have a human readable form of the pyui file so that manipulating its parameters in the texteditor is possible. They hardcode filenames, etc. so they need work but they are a good start towards the goal of @polymerchm.The goal of @wradcliffe, to convert pyui into Python code, is much more ambitious. A useful proof of concept would be to do it for one class (ui.Button for instance) to see what issues arise.
-
I once wrote an alternative to load_view, with the goal of beig able to late bind methods from custom classes (for instance, actions that point to the custom view). I think I may have lost it in a shellista rm * disaster, but the basic idea was that it is easy to get a valid dict, as ccc showed.
Each node has a class or customclass, frame, attributes, and nodes, which is a list of subviews. You can walk the dict, creating objects and setting attributes as you go. There are a few things like colors, and frame that have to be handled special. TableViews are another edge case.
I'd suggest, rather than trying to create python code, you just extend the pyui dict and write a custom loader.
-
@JonB - @ccc - I was hoping for the python code generation, but the alternative load_view would have covered the basic dict walking procedure. Too bad you lost it. The general case is probably too much work for such an obscure use case as mine. There might be value in printing out boiler plate code that would take away a lot of the grunt work of a manual inspection and conversion from pyui to python code.
-
Finally got around to rewriting the alternate load method. Still somewhat in work, to allow actions and delegates to point to instance objects... https://github.com/jsbain/uicomponents/blob/master/loadpyui.py
-
I tried this out on some existing projects and it handled everything perfectly. This should help make conversion from pyui to straight code a quicker process. Thank you. Glad to hear that this work was not lost!