Forum Archive

Pythonista (seems to?) run in background if script starts a Picture in Picture video

cvp

Try to start this script, using your own .mp4 video, then open another app (fi Safari) without stopping Pythonista, and come back in a few minutes, you should see in the ui.View title the number of seconds the script has run.

import ui
import sys
import time
import os

class MyPictureInPicture(ui.View):

    def __init__(self,main_view, movie_path):
        self.main_view = main_view
        self.frame = (0,0,10,10)
        self.hidden = True
        self.web = ui.WebView()
        self.add_subview(self.web)
        self.main_view.add_subview(self)
        mov_url = 'file://' + os.path.expanduser(movie_path)
        html = '''
        <head>
            <meta name="viewport" content="width=device-width,initial-scale=1">
        </head>
        <body>
            <video video {min-height: 400px;} id="videoplayer" loop controls="" autoplay="">
            <source src="mov_url" type="video/mp4">
            </video>
        </body>
        '''
        html = html.replace('mov_url',mov_url)          
        self.web.load_html(html)

    def set_PiP(self):  
        time.sleep(0.2)         
        # wait for it to complete
        while self.web.eval_js('document.readyState')!='complete':
            time.sleep(0.1)                 
        self.web.eval_js('videoplayer=document.getElementById("videoplayer");')
        self.web.eval_js('videoplayer.play();')
        time.sleep(0.5) 
        self.web.eval_js('videoplayer.webkitSetPresentationMode("picture-in-picture")')
        self.main_view.remove_subview(self)

class MyView(ui.View):
    def __init__(self):
        self.frame = (0,0,400,400)  
        self.background_color = 'yellow'
        self.n = 0  
        self.update_interval = 1        
    def update(self):
        self.n += 1
        self.name = 'Test runs for '+str(self.n)+' secs'

# Protect against import    
if __name__ == '__main__':
    mv = MyView()
    mv.present('fullscreen')    

    myPiP = MyPictureInPicture(mv, '~/Documents/Pythonista_runs_in_background.mp4')
    myPiP.set_PiP()

cvp

If you want to create a mp4 from a text, like my example, use

import images2gif
import io
from PIL import Image
import time
import ui

v = ui.View()
v.frame = (0,0,100,100)
v.background_color = 'lightgray'
iv = ui.ImageView()
iv.frame = v.frame
v.add_subview(iv)
v.present('sheet')
t = 'Pythonista runs in   background'
pil_images = []
for i in range(len(t)):
    with ui.ImageContext(iv.width, iv.height) as ctx:
        ui.draw_string(t[:i+1],rect=(0,20,100,60),font=('Menlo',16))
        ui_image = ctx.get_image()
        iv.image = ui_image
        time.sleep(0.1)     
        pil_image = Image.open(io.BytesIO(ui_image.to_png()))                   
        #pil_image.show()   
        pil_images.append(pil_image)

images2gif.writeGif('a.gif', pil_images, duration=0.2)

Then convert (online) your gif into mp4, fi here

cvp

You can find my mp4 here

and download it so

cvp

@mikael did you try it?

mikael

@cvp, just got around to trying it with a random .mov I had on my phone, and can confirm that it seemed to keep Pythonista alive, at least for 47 seconds.

Did you have a use case in mind for this?

cvp

@mikael said:

Did you have a use case in mind for this?

Yes, I've a script that uploads files at the same time to Google Drive, Adrive, iMac and an usb flash on my router, and that can take some time, and during this transmission I was not able to use my iPad for other tasks...

I just wanted to find a way to run Pythonista in background and I had already tried a lot without success.

mikael

@cvp, congratulations. Let us now if you run against some limit, but probably not.

PiP is also cool to have in itself, and I guess even more workable on an iPad than on the phone.

JonB

Is this any different from, say, split screen?

cvp

@JonB It is different because split screen allows your Pythonista to run UI and we also are sure there is no limit of usage nor duration.
I should still test this "simili-background" mode which is more like a music application or like download in safari or copy in Files app, all of them can use cpu without being visible on the idevice.

aldoblack

Did you find any solution? I am trying to run Pythonista in background as well.

cvp

@aldoblack no, excepted short scripts running without user interaction and relaunched by a shortcut automation.