You could build beautiful apps purely in Python.
No solution is complete without a great UI.
Python has been successful in a wide variety of use cases. But few understand and appreciate its ability in creating graphical user interfaces (GUI.) Indeed we have a handful of extraordinary Python GUI frameworks that helps developers quickly put up an interface to interact with their project.
This post will discuss 3 cross-platform GUI frameworks and 2 web interfacing tools. We'll also cover some basic examples and their strengths so that you can be well-acquainted with picking the right one for your next project.
Here are the five tools you need to know for building helpful UI.
Gradio is a beautiful web UI builder. It’s specifically designed for machine learning projects.
Related post: Create Web UIs for Python APIs and ML Models
Gradio has about 25 different widgets. Each has its own set of configurations. You can wrap each Python function in a Gradio interface and display its output on another widget.
What’s cool about Gradio is its ability to collect feedback. As data scientists, you often need to collect feedback from evaluators on your models.
Gradio has a flag option. If the user finds the model prediction is off. They could click on the flag button. Gradio will collect all the input values, the prediction, and other information.
As most experienced data scientists know, building and deploying models is a highly iterative process. Hence, feedback collection makes Gradio the perfect fit for ML app prototyping.
You could also deploy your app on Huggingface spaces for free. That way you can share your work with a wider audience and collect more feedback. Huggingface only allows Gradio and Streamlit apps (and static pages) on their platform.
Which makes Streamlit the next best Python tool for GUI development.
Streamlit is a fantastic tool for developing web apps using Python. It has a declarative coding style which I found very helpful.
Related post: Secure Your Streamlit App With Django
You can install Streamlit with the following command.
pip install streamlit
Here's a simple application that displays a pandas data frame on a web page.
import streamlit as st import pandas as pd df = pd.read_csv( "https://raw.githubusercontent.com/ThuwarakeshM/PracticalML-KMeans-Election/master/voters_demo_sample.csv" ) st.title("Interactive K-Means Clustering") st.write("Here is the dataset used in this analysis:") st.write(df)
Basic Streamlit example — snippet by Author.
Create a file named
quickstart.py with the above content. To start the streamlit web server, run the following command.
streamlilt run quickstart.py
This command will start a development server, and on your web browser, you can see the following.
You can learn more about Streamlit in my previous post on the topic. There is also a live version of this app you can check out.
The Quick and Easy Guide to Create Wordclouds in Python
How to Create Stunning Web Apps for your Data Science Projects
Plotly has been around for several years. Dash is a framework that extensively leverages Plotly charts to build GUIs with less code.
Plotly Dash is a flexible solution that fits a variety of applications. Their website says it's designed to reach places where Tableau and PowerBI can not.
In my opinion, it does.
Install Plotly Dash with the following single command from the PyPI repository.
pip install dash
To start, you can create a file named
app.py with the following content and run it like any other python script.
import dash import dash_core_components as dcc import dash_html_components as html import plotly.express as px import pandas as pd app = dash.Dash() df = pd.read_csv( "https://raw.githubusercontent.com/ThuwarakeshM/geting-started-with-plottly-dash/main/life_expectancy.csv" ) fig = px.scatter( df, x="GDP", y="Life expectancy", size="Population", color="continent", hover_name="Country", log_x=True, size_max=60, ) app.layout = html.Div([dcc.Graph(id="life-exp-vs-gdp", figure=fig)]) if __name__ == "__main__": app.run_server(debug=True)
Basic Plotly Dash example — snippet by Author.
Run the below code to activate the webserver. It'll initiate a Flask development server and open it up on your default web browser.
You can read more about Plotly Dash in my previous blog post on this topic.
Related post: This is How I Create Dazzling Dashboards Purely in Python.
Plotly Dash and Streamlit are more mature technologies in this list. While they are very helpful in developing a web application, we have tools that help us build cross-platform apps in Python. The next one is a great example.
Kivy is a cross-platform Python software development framework. With it, you can create Desktop applications for Windows, OS X, and Linux, build mobile apps for iOS and Android, and use it on embedded devices on top of Raspberry Pi.
You've read it correctly.
Python is no longer limited to web and desktop development. You can do a lot more stuff you can't do with other languages. Perhaps, that's why Python is the most popular technology in the Stackoverflow developer survey, 2021.
Kivy's documentation includes detailed guides from installation to publishing in Android Marketplace. They are significantly different from platform to platform.
Since I'm on Ubuntu (Linux), I'm using the Kivy official PPA way to install it. Following are the instructions to do it.
$ sudo add-apt-repository ppa:kivy-team/kivy $ sudo apt-get install python3-kivy
Once installed, you can create a file
app.py with the following content and run it to see Kivy in action.
from typing import Text import kivy from kivy.uix.widget import Widget kivy.require("2.0.0") # replace with your current kivy version ! from kivy.app import App from kivy.uix.label import Label from kivy.uix.textinput import TextInput from kivy.uix.gridlayout import GridLayout class HomePage(GridLayout): def __init__(self, **kwargs): super(HomePage, self).__init__(**kwargs) self.cols = 2 self.add_widget(Label(text="Hello, what's your name?")) self.add_widget(TextInput(multiline=False)) class MyApp(App): def build(self): return HomePage() if __name__ == "__main__": MyApp().run()
Basic Kivy app — snippet by Author.
As you can see in the above example, Kivy's Grid layout is handy for arranging UI elements on the screen. You only have to specify the number of columns and start adding elements. Kivy is smart enough to display them in the best possible way.
Of course, Kivy is more than this. From their official docs, you can learn more about GPU optimization, mobile builds, touch support, etc. Here's an excellent Freecodecamp tutorial to watch on Youtube.
Kivy is a great tool to prototype your mobile applications. If your team doesn't have the skillset to develop mobile apps, this may be the best choice.
PyQt5 is a framework built around QT. QT (and PYQT5) supports cross-functional development. It supports Windows, Mac, Linux, and mobile platforms such as iOS and Android.
Besides its impressive UI components, PyQT5 integrates well with native OS APIs. A great benefit of PYQT5 is the QT Designer, a tool that lets you visually compose UI with QT widgets.
You can install the framework using PyPI as follows:
pip install PyQt5
A hello world style app would only have a few lines of code that puts up a nice-looking UI.
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot def window(): app = QApplication(sys.argv) widget = QWidget() textLabel = QLabel(widget) textLabel.setText("Hello World!") textLabel.move(90,85) widget.setGeometry(50,50,320,200) widget.setWindowTitle("My first PyQt5 project") widget.show() sys.exit(app.exec_()) if __name__ == '__main__': window() sys.exit(app.exec_())
Basic PyQT5 example — snippet by Author
QT and PyQT5 are mature frameworks. Because its ecosystem is vast, it might take some time to master it. Yet, it won't be any difficult to find free learning resources.
PyQT5, however, is not free for commercial use. The next one is.
Related post: PyQt5 Tutorial 2021, Create Python GUIs with Qt
Tkinter is a standard Python GUI library. Like Kivy and PyQT5, Tkinter too works on Windows, Linux, and Mac OS. Since Tkinter uses OS native elements, they look and feel like everything else on the platform it's running on.
Tkinter is relatively easy to learn and implement. But its limitation is its lack of advanced widgets. Nevertheless, it's a great tool to prototype your ideas in a very short time.
Official Python installation comes with Tkinter along with it. Yet if you are on Ubuntu, you'd have to install it with the following instructions.
$ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt-get update $ sudo apt-get install python3.9 python3-tk
Once you have installed the package, you can execute the following python script to spin up a hello world app.
import tkinter as tk root = tk.Tk() txt = tk.Label(root, text="My First Tkinter App!") txt.pack() root.mainloop()
Basic Tkinter example — snippet by Author.
The above code will open up a native-looking GUI like the one below.
As you can see, this one is very straightforward compared to our previous tools.
Related post: Python GUI Programming With Tkinter - Real Python
Over the past couple of decades, Python has expanded into multiple territories. Today we use this elegant programming language for data science, workflow integration, software development, etc.
Python is fully capable of GUI development as well. That's precisely what we've covered in this post.
The five tools we discussed here are helpful for almost all GUI development needs. From prototyping to mobile app releases to Google and iOS stores to Web applications. The only skill set you should have for all these tools is Python.
Choosing the right tool for the right project is art. From this list, my suggestion is as follows:
- Looking for quick prototyping? Go with Tkinter
- Need more widgets? Go with PyQT5
- Developing cross-platform and mobile apps? Kivy works well
- Want to build a web app and give users concurrent access? Streamlit is fantastic
- Need more flexibility? Plotly Dash is the answer.
I hope this guide is informative in choosing the Python GUI tool for your next project.
Did you like what you read? Consider subscribing to my email newsletter because I post more like this frequently.
Thanks for reading, friend! Say Hi to me on LinkedIn, Twitter, and Medium.