Python command line interaction library? - python

I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.
It should be crossplatform (Windows and Linux)

A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
provides list of supported commands(they end up being implemented as methods)
help command that can be called to explain one of your commands
handles the whole entering a command, checking syntax, and calling your method command loop cycle.
users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.

You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)

Curses is the most widely used in the Unix environment according to the doc. For Windows you could look at Windows Console Driver, WConio - Windows CONsole I/O for Python or Wcurses. I couldn't find much on cross platform console libraries unfortuntaly.
If your Windows users are CLI users they probably have cygwin which has ncurse support so curses is still the best option if you ask me.

Related

Python Can I Print to Printer using Tkinter

I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.

Python Module for Non-canonical Keyboard Input Processing

I have a Python-3 application that has an interactive command-line interface. Now I would like to add non-canonical keyboard input functionality to this application. I would like it to behave much like how Python interpreter handles its keyboard inputs - for example, keys like up-arrow and down-arrow could scroll command histories and other control keys could move the cursor to beginning/end of line, etc.
I had a cursory glance of getkey and keyboard modules, but what is the best (and easiest) Python 3 module that I could use for this feature?
There are special modules to create interactive command line.
They can use special keys to show history, edit line, auto suggestions.
There is standard module cmd which should have this functionality.
See also extended cmd2
Probably the most popular is Python Prompt Toolkit. Probably even IPython uses it.
See Gallery, basic tutorila and examples with PtPython
Probably most of them use standard modul readline - so maybe you will need this for your project.
Other
Click - probably it can also works as interactive command prompt
bullet - it has interesting menus
qprompt
prompter
promplty
python-prompt
BTW: 4 Python libraries for building great command-line user interfaces

Equivalent of pywinauto library for debian

I'm looking for an equivalent Python library that will work when I run my script on Raspberry Pi. I'm under the impression that pywinauto only works on windows machine.
I want to control some inputs to a GUI launched by my script, using my script. Apart from pywinauto I have no idea how to do this. I thought about using command line to control the software directly, but if there is a simpler way I would really appreciate knowing about it.
Thanks for your time.
ATSPI is an Linux accessibility technology to obtain GUI text/rectangle properties programmatically. See how to find and run ATSPI registry daemon and how to enable ATSPI for the most popular types of GUI apps. Usually it's
$ /usr/libexec/at-spi-registryd &
There is a Python bindings for ATSPI. See this answer for details:
How to install pyatspi?
The pyatspi package has too many dependencies like pygobject etc. It also requires some compilation during installation steps. We think this is not user friendly so we decided to use libatspi.so directly (without any dependencies). This work status can be tracked here: https://github.com/pywinauto/pywinauto/pull/449
There is no exact deadline for pywinauto 0.7.0 with this feature (it's a hobby project), but I would say this summer sounds realistic.

How to use auto completion in Python console applications?

I have a Python console program, which uses input() from builtin to read data from the user. The program has a configuration option, which requires the user to input several paths. I would like to ease these inputs e.g. by a tab-completion.
Is it possible to implement an auto completion e.g. for path names? I see currently no way to hook into the input function to catch tab events/key presses...
Does the Python standard library provide such a feature?
The solution must work on Windows, Linux and Mac OS :).
Please note: I'm not looking for auto completion in Pythons interactive shell/console.
CLI Autocompletion is not included in Python standard library.
You can try:
urwid (http://urwid.org/tutorial/index.html) for a rather low level CLI library
Cement for a full CLI framework (http://cement.readthedocs.io and http://cement.readthedocs.io/en/latest/examples/bash_auto_completion/)
In all cases it's probably quite a bit of work :-/

Making python terminal user interface

I'm trying to make terminal user interface with python which I will use it as post installation script for min linux os. But I don't want to use ncurses or urwid because it feels like overkill. I'm looking more in whiptail or screen direction. But I don't know is it better to call ui terminal rendering from python subprocess or to use it with python bindings like pythondialog, here are the reasons for my doubts.
Is whiptail/screen available on every minimal linux image... subprocess should be better suited for my program.
pythondialog requires installation of python3-dialog package. Since I want to make a postinstallation program for linux min image I want to use dependencies as little as possible.
What would you suggest for my problem?
Maybe npyscreen is what you are looking for, but i havent tried it. It just installed for me in fresh 2.7 virtualenv with zero dependencies - EDIT: sorry no, it runs on top of ncurses.
I've had a similar notion about ncurses or urwid. You might want to try:
prompt toolkit - focus on UI, comes with tons of examples
asciimatics - handy for UI and animations, also with a bunch of helpful examples
Both have a responsive and active community.

Categories

Resources