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.
Programmable keys in Pythonista for quickly wrench actions execution
-
@JonB Impressive, really! Thank you! I still find hard to believe that you can do certain things with Pythonista.
I tried your script in my iPhone 4'' screen and works very well.
I've seen that the user buttons remain in the three Pythonista windows: file editor, text editor and python console. It is a wonderful thing in my opinion: in this way user can have quickly access to favourite actions in file editor window (for actions an files and folders) and in text editor window (for actions on text and strings).Your code is a little incomprehensible to me but I will try to adapt your work in order to use it with my frequent actions.
Thank you @JonB and @cvp for all help!
Regards -
@JonB Hi! Sorry but I can't solve a little problem with your code.
The procedure I follow to reproduce the issue is:
- full restart of Pythonista,
- run your script in order to show the half button in right up corner,
- write a simple python script like
print('hello')
, - execute the script with |> button (it automatically goes to console window),
- return to text editor windows and touch the half button,
- it return
NameError: global name 'ui' is not defined
.
Note that the user buttons in action bar work well: if I touch for example the button 'Blah' it print in console the number 50.0. The problem is that I can't anymore hide the full action bar.
When you have time, can you kindly test the issue? Thank you very much,
Regards
PS: I use Pythonista 3.1 (301016).
-
@Matteo put the import ui in the action def...
-
Right, your actions have to stand alone, and cannot rely on globals, or external imports. All imports must happen in the def itself. That's because globals and modules get cleared.
-
@JonB Why did __persistent_views not disappear?
-
-
@Matteo Really, @JonB did all the work, it was described in his comment at begin of script
''' A set of tools to add or delete custom buttons from the toolbar. This may not be super robust, but seems to work ok. Button objects and actions are saved so they survive global clearing, but thid has not been tested extensively. If a function relies on imports that occured outside of the function, these might dissappear -- user must make sure those modules are added to a module that is kept by pythonista, such as anything in site-packages, or name starting with __ '''```
-
@cvp variables with double underscores don't get cleared when running a script. Nor do modules in site-packages, modules starting with dunders, and a few other rules which I can never remember (you can look at pythonista_preflight.py)
-
@JonB Ok, understood, thanks. But, in this case, why to set this variable as global?
-
@cvp Yes you are right, I've tried to read JonB string at the beginning, but I don't have great programming knowledge so, when I read, lets say, 100 words about programming, I understand, the first time, about 20-30 %.
Anyway I will try to work with JonB script, that in my opinion gives to Pythonista much power for little screens (I repeat that Wrench action window of Pythonista is a wonderful feature, but in my little screen its window hides all my screen preventing me to see my code in text editor window).Regards
Bye -
-
@JonB Sorry, I feel very very little/bad/novice/zero (delete eventual useless adjectives, if any) but even that I can't 😢: I don't find this Pythonista_preflight?py
-
@cvp It's an internal script used by Pythonista, you can't find it anywhere in Pythonista's file browser. You can open it using the
editor
module though:import editor import os py3kit_dir = os.path.dirname(os.path.dirname(os.__file__)) editor.open_file(os.path.join(py3kit_dir, "pykit_preflight.py"))
-
@dgelessus Thanks, but I searched for pythonista_preflight.py 😢
-
@dgelessus & @JonB Thanks to both to help me to learn each day something's new.
-
It's the same script - in older versions it was called
pythonista_preflight.py
, then it was renamed topykit_preflight.py
. I think @JonB was just using the old name. -
@dgelessus Thanks
-
By the way, here are my notes on ways to keep variables/modules from getting cleared by preflight:
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
2) 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
- start variable with __
__myglobalvar=4
- add to pythonista_startup
myglobalvar=4 import pythonista_startup pythonista_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. - for modules, delete
-
@JonB Thank you for these advanced info.
About your code, I've tried to understand how to add an extra horizontal bar with extra six buttons under the default bar.
In this the extra bar under the main one will hide the filename , the run button |≥ and some other built-in keys. But this is not a problem because user can hide the two bars with one touch on the half button in high corner in order to use run key or others.
The target is to have about 10/12 user keys that , when visible, hide the entire height of Pythonista bar.
Unfortunately I can't create this extra bar under the one you see in image.As always, I ask you if you have time how I can modify your code to add an extra bar with extra 6 buttons.
Thank you again
Regards -
Hi! The empirical approach always helps (almost always indeed). After some tests with some changing of numeric values and adding some extra code (copying/modifying some JonB's pieces of code of his Great script) the output is the following (images below).
First (file browser):
Second (console):
I wait to post the full code (that is the JonB's code slightly modified) because I have a little problem with screen rotation: from portrait to landscape and from landscape to portrait the two bars can't return to default one (you can see the problem in image below: the bars become very large...):
I will try to solve this bug that I've introduced modifying the JonB's script and if I will find a solution I will post here the full code, that you can personalize with icons and actions (I can't insert text in background under the buttons/icons ... It would be useful for whose like me can't remember the action of each button/icon without any keyword like, for example, 'find in files' or 'on/off' word wrap).
Regards