I am creating a TUI application using Python and curses. And I want to embed/integrate the shell, like you have in VS Code, etc. How can I do this?
I don't just want to capture input and execute it using bash, I want the real shell. E.g. in VS Code you see the real shell prompt (as defined in bashrc) and also you can run a whole application (e.g. vim) from inside the VS code terminal.
I have looked at pty module: not sure if it can help here. I want to basically have a library function like invoke_shell() to which I just pass a reference to a curses window (inside of which I want to show the shell). I do not need to interact with the shell in any other way, except to: (1) close it, (2) capture a keystroke for my application to move focus away from the shell window to another curses window in my application (and move focus back to it when I need it). Is there any such library?
Any help is greatly appreciated! Thanks in advance.
Related
I'm developing a python application and at some point I would like to open an interactive shell terminal (like bash or xterm) for the user. I also need to read and modify the commands typed in this terminal before their execution.
I have a python with a GUI created from tkinter widgets.
I read about the Cmd module for python and it's default method but it does not allow to autocomplete shell commands (only the one added). Also it does not seem possible to keep the terminal state (current directory etc.)
I have read some stuff about embedding xterm with the -into option in my app. It's nearly what I need but it does not allow me to modify the commands typed in before they are launched.
I basically need something like the software faraday has done:
Typed in commands are read and completed on the fly with elements depending on what command was going to be launched.
Thanks
I have no idea if this problem has the solution I want but here goes.
I'm making a PyQt4 program that apparently needs the console window to run properly. However, whenever I activate another window, sending the program I'm working on to the back, the only way I can get back to it is by closing all the windows in front of said window. I can't just click on the taskbar because the only thing that comes back is the console window.
I'm curious. Is there a way to have the GUI window activate along with, or independent of, the console window without having to go through the annoying process of closing (minimizing) potentially all the rest of your windows?
Edit: I just realized my question is pretty vague. Let me elaborate.
I'm compiling said program using pyinstaller.
The reason it needs the console window to work properly (I have tried using the .pyw file as well, to no avail) is because there's another program that's the core of this one that prints out to it in a way I can only describe as violently.
Apparently it won't be happy unless it has the console to record it's outbursts.
That being said, I need the console window. However, as I mentioned before, that is the only thing that comes up when the pyinstaller icon is clicked.
There is a gui attached to the console, but there's no way to get it back even after the user would minimize it because the pyinstaller icon insists it doesn't exist.
Maybe it has something to do with how I defined the window while programming it, but I don't see why that would be the case. Is there something in particular pyinstaller doesn't like that would make it act like this?
How are you launching the PyQt application?
If you're launching it with the python executable, it will create a console.
python my_application.py
Instead, launch it with the GUI version of python -- pythonw:
pythonw my_application.py
If the python path isn't in the system path, you may need to specify the whole path to the executable:
C:\python27\pythonw.exe C:\path\to\my_application.py
I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.
I wrote a program that uses the console. Most of the time, the user must see the console informations. For a specific function from command line, I would like to run the script without the console rises. I just don't want see the window but it can be in the task bar. I know I can use extra modules (gui, win32,..) to do that but I would like to use the standard python librairy.
Is it possible to do that?
The program should run on Windows. (python 2.7)
I specify... I know I can use pythonw.exe too. The question then is how to launch the same script with python.exe sometimes and with pythonw.exe (from command line) for a specific function?
Found this question via google, so to answer the question, how to minimize (not completely hide) the console window on Windows when running a Python script:
# Python 3
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
GetConsoleWindow() will return the window handle for the current console.
ShowWindow(hWnd, nCmdShow) will set the properties for the specific window. 6 is SW_MINIMIZE. Click on the link for other parameters.
on windows there are two python executables in your installation, one is "python.exe", which is the one you typically use. There is another called "pythonw.exe" which is for gui programs. It works just like python.exe, but does not display a console at all.
My first idea would be to have a .pyw-script for that specific task, that is launched in this case. Then only the original script's console pops up for a short time.
Is there any way to inject a command into a bash prompt in Linux? I am working on a command history app - like the Ctrl+R lookup but different. I am using python for this.
I will show a list of commands from history based on the user's search term - if the user presses enter, the app will execute the command and print the results. So far, so good.
If the user chooses a command and then press the right or left key, I want to insert the command into the prompt - so that the user can edit the command before executing it.
If you are on Linux, just fire up a bash console, press Ctrl+r, type cd(or something), and then press the right arrow key - the selected command will be shown at the prompt. This is the functionality I am looking for - but I want to know how to do that from within python.
You can do this, but only if the shell runs as a subprocess of your Python program; you can't feed content into the stdin of your parent process. (If you could, UNIX would have a host of related security issues when folks run processes with fewer privileges than the calling shell!)
If you're familiar with how Expect allows passthrough to interactive subprocesses (with specific key sequences from the user or strings received from the child process triggering matches and sending control back to your program), the same thing can be done from Python with pexpect. Alternately, as another post mentioned, the curses module provides full control over the drawing of terminal displays -- which you'll want if this history menu is happening within the window rather than in a graphical (X11/win32) pop-up.
See readline module. It implements all these features.
If I understand correctly, you would like history behaviour similar to that of bash in
a python app. If this is what you want the GNU Readline Library is the way to go.
There is a python wrapper GNU readline interface but it runs only on Unix.
readline.py is seem to be a version for Windows, but I never tried it.
ncurses with its python port is a way to go, IMHO.