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.
Encapsulating PYUI files into PY files
-
-
@ccc, thanks. Not exactly what I had in mind, some ways better, other ways not as good. Some how this is the point in question, a lot of code ends up in gists, with no searchable references here. I am not skilled enough, but I am sure you and others are skilled enough to be able to help @omz provide a new category here called something like a virtual category 'gists' (created and updated programmatically ) This would be nice. Could look through all the gists (main entries), as well when searching, the virtual gists matches would also be returned.
For my solution, I will keep searching for a way to pass a memory file to ui.loadview(mem_file as path). Maybe will be in 1.6 release. In the mean time, I will keep trying
-
I'm pretty sure
ui.load_view()
supports that already, even in 1.5. You can just pass filename to it as a string argument. At least I think that's how one does it… -
The design point of
Pythonista-Tools
was to be the one place to search for Pythonista-related repos and gists. Not perfect but open to improvement by anyone/everyone in the community. -
@ccc, I understand, and it is great, would even be better if it was mirrored in the forums here. It's all interrelated information. Then you have a one stop shop. Well my humble opinion anyway
-
@Gerzer, could you please elaborate. I Tried many things, fail fail :(
-
Phuket2: Don't use
ui.load_view('__temp.pyui')
. Try omitting the ".pyui" part of the path. The correct code would beui.load_view('__temp')
. Pythonista automatically adds the file extension, soui.load_view('__temp.pyui')
will actually look for the file__temp.pyui.pyui
. -
phuket-
re loading Pyui files from memory: one option is a custom loader, along these lineshttps://github.com/jsbain/uicomponents/blob/master/loadpyui.py
build_view
in this case takes a dict loaded from a json and generates the view.the other option is to use a temporary file, and use the built in method, then delete the temp file.
-
Phuket2, Did you get what you needed on this question? If not, why not create a GitHub repo with this code and we can hack on it together?
Also, do you hack Pythonista on an iPad or an iPhone? The shortness of your doc-string lines makes me think iPhone ;-)
if __name__ == '__main__': ''' write _aview to a temp file as json so ui.load_view(tempfile) can open it and display it '''
-
@JonB, thanks, I looked at the code on github, but the author says its incomplete, does not deal with some GUI elements, so will not use that. Also could require maintence every time a new GUI element is added. Yeah, the temp file is ok(just) , understand I should use the right API, instead of doing my own temp file. I will keep searching for a solution though. As you know my experience is minimal, but with the flexibility I have seen with Python, I can't believe that ui.load_view() can't be tricked into taking a memory file somehow, negating the need for temp files. I did try some things with StringIO, but I failed, that could also be a lack of understanding on my part.
Of course the real answer lies with @omz. I hope he adds a stream type to load PYUI file to his next release. But I understand this is and should be a non issue for him whilst he is working on the next release. This is just something I want to do rather than need to do, although I do think it would be beneficial -
@ccc, hmmmmmm. I am using iPad2 Air. However, no external keyboard. I have one, but at this stage, I need to think more about the Python language rather than typing faster. Really, can't wait for the day, that the on screen keyboard is holding me up. I think that may have been a different issue if pythonistia's keyboard was not so great. I am not even sure I would have continued to learn Python on my iPad, if it wasn't for the excellent implementation of the on screen keyboard.
So, no, I have not found a solution yet. I tried IOString.IOString, no luck. But that could have just been me.
Thank you so much for the invitation to work on this together. I would love nothing more. But I would just embarrass myself at the moment. I am just poking in the dark, with no real direction. Doubtful if I would be able to understand your code, and I would have difficulties forking from github :) so it would be irresponsible for to take you up on your offer, right now (it would waste your time). But when I have enough base skills to participate in something like that, I will be knocking your door down :)
I have to say, so much has changed since I was programming. People are so willing to share and are not intimidated to share their code and expertise. So refreshing. I was also the sharing type, I felt good enough in my skills that I was not fearful to share.
Have a great day -
I do all my Pythonista hacking with the onscreen keyboard and I am quite happy with that but I certainly understand that others prefer an external keyboard.
I wrote my first code in the 1970's and yet I still feel like I am just poking in the dark. However, collaborative coding is all the rage these daze for good reason. We learn faster by working with other folks on code.
One more snippet for you to consider...
pyui_variable.py
This code will create, use, and automatically delete a temporary file.A great place for learning the Standard Library is the Python Module of the Week website and associated book.
-
I'd gladly help you with your code. I need something more productive to do with my programming skills. I'm right now caught up in an OS X app that converts images to ASCII art, so yeah, really productive and useful. It'll revolutionize the ASCII art industry. ;-)
Anyway, PLEASE post your code on GitHub! I insist that you let me help out. :-)
-
@Gerzer, really, thanks for the offer. I have no code to speak of. I have just been trying snippets here and there from time to time. I am still trying to revolutionise the UI world :) it's very hard to improve on what omz has already done. I find myself doing some helper functions/ classes for a few days only to come to the realisation, practically would not help much at all, or worse still more burdensome to use.
I think I have come up with an abstraction, that maybe useful, but it remains to been seen. It's a view layout manager class. So you can for example add a title bar, a bottom bar, a left and right bar or full content bar. The idea these views, self resize, position , handle orientation etc. with help from flex of course. Looking at most iPad apps, made up of these basic elements arranged in different configs. Anyway, early days on that one. May also turn out to be a dud, but fun trying.But I will be out action for the next 10 days for the most part. Friends arriving later today, they will be here for close to 10 days. So I will have little time to do anything during this time.
Would be happy to look at your ASCII art app if you want some feedback. I have a Mac Pro, late 2013 model I think. Not that I am a art guy. But come across some of these apps when I was looking at the python module for creating ascii banners in the console. Would be a nice addition to Pythonistia I think just to spruce up some console apps.But look thanks again, below is the last lame attempt I had with trying to pass a memory file to ui.load_view(). It's a mess because I was trying different things without knowing what I was doing. Just poking in the dark :)
import StringIO import ui, json _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'}] if __name__ == '__main__': fh = StringIO.StringIO() fh.write(_aview) #json.dump(_aview, fh) print fh.buflist v = ui.load_view(fh) v.present() fh.close
-
@ccc, wow. Sounds like you must be older than me. I am 50. I started coding mid 80's. I did most my work in c. I still did HyperCard, SuperCard and later VB. not sure if you ever wrote XCMDs :) all my c work was on Mac's with the 68000 line. So different to python programming :) the thing I remember doing the most was calling hlock(h) so could get to a pointer **h without the memory manager moving the data on me :) but because I sold out and went into management, it's been so many years since I did any commercial programming. Selling out worked out though. Retired very early, and now have time to program for fun a a leisurely pace. Sorry to mix my personal info here, but as there are no profiles, hard to know about people. I think profiles here would be a great addition.
But anyway, thanks for the temp file code. I will save that away. I realise it will be one of those routines you just write off the top of your head, just a lot floating in my head now ;)Also, will look at the website you suggested. When I was younger I would have read and re read the std lib docs. I am enjoying learning by trail abd error at the moment. But if I had to make a living from Python, sure I would take the other approach. When I was a Apple developer, I read most of those menacing white binders, I also attended the WWDC for many years. Back then it was held in San Jose. Also did some disassembly debugging courses at Apple. So I am not shy to learn the hard way, just as I am getting older looking to enjoy it more :)
Again, thanks for your comments. The community here adds to the enjoyment of learning Python.
-
Phuket2, the problem with many of the attempts above is that the parameter that
ui.load_view()
is expecting is a file system path to a pyui file and not a file handle. That is why in my code above, I passed intemp_file.name
instead of justtemp_file
. StringIO and other in-memory files do not have file system paths.The only way around this limitation in the current ui.load_view() method is to create your own replacement function as suggest by JonB above.
-
My age is already
well documented
in the forum. -
@ccc, sorry I was lazy, I just did a Google search or two, I didn't work out your exact age (in code challenge), but pretty close. The day you posted 20,000 days, google reports 54.7582 years. Then days from then until now, was 286 + 16 hours. So you are still 55 and will be 56 in August 22nd. although I am only 50, I have lived 3 life times already ;) the new 56 is the new 30. These young guys can never keep up :)
-
@ccc, on stackflow people have said normally functions that expect a file path would normally deal with a file handle also. I am not sure this is true or not, or if iOS is challenging in this regard. But I understand the temp file concept, I just don't like it. Again, I know it's not life changing, just digging into things. It's a bit of a tangent, but still helps me to learn
-
that is sometimes true, and often a good design, but requires coding it that way specifically. unless the documentation specifically mention that feature, you shouldn't assume it. the tempfile approach is nice in that it will automatically clean up after itself. however the NamedTemporaryFile approach doesn't work on windows, since you can't reopen an open file, so you have to manually delete it.