How to prevent commands execution in Pythonista console
Hi, do you know a way (with some esoteric objc maybe?) to bypass (prevent the execution of) the Pythonista internal python interpreter when user presses the key return in built-in keyboard in the Python console after writing a command, in order to run other external scripts written by user?
For example: when I'm in the Pythonista console and I write:
>>> print('hello') ## and press "return" key in the keyboard
I'd like to force Pythonista to execute a script X instead of the normal execution (that is the execution of the command "print('hello')" by the internal python interpreter of Pythonista). So, following this example, I don't want the common output that is:
but I'd like to have, as output and as a simple example, the number of single characters in the string of the command "print('hello')" (if the script X is a script that reads the string written by user and counts its characters). So when I press return key, I'd like to see in console the output:
that are the number of characters of the input string "print('hello')".
The script X that counts the characters of the user input string in console is only an example, only to explain better my question.
in particular, the displayhook. sys.displayhook lets you change the way objects are shown when you type an object in the console. so you could have it print the length of strings, etc.
I don't think
sys.displayhookwould be very helpful here - it only affects how the final evaluated object is displayed, it does not change anything about how the typed-in Python code is executed. In particular,
If you want to affect
sys.stdoutwith a custom file-like object.
If you want to affect the code execution even more than that, you're probably better off writing your own REPL, perhaps based on the standard
codemodule or simply
eval. This lets you run the entered code in a different environment (by passing a dict to
eval) and you can even analyze and modify the code before it gets run (although that might be overkill depending on what you want to do).
You are right, I misread the post.
I would think perhaps the
cmdmodule might be useful here -- for instance if one wanted to write a sagecell console that sends commands to a remote sagecell instance, then prints the responses. Does
codelet you subclass to do pre/post hooks?
@JonB and @dgelessus, thank you for reply, yes my ultimate target is to write something (I don't know if I will have something working) for the Pythonista console able to send strings to sagemathcell server instead of execute them with internal python interpreter and to post-process the output in order to print the output from sage as string in the console (as described in my first example above).
Example of possible usage:
- user enables the by-pass of internal interpreter with the execution of a specific script A as a wrench script
- in this way the return key, when touched on the keyboard, doesn't call the internal python interpreter but reads the full string (the command written by user) and sends it to sage with all required arguments, waits for output and prints any output (output, warnings, errors, etc...) as string with images if existing.
- user can decide if evaluate the output in order to convert it as a number, array, etc...
- when user wants to return to use internal interpreter, a second script B should be executed always through the wrench menu (or the @JonB's StatusBarOverlay.py).
Like the following command in Pythonista console (as example):
>>> minimize(fun,x0,...) ## from scipy.optimize
and the touch of the return key should send the string "minimize(fun,x0,...)" to server with all explicit arguments (fun, X0, args, method, etc...) after entering in the cell string the string "from scipy.optimize import minimize" for proper importing of the required module.
I asked about the existence of some objc commands to by-pass internal interpreter when user touches the return key after inserting the command in the console. Other option is to be able to install in Pythonista some specific library like fire, for example (but it seems to me not pure python, even if GitHub says it is 100% python: I tried to install it in Pythonista but it doesn't work).
Other option is to program a user key with @JonB StatusBarOverlay.py, it is what I will do. So (I hope) I will have a user defined key in upper status bar that simulates the return key of the built-in keyboard but with a specific pre-processing and post-processing action.
But I still have the curiosity to know if with Pythonista (and its several surprises) user can customize the effect of the return key touching in console environment.
Thank you for numerous tips and explanations.
One thing you might consider is making a stash extension. Stash takes care of the history buttons (which otherwise you have to manually manage in a cmd loop).
I think what you might want to do is set up the wrench menu to essentially put you into this mode when you finish a script -- that way you can hang onto the session, etc, and start exploring the data you just manipulated. Then you would use the same response handling from the wrench script to, e.g, show returned images.
@JonB ok with scripts, but I'm searching for a way to use only Pythonista console with sage sarver (it is sometimes more convenient and fast to stay in console environment than to swipe from/to console and script editor).
Sorry for patience, I ask if Pythonista console module (ore some extra objc code) can be used by a wrench action to read console input written by user without pressing the return key on keyboard. I mean:
- I write in console the following:
- I don't press return key on keyboard but I press a wrench action C that reads the input and save it as a string in a variable (for example named "console_input"), so regardless of whether or not I pressed the return button on keyboard, if I write in console:
and press return, I'd like to see the output as a string (not evaluated, like if I do print('a=1.5')):
Can it be possible?