Appreciate any comments, suggestions, on the code below.
The code below is not meant to be finished or refined, but just an idea. From my searches here, I can't find anything relating to this, I could have missed it.
The idea is simple, just to be able to encapsulate 'PYUI' files in 'py' files. What I did so far, seems easy enough to read in a PYUI file copy the contents, copy it into your Py file and assign it to a variable. I could find no way to pass that variable to a ui function that would render the view. So, I write the var to a temp file and load the view that way. It appears to work, although it's not very graceful. I am not sure if people think this is a waste of time or not. Intrested if what you think. I thought it could be handy for posting code here on the forum, also a quick and dirty way for moving projects quickly between devices. What would be great also if there was a ui API call that rendered the view from the data, rather than writing a file. Maybe there is a memory type file I could pass to ui.load_view() were the load_view() call would not know the difference. I looked, but could not see a way to do that. Above my experience.
# coding: utf-8
import ui, json, clipboard
_aview = [{u'attributes': {u'custom_class': u'main', u'flex': u'', u'name': u'Main', u'border_color': u'RGBA(0.000000,0.000000,0.000000,1.000000)', u'enabled': True, u'tint_color': u'RGBA(0.000000,0.478000,1.000000,1.000000)', u'background_color': u'RGBA(1.000000,1.000000,1.000000,1.000000)'}, u'frame': u'{{0, 0}, {320, 504}}', u'nodes': [{u'attributes': {u'flex': u'', u'name': u'switch1', u'border_color': u'RGBA(0.000000,0.000000,0.000000,1.000000)', u'enabled': True, u'value': True, u'uuid': u'E3628BC2-CB40-43BE-A68E-DFDFE587E315'}, u'frame': u'{{238.5, 19.5}, {51, 31}}', u'nodes': [], u'class': u'Switch'}, {u'attributes': {u'flex': u'', u'font_bold': False, u'font_size': 15, u'uuid': u'62C7DAF4-12CF-4FF6-9EAA-8B443398D3BD', u'title': u'Ok', u'border_color': u'RGBA(0.000000,0.000000,0.000000,1.000000)', u'enabled': True, u'action': u'hit_test', u'name': u'button1'}, u'frame': u'{{224, 455.5}, {80, 32}}', u'nodes': [], u'class': u'Button'}, {u'attributes': {u'flex': u'', u'font_size': 17, u'name': u'label1', u'text': u'Test Label', u'border_color': u'RGBA(0.000000,0.000000,0.000000,1.000000)', u'enabled': True, u'text_color': u'RGBA(0.000000,0.000000,0.000000,1.000000)', u'alignment': u'left', u'uuid': u'6F6DFFF1-495A-4468-A6B1-E7CEC7508E34'}, u'frame': u'{{17.5, 18.5}, {150, 32}}', u'nodes': [], u'class': u'Label'}], u'class': u'View'}]
def copy_pyui_to_clipbord():
'''
open a .pyui file, read it with json,
copy the contents to the clipboard,
so can be pasted into a .py file
'''
fh = open('json_test.pyui', 'r')
x = json.load(fh, encoding = 'utf-8')
clipboard.set(str(x))
fh.close()
if __name__ == '__main__':
'''
write _aview to a temp file as json
so ui.load_view(tempfile) can open it
and display it
'''
fh = open('__temp.pyui', 'w')
fh.truncate()
json.dump(_aview,fh )
fh.close()
v = ui.load_view('__temp.pyui')
v.present('sheet')