running command line program from npyscreen select option - python

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.

Related

ubuntu python script use PyAutoGui to press some buttons not run when make the focus on the sublime text editor

I wrote this script in python
import pyautogui
import time
time.sleep(.1)
pyautogui.keyDown("ctrl")
pyautogui.press("a")
pyautogui.keyUp("ctrl")
pyautogui.press("c")
pyautogui.press("p")
pyautogui.press("p")
pyautogui.press("t")
pyautogui.press("enter")
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press(",")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
pyautogui.press("tab")
And I created a shortcut in ubuntu to run it. python3 Scripts/cpp.py
The script works correctly when I make focus on any text element (on the browser for example). But when I make focus on the sublime text or any other text editor It does not work.
What is the reason for this issue?
(This script makes sense and do something useful for me)
If you’re running your program as administrator, pyautogui won’t be able to interact with it. This can catch people out pretty easily because you can set certain programs to always run as administrator, so it won’t be the first thing you think of. This is the case for Windows anyway.
On Ubuntu, from your experience, it seems like it is actually important to run it as an admin. So I guess in general keep the privileges in mind when you have programs interacting with other programs.
Also, your script can be cleaned up a bit.
# Probably a good idea to have a bit of a slightly longer sleep.
time.sleep(0.3)
#pyautogui.keyDown("ctrl")
#pyautogui.press("a")
#pyautogui.keyUp("ctrl")
# Is equivalent to
pyautogui.hotkey("ctrl", "a")
# The next block looks like you're writing text. So write some text.
#pyautogui.press("c")
#pyautogui.press("p")
#pyautogui.press("p")
#pyautogui.press("t")
pyautogui.write("cppt")
pyautogui.press("enter")
#pyautogui.keyDown("ctrl")
#pyautogui.keyDown("shift")
#pyautogui.press(",")
#pyautogui.keyUp("ctrl")
#pyautogui.keyUp("shift")
# Again, use a hotkey here.
pyautogui.hotkey("ctrl", "shift", ",")
pyautogui.press("tab")

Debugging Python in VS code with simple command line (like: debug(function) )

I am used to debugging in RStudio. Using R I simply use the command: debug(my_function), then run the code and automatically the script debugs at my_function.
Now I am using Python in VS Code, I see debugging is possible by setting breakpoints visually using the red dots. However I need to debug a function without actually knowing where this function is stated. So my question is can I debug a specific function in VS Code using a command?
I hope to use something like this command line -> debug(my_function)
Thanks!
It looks impossible for now, I have submitted a feature request on GitHub.
Maybe you can try to copy the function into an isolated python file to debug it for now.
They are called Function Breakpoints
A function breakpoint is created by pressing the + button in the BREAKPOINTS section header and entering the function name. Function breakpoints are shown with a red triangle in the BREAKPOINTS section.

How do I make an interactive application at the command line with Python, rather than just a script which returns output?

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.

Make a full terminal window application like Vim/Mutt/Cmus

I'm not sure quite how to word this, which is probably why I'm having trouble finding an answer.
I have a command line script that runs a rummy game, I want it to take over the terminal kind of like how Vim or Mutt does, so that each round is refreshed in the full terminal window rather than just printing out row after row of text.
Can someone tell me what that is called, so I can research it and find out how to do it?
Repo: https://github.com/sarcoma/Cards
You're looking for a console user interface. One of the best libraries for python would be http://urwid.org/
As mentioned in a comment "pythons curses module does what you require".
This is what you need to take over the terminal: https://docs.python.org/3.9/howto/curses.html

GEdit/Python execution plugin?

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

Categories