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.
Prevent duplicate launch from shortcut
-
Iirc, @dgelessus figured out a way to bypass the preflight altogether. I can't find that thread right now, I think it involved hijacking a call that happened within preflight.. this might be an option in his pythonista_startup.
You probably know all this, but from another thread, here are some methods to avoid module and or variables getting cleared:
##ways to keep modules from clearing:
- for modules, delete file
e.g
import mymodule del mymodule.__file__
now mymodule is not removed from sys.modules
- create name staring with dunder__
import mymodule sys.modules['__mymodule']=sys.modules['mymodule']
- set file access to nonwritable
chmod(mymodule.__file__,0xo444)
- place module in site-packages
##ways to keep global vars from being cleared
1.start variable with__
__myglobalvar=4
2.add to pythonista_startup
myglobalvar=4
import pythonista_startuppythonista_startup.myglobalvar=myglobalvar
-
for things like ui.Views, threads, which needs to reference itself, but does not need to be refernced externally, etc it is sufficient to simply add the var to an existing builtin module
try:
ui.mysavedviews.append(myview)
except NameError:
ui.mysavedviews=[myview]
retain_global
is another way.other thoughts
For modules getting cleared out from under you, the only practical solution iirc is that every function must import what it needs. That's what stash does, and it can survive anything (I think including pythonista://). It is really tedious-- basically anything that can get called after your initial launch cannot rely on modules being imported at the file/module level. Instead, each def must import modules it uses. There is not a great way to check that you remembered everything.
- for modules, delete file
-
Take a look at (here](https://github.com/dgelessus/pythonista_startup/blob/master/_preflight_hook_experiment.py)
And also
Here
at line 39
The first let's you register hooks that get called very early in preflight. I suppose you could check for your UI, and if present, raise an Exception, thus killing preflight. (I'm not 100% sure that's work).Option 2 uses the DirAllTheGlobals trick to patch pythonista_startup's dir to return everything in main -- since preflight checks if each variable is in pythonista_startup.
I'd think you could also create a custom importer, that forced all imports into pythonista_startup where it would be safe, or maybe have a system to "register" required imports/globals associated with an app, that can be cleared when app closes
-
@JonB thanks!
Amazingly, yanking out just the "disable global clearing" part of that pythonista_startup magic worked to allow relaunch from the home shortcut.
So, the ugly tinkering is relatively short: replace the existing pythonista startup module with a new module object containing the globals for the current pythonista_startup module, and which returns all the globals in main when asked for its contents.
This will effectively short-circuit the preflight system from clearing out any globals, since it skips any which are inside the pythonista startup module itself.
Here's the shortened code just to solve this one issue, if anyone else is encountering it:
print(u"Executing pythonista_startup...") try: print(u"Preventing globals clearing...") import sys import types class SaveGlobals(types.ModuleType): import __main__ def __dir__(self): return dir(type(self).__main__) # THESE LINES MUST COME LAST. # Anything past this point is executed in the context of the old # pythonista_startup module, which may already be partially # garbage-collected. saved = SaveGlobals(__name__, __doc__) vars(saved).update(vars(sys.modules["pythonista_startup"])) sys.modules["pythonista_startup"] = saved del sys del types del SaveGlobals del saved print(u"Done preventing globals clearing.") except: # Catch everything import sys import traceback print(u"Swallowed exception:", file=sys.stderr) traceback.print_exc() print(u"Attempt to re-raise", file=sys.stderr) del sys del traceback raise print(u"Done executing pythonista_startup.")
-
@shinyformica hello!
how do you make a homescreen shortcut to a pythonista script?
thanks! -
-
@cvp thank you!
when you say:Altough last version of Pythonista offers an easy way to add an home screen shortcut for an edited script
do you mean the first link above?
-
@jmv38 no, via wrench icon
Problem is that beta expires in 2 days...
-
@cvp actually it does work on the regular version.
Not sure why i didnt see that before.
Thanks. -
@TPO Do you have this on a Github anywhere? It's useful.
-
@nfmusician there you go : https://github.com/TPO-POMGOM/Pythonista-utilities
(I currently lack time to migrate to Github other utilities and modules I have developped for Pythonista, will do it as soon as time allows)