Creative an interactive in Python is very easy, there are many packages available that can help you to achieve this task. One such tool is Pyinquirer. Letâs learn about building a Python command line tool using PyinquirerâŚ
Letâs start building the Project
For Package Managementđ˘ we are going to use Poetry, Donât know how to use poetry learn it from here https://pratikpathak.com/python-poetry-example/.
We are going to build a simple command line tool, which gives an interactive menu of list of wifi passwords and when the user selects one option it will show the wifi password.
Create a Commandline Python project by using Poetry Python
Poetry can create a Boilerplate template for your project, you just need to use poetry new the command to create a new project. By using this command poetry will create all the necessary files required in a project.
poetry new wifiPwd
This command will create a folder named wifiPwd and inside the folder, you will see a list of files, we will go through each of them individually.
The folder structure looks like this:
D:\PROJECTS\WIFIPWD
â pyproject.toml
â README.md
â
ââââtests
â __init__.py
â
ââââwifipwd
__init__.py
Tip! : You can use the âTreeâ command to view the folder structure
Creating the Driver Code main.py
Letâs create a Python file named âmain.pyâ in the root folder. The project entry point will be from here. Before writing the code letâs install all required packages beforehand.
We will use âPyInquirerâ to create an interactive terminal. letâs install PyInquirer using poetry add
command.
Install Packages using Poetry Add Command
poetry add pyinquirer
This command will fetch the PyPi registry, download the packages from here, and install it in your system. This will also update the pyproject.toml file, it will add Pyinquirer into dependencies.
Now we need a tool for formatting Python code. We will use the âBlackâ package for formatting our code. But this is not mandatory for the project, itâs a dev dependency, We want to format the code for us easily, it has no purpose in production whichâs why itâs a dev dependency
Install Dev Package using Poetry Add Command
poetry add black --group dev
First thing first, Step 0 of our program will be to run the command.
poetry install
This will validate the pyproject.toml
file, and then it will install all the dependencies in the file. After running âpoetry installâ you will see output something like this
Creating virtualenv wifipwd-s1RAI5FV-py3.9 in C:\Users\pratik\AppData\Local\pypoetry\Cache\virtualenvs
Resolving dependencies... (0.1s)
Installing the current project: wifipwd (0.1.0)
Did you notice? It says âInstalling the current project: wifipwd (0.1.0)â, what does it mean?
It means Poetry is treating the whole project as a package. Now you can use import wifipwd
it to import the project functionality directly.
Final pyproject.toml will look something like this â
[tool.poetry]
name = "wifipwd"
version = "0.1.0"
description = ""
authors = ["Pratik Pathak <pratikpathak200@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.9"
pyinquirer = "^1.0.3"
[tool.poetry.group.dev.dependencies]
black = "^23.12.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Now letâs go back to main.py fileâŚ
Here we have to take input from the user in our example wifi name, and then we will just show the output.
How to Interactively take input from commandline python?
You can easily create an interactive menu using Pyinquirer first lets get list all saved wifi password.
You can get the list of saved wifi passwords by using
import subprocess
data = (
subprocess.check_output(["netsh", "wlan", "show", "profiles"])
.decode("utf-8")
.split("\n")
)
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
the list âprofileâ consist all the wifi names. Letâs go on and create an interactive menu using Pyinuirer
from PyInquirer import prompt
question = [
{
"type": "list",
"name": "wifi",
"message": "Choose the wifi whose password you want to see",
"choices": profiles,
}
]
answer = prompt(question)
wifiName = answer["wifi"]
Now we have got the wifi name in âwifiNameâ variable, now the only thing remain is to create a function which returns the password.
The main.py file will look something like this
import subprocess
from PyInquirer import prompt
from wifipwd import wifiPassword
data = (
subprocess.check_output(["netsh", "wlan", "show", "profiles"])
.decode("utf-8")
.split("\n")
)
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
question = [
{
"type": "list",
"name": "wifi",
"message": "Choose the wifi whose password you want to see",
"choices": profiles,
}
]
answer = prompt(question)
wifiName = answer["wifi"]
results = wifiPassword(wifiName)
try:
print("{:<30}| \033[92m{:<}\033[00m".format(wifiName, results))
except IndexError:
print("{:<30}| {:<}".format(wifiName, ""))
Did you notice? we used âfrom wifipwd import wifiPasswordâ, we are treating the whole project like a package, so letâs write the âwifiPasswordâ function. wifiPassword function will be located at âwifipwd > __init__.pyâ.
Now we will add wifiPassword() function, Lets edit âwifipwd > __init__.pyâ, the final â__init__.pyâ will look like
import subprocess
def wifiPassword(name):
try:
results = (
subprocess.check_output(
["netsh", "wlan", "show", "profile", name, "key=clear"]
)
.decode("utf-8")
.split("\n")
)
except subprocess.CalledProcessError:
print("{:<30}| {:<}".format(name, "ENCODING ERROR"))
results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
try:
return results[0]
except IndexError:
return ""
The Project is Almost completed. The last step will be to format the code. We have already installed Black
python package for code formatting, Letâs format our code using black.
How to format code using Black Python formatter?
Itâs very easy to format our code using âBlackâ just run the below command.
black main.py
Pro Tip!: You can also format the whole folder by providing the folder name for example.
black wifipwd
You will get output something like this
All done! ⨠đ° â¨
1 file reformatted.
Note: If you get any error make sure you are inside a virtual environment, use âpoetry shellâ to activate the virtual environment, if it still not working update the black by using âpoetry add black@latestâ
Thatâs it!
You are done. You have successfully created a Python Command line tool, which is also packaged together using poetry.
Top comments (0)