Python is a simple, developer-friendly, and interpreted general-purpose programming language. Python is gaining more popularity every day because of its simple grammar, rich library ecosystem, and highly productive developer environment. Programmers use Python for building web backends, utility scripts, and native desktop applications. Nowadays, Python is very popular among scientific engineers, data analysts, machine learning engineers, and data scientists too.
Programming languages typically become more popular when they reach different developer communities. For example, JavaScript became more popular because of the Electron framework, which lets developers build desktop apps with web frontends. Python also tries to enter new developer communities with impressive innovations done by the Python developer community.
Now we can use Python to build anything like JavaScript. However, most developers still aren’t aware of these impressive Python-based innovations. In this story, I will explain several lesser-known use-cases of Python. Try to use Python for one of these use-cases, and help Python be more popular.
Python for Creating Mobile Apps
Now Dart and JavaScript languages are most favored for building cross-platform mobile applications. In other words, Flutter and React Native dominate the mobile app development market. But, do all Python programmers have to learn Dart or JavaScript to write mobile apps? No — the Kivy GUI framework can produce native apps for Android and iOS platforms already. Therefore, you can use Python to build cross-platform mobile applications.
Kivy comes with its own widgets toolkit similar to Flutter. Kivy uses SDL (Simple Directmedia Layer) for rendering 2D elements on both Android and iOS like Flutter uses Google Skia. Kivy communicates with native mobile APIs via JNI/Cython on Android and Objective-C/Cython on iOS.
Like other mobile frameworks, Kivy also provides toolchains to make platform-specific application bundles. Moreover, you can run Kivy apps on desktop operating systems. Google Play and Apple AppStore accept Kivy-based apps. If many developers start building their apps with Kivy, it can undoubtedly compete with both Flutter and React Native.
Running Python on Browser
The Python interpreter typically runs on the operating system. What if you need to run Python on the web browser? You might think we have to host a Python interpreter instance on a remote server and communicate via the WebSocket protocol. Yes — that’s what most online Python interpreters do. But now, you can run any Python module purely from the client-side with the browser with Pyodide. The Pyodide runs a WebAssembly version of CPython via Emscripten. Browser’s WebAssembly implementation typically offers a virtual CPU-like execution environment.
The Emscripten WebAssembly compiler supports a virtual in-memory file system too. Therefore, we can run Python filesystem API modules on the browser too. See the following code snippet — it will work on the browser.
console.log(pyodide.runPython(`
import os
os.makedirs('dir/test')
print(os.listdir('dir'))
`));
The above code will work with Emscripten’s virtual filesystem implementation and print the following output.
['test']
Now, you can directly run scientific Python code on the web browser. Pyodide supports popular scientific programming libraries such as NumPy, pandas, SciPy, Matplotlib, and scikit-learn. The Pydiode Python implementation also offers a way to access web APIs. For example, you can show a browser alert with the following Python code.
import js
js.alert('Hello JavaScript!') # js module refers to window :)
Now it’s no magic if a Python developer wrote a React application in Python.
Python for Writing Shell Scripts
System administrators usually write Bash scripts for various system maintenance tasks. Developers also tend to automate their repetitive and time-consuming manual tasks with Shell scripts — to boost their productivity. Some programmers also use Python to write Shell scripts since Bash doesn’t offer every language feature that we need from a modern scripting language.
However, you have to write more code than Bash to work with processes in Python. The ShellPy project offers an impressive solution for this issue. ShellPy lets you write Bash-like automation scripts with Python. Look at the following example.
#!/usr/bin/env shellpyprint('Hello ShellPy')
`
echo "Hello" > test.txt
cat test.txt
`
As you already noticed, you can run Bash commands and Python snippets in the same ShellPy script file. You can process data with Python and still use Bash to spawn processes. The great thing is that you can directly get Bash command outputs to a Python variable. Try ShellPy to write your next automation script with clean and simple Python code.
Build Pythonic Hybrid Desktop Apps
Native desktop application development is technically great, but we have to maintain separate codebases for each platform. Therefore, now developers tend to build desktop applications with cross-platform app frameworks and GUI toolkits. Frameworks like Electron offers a way to create cross-platform desktop apps with web frontends. These applications are known as hybrid apps since we embed native features to a web application.
Electron-based applications typically use Node.js code in the backend. But, Python programmers can write Python code for Electron app backends via these two projects: python-gui-electron and electron-python-example.
However, we all know that Electron often produces bloatware due to the overhead of embedded Chromium and Node.js. As an alternative, you can write Python-based hybrid apps with the Eel framework and run them as Chrome/Chromium apps. Eel creates a communication channel between Python and Chrome with a WebSocket connection — by allowing you to write Python code for the app backend.
Also, you can build lightweight and portable hybrid desktop apps with Neutralinojs by using a Python backend.
Create CLI Programs in Record Time
We have to build CLI applications when we make developer tooling for frameworks and libraries. For example, the Flutter team created Flutter CLI to manage and configure Flutter applications. I also created a Node-based CLI program for the framework that I maintain. Moreover, we can turn our automation scripts into CLI programs to enhance usability. For example, you can trigger a particular action or process when you send specific commands to your scripts.
Most programmers use Node.js for building CLI programs with popular Node modules such as commander.js, ink, oclif, and chalk. Libraries like commander.js offer productive chainable methods API to build CLI programs faster.
Google Open Source’s PythonFire project offers you a more productive way to develop CLI programs with Python. PythonFire automatically generates the CLI structure based on function definitions and arguments. Also, you can use this library to convert your existing automation scripts to CLI programs in record time. Once you write the following code:
import firedef add(a, b):
return a + bdef sub(a, b):
return a - bif __name__ == '__main__':
fire.Fire()
You can use the following commands to call both functions.
./cli.py add 10 5
./cli.py sub 5 2
Conclusion
Every programming language now focuses on becoming WORA (Write Once, Run Anywhere). JavaScript became the number one WORA language by covering every developer community.
Python is the next biggest game-changer in the programming world. It came as a simple general-purpose language that is more suitable for processing data. Now, Python slowly enters the mobile app and hybrid app development. We can’t predict yet which will perform better in the WORA competition. Let’s see in 2022!
Thanks for reading.
(levelup.gitconnected.com)
0 Σχόλια