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.
Polling from a ui.View (built in timers in ui.Views)
-
You can also use async module timer
import ui import asyncio from time import localtime class DigitalClock(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def draw(self): t = localtime() ui.draw_string("{:02}:{:02}:{:02}".format( t.tm_hour, t.tm_min, t.tm_sec), font=('Helvetica', 20), rect=(100, 100,0,0), alignment=ui.ALIGN_CENTER) def update(self, event_loop): self.set_needs_display() event_loop.call_later(.5, self.update, event_loop) v = DigitalClock(frame=(0,0,300, 300), frame_interval=10) v.present('sheet') event_loop = asyncio.get_event_loop() event_loop.call_soon(v.update, event_loop) event_loop.run_forever()
Stop watch example (use the stopwatch.pyui from gist link in previous post)
import ui import asyncio class StopWatch(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.value = 0 self.state = 'stop' def draw(self): t0 = (self.value//(600*60), self.value//600, self.value//10) t1 = (t0[0], t0[1]%60, t0[2]%60) ui.draw_string("{:02}:{:02}:{:02}".format(*t1), font=('Helvetica', 20), rect=(150, 0, 0, 0), color='black', alignment=ui.ALIGN_CENTER) def update(self, event_loop): if self.state == 'run': self.value += 1 self.set_needs_display() event_loop.call_later(.1, self.update, event_loop) def button_action(sender): v1 = sender.superview['view1'] if sender.title == 'Reset': v1.value = 0 v1.state = 'stop' elif sender.title == 'Start': v1.value = 0 v1.state = 'run' elif sender.title == 'Stop': v1.state = 'stop' v = ui.load_view() v.present('sheet') event_loop = asyncio.get_event_loop() event_loop.call_soon(v['view1'].update, event_loop) event_loop.run_forever()
-
This post is deleted! -
@enceladus , they thanks for your code samples also.
I modified your update method in the first example. Just quickly, so it would stop after closing the window. Guessing more checks should be done.
def update(self, event_loop): if not self.on_screen: event_loop.stop() return self.set_needs_display() event_loop.call_later(.5, self.update, event_loop)
According to the docs the BaseEventLoop class is not thread safe. I am not good enough to under the implications/restrictions of this both as a standalone as well how it would interact with Pythonista's Threads. Any insights would be appreciated.
Again, for me at least is just to have a reliable and controllable way to easily add a way to call a so called poll/update method on a class, including a ui.View async.Out of the above implementations again I am not good enough to say which is the best implementation. Appears asyncio is py3+ only. Which is fine by me, other might be looking for a 2.7 solution also.
-
I think that calls in event loop are run on the same thread. Button action (in stop watch example) should be put in the same event loop so that there are no thread-safety issues. As you have mentioned there could be other issues. (My experience in asyncio module is very limited and I am in learning mode.)
def button_action(sender): event_loop.call_soon(button_action_event_loop, sender) def button_action_event_loop(sender): v1 = sender.superview['view1'] if sender.title == 'Reset': v1.value = 0 v1.state = 'stop' elif sender.title == 'Start': v1.value = 0 v1.state = 'run' elif sender.title == 'Stop': v1.state = 'stop'
-
@Phuket2 I've added something to the latest beta. From the release notes:
In custom
ui.View
subclasses, you can now implement anupdate
method (no arguments, except forself
) that gets called automatically by an internal timer. To make this work, you also have to set the newupdate_interval
attribute. It specifies how often the timer fires (e.g. set to 1.0, the timer fires every second). It defaults to 0.0 (disable update timer) because otherwise this new feature might break existing code that happens to implement a method calledupdate
.I hope this works for you.
-
@omz , yup pulled another rabbit out of the hat :) was so excited when I seen the email this morning for the new beta. You have seemed to have done a lot. Just did a quick test with the update method. Appears to work perfectly. Just had a button on a form to start and stop the updates by setting the update interval. Very nice. I will do some more tests with opening Dialogs over the top etc... but many thanks, I think this will make a lot of ppl happy. Ok, back to exploring the new features :)
-
Is the update_interval, and update, attributes in the latest pythonista?
If now, can I get the beta?By the way, it would be nice if the update method was an empty method (just "pass") that already existed, so users could set the update_interval and update method in the subclassed view's init method.
And whatever already exists, or is done in the future, thank you for a truly great product!
-
@technoway It's currently only in the beta. With regards to an empty method in the base class, setting the method in
__init__
etc., that's not really possible because the act of implementing the method is a signal to change the behavior (hope that makes sense). The entire update timer machinery isn't initialized at all if your View class doesn't implementupdate
. -
Thank you for the quick reply.
Am I allowed to install the beta version, and if so, where is it?
I am running Pythonista on an iPhone (and iPad too, but I need this particular functionality on my iPhone).
I have purchased both the Python 2.x and the Python 3.x versions of Pythonista for my iPhone.
-
Ah, I found the message about sending my apple ID through the e-mail. I'll do that. Thanks again, and sorry for all the noise. (I did search before my last post, but missed the relevant post).
-
@technoway You should have a beta invite in your email (please check your spam folder if it isn't there).
-
@omz, arrgh, spam. I have been patiently waiting for my invite, so long that the spam folder has been purged already. Is it easy for you to resend my invite, or should I just send another request? Sorry.
-
@technoway I sent the invite to the Gmail address you sent me yesterday. Did you just purge your spam folder?
-
@omz, I am not technoway. Sorry for jumping in; I have been waiting for the beta invite in order to test the polling functionality.
-
@mikael Ah, I'm sorry, I'll send a new invite to the email address you're using for this forum.
-
@omz, got it, thanks!
-
@omz, some feedback on the
update
feature, all of it positive:- Very stable and consistent. Stops when the view is closed, no threading hassles.
- Very intuitive to use. Changing the
update_interval
to 0.0 stops updates, and positive numbers start them again.
With my limited Threading skills, I was unable to create stable and predictable UI animations with either ui.delay or custom Threads. With
update
, no issues. This is my vote for moving the feature out of beta. -
@mikael Thanks for your feedback! I appreciate it.
-
@Phuket2, thank you for taking this up.
-