I would like to make a command line application in Python which, when I call its name, launches a new "mode" or interface at the command line, like launching Vim or Mutt, where key presses like 'q' or 'y' have specific functions - rather than just a script you run which returns some output.
How do I make this? Is there a specific library I would use for making this application? Or are there specific commands that tell the shell to display a kind of "window", and to listen for key presses and to execute commands on them?
You can take a look at this question, there are some useful advices. In particular the best option seems to be the curses extension module.
I suggest also to check out this project that can give you an useful example to do what you ask, even if it uses a GUI kit.
For example, in R you can set options(error = recover), and whenever an error occurs, it will let you inspect all the functions that are active at the time along with all the variables in their scope. It's basically like retroactively inserting a breakpoint wherever an error occurs.
Does pdb or Python have a tool like this?
If you are using VS Code as your editor, you should be able to do this.
Simply click Run > Start Debugging, and select a debug configuration
Screenshot of me using a variable that does not exist:
I've never used an IDE so probably my problem is really basic.
When I start the debug mode, it shows me only this empty screen
pic
How can I let it start correctly?
You need to add breakpoints by clicking on the sidebar near the line count.
Then, when you will start a debugging session the execution of the code will stop at your breakpoint, you can then uses the multiple controls to navigate in your code.
You will also see the variables related to your code.
I have a npyscreen program which has set of options and also I have another normal python command line program which interacts with user by asking yes/no question(s) like a wizard.
I want to integrate the normal python command line program in to the npyscreen program, so when user selects a option I want to run this normal python program. I do not want to reimplement the whole normal python command line program into npyscreen.
Is there anyway to do?
I found one function "npyscreen.CallSubShell" but didn't find any example code and much help in the documentation about this function.
Thanks in advance for any help.
/Shan
npyscreen has CallSubShell function which allows to execute a command line program. The CallSubShell is actually switching from curses mode to normal mode and executes the command using os.system then at the end of the command execution switches back to curses mode.
Note:- I was not able to make the standard input working properly in the command execution. Also you may want to clear the screen before calling CallSubShell.
Thanks for the solution Shan. This works for me. Also as you said, uncommenting curses.endwin() works for scripts that are interactive.
I'm just starting out learning python with GEdit plus various plugins as my IDE.
Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keypress, gets executed in the F# console.
Is there a similar facility/plugin which would enable this sort of behaviour for GEdit/Python? I do have various execution type plugins (Run In Python,Better Python Console) but they don't give me this particular behaviour - or at least I'm not sure how to configure them to give me this. I find it useful because in learning python, I have some test code I want to execute particular individual lines or small segments of code (rather then a complete file) to try and understand what they are doing (and the copy/paste can get a bit tiresome)
... or perhaps there is a better way to do code exploration?
Many thx
Simon
Yes, you use "external tools plugin"
http://live.gnome.org/Gedit/ToolLauncherPlugin
As an example,
Edit > Preferences
Plugins
Tick "External Tools"
Close the Preferences Window
Tools > Manage External Tools
Click the "Add new too" icon in the bottom left
Name it "Execute Highlighted Python Code"
give it a keyboard shortcut
change the input combo box to : "highlighted selection"
change the output to : "Display in Bottom Pane"
In the editor window for the tool, replace everything with :
.
#!/usr/bin/env python
import sys
result = eval(sys.stdin.read())
print expression, "=>", result, type(result)
.
If you wish to see the result of entire .py file, you can put this code in your new created external tool window
#!/usr/bin/env python
import sys
exec(sys.stdin.read())
and change the Input to Current document.
For python, You can use "external tools plugin":
#!/bin/sh
python3 "$GEDIT_CURRENT_DOCUMENT_PATH"
Option of external tool:
Save: Current Document
Input: Current Document
Output: Display in bottom panel
Language: Python or Python3
Don't forget the quotes around $GEDIT_CURRENT_DOCUMENT_PATH....
To answer your second question, and hopefully guide you in a direction you'll be happier with, I think you ought to consider trying some different editors. There are many with more powerful code exploration features than GEdit has. Check out this post:
What IDE to use for Python?
I installed iPython console in gedit and do most of my simple scripting in it, but gedit is a very simple editor, so it'll not have some advance feature like an IDE
But if you want code exploring, or auto completion, I recommend a real IDE like Eclipse.
If you just want a editor, KomodoEdit is fine.
What I do is keep a file called python_temp.py. I have a shortcut to it in my dock. I use it as a scratch pad. Whenever I want to quickly run some code, I copy the code, click the shortcut in the doc, paste in the text and hit f5 to run. Quick, easy, simple, flexible.
I think what you're looking for is http://live.gnome.org/Gedit/Plugins/BetterPythonConsole.
You hit F5 and it runs the code in your file in a IDLE-like console. I don't know if it can only run selected code. (I don't think it can) but you can always copy the needed code in a new window and run it from there.
Have a look through the plugin list for other interesting stuff: http://live.gnome.org/Gedit/Plugins
The closest to a decent IDE...
Install gedit-developer-plugins (through synaptic || apt-get) and don't forget to enable (what you need) from gEdit's plugins (Edit->Preferences [tab] plugins) and happy coding