Building Data Science GUI Apps with PySimpleGUI | by Tirthajyoti Sarkar | Towards Data Science

Data science on Jupyter notebook is great. But you want to take it further and deploy your project/model on a cloud server. There are many excellent solutions for that — using libraries such as Flask, Django, or Streamlit.
See my article on using Streamlit to build data science apps.
towardsdatascience.com
Although cloud-deployed solutions remain the most popular choice, you may frequently want to build a quick GUI app for your data science and machine learning projects. For example,
To test an ML model with various datasets. You may just want to drag and drop a CSV file onto a model and plot an AUC/ROC curve. A GUI app would be nice, wouldn't it?
Plot a random variable or a statistical distribution in a given range and control the parameters dynamically using a GUI
Quickly run some data wrangling/pre-processing tasks on a dataset interacting through a GUI (instead of writing a bunch of repetitive code)
In this article, we show how to build such a GUI following a minimal learning curve and with intuitive Python code.
There are some highly popular GUI frameworks in Python eco-system like Tkinter, PyQt, etc. But often, they are somewhat bloated and difficult to learn for data scientists, who may want to build simple apps with minimal coding, rather than focusing on building large enterprise-level GUI programs.
As per their website, PySimpleGUI is a “Python GUI For Humans — Transforms Tkinter, Qt, Remi, WxPython into portable people-friendly Pythonic interfaces”. Basically, it takes all these popular and well-established GUI frameworks and wraps them into a single library, which is extremely simple to learn and build apps with.
Basically, one can start producing functioning (and useful) GUI apps using this library with one afternoon’s effort and a basic (not advanced) familiarity with Python. You don’t even have to write a single class definition to produce a GUI app!
Often, data scientists do not come with a ton of software engineering experience. They may be immensely benefited by learning and using a tool that gives them the ability to whip up small, targeted GUI apps for exploring data, analyzing and experimenting with ML models, etc.
Therefore, I started the following repo with the aim of filling it up with short and focused Python scripts, which, when run, will produce desktop apps.
github.com
There are some good-quality tutorials on PySimpleGUI already on the internet. However, in this repo, I specifically focus on creating simple demo programs related to data science (simple analytics, statistical modeling, visualizations, and basic machine learning) using this powerful GUI building tool.
... they are somewhat bloated and difficult to learn for data scientists, who may want to build simple apps with minimal coding, rather than focusing on building large enterprise-level GUI programs
No callback functions needed
It leverages Python constructs to shorten the amount of code. For example, a widget is configured in place, not at a distance of several lines of code.
Single package dependency — Wraps Tkinter and does not require any other packages to be installed.
The same GUI program can be executed on multiple GUI platforms — including a web browser — without changing the source code (only except the import statement).
Able to create GUI for multiple OS platforms — Windows, Linux, Raspberry Pi, and Android (PyDroid3) — with very minor changes.
Basically, one can start producing functioning (and useful) GUI apps using this library with one afternoon’s effort and a basic (not advanced) familiarity with Python.
Install the library,
pip install pysimplegui
You will also need the following for running the examples shown in this article,
Numpy
Pandas
Matplotlib
Scikit-learn
Seaborn
The simplest program in PySimpleGUI can be printing a “Hello World” on a small window. But, in the spirit of data science, let’s make our introductory program, a bit more involved and useful, shall we?
Here is a script. Let’s call it ‘GenRandom.py’.
Go to the directory where you have this script and run,
python GenRandom.py
You will see a simple window pop up where you can click on a button to call the update function (see the code above) as many times as you want to generate a random integer between 1 and 99. Note the while True loop in the code for this infinite loop action.
Although this is a very simple script, it features,
layout (with styling arguments e.g. size and font) and a window
a button element that calls an external function (event)
the function updating a text element of the window object
We can essentially follow the same path and add more layers of layout, events, logic, and widgets to make powerful data science apps.
Here is another script illustrating other widgets. Just run with python FontUpdate.py command and you will see this window pop up where you can dynamically update the font of the text. Here is the demo video,
Run with python QuadraticEquation.py script command and you will see this window pop up where you can enter coefficients of a quadratic equation and get the solution (even if the roots turn out to be complex numbers!)
In this example, we show how easy it is to create a GUI that interacts with a widely used object by data scientists e.g. Pandas DataFrame. As usual, you launch the script by,
python SimpleDataFrame.py
At the start, it will ask you for a dataset file (a CSV).
When you click on the ‘Browse’ button, it will show a file browser dialog first. Make sure you select the correct dataset for this demo from under the data directory.
After you select the cars.csv, you will see other prompts popping up,
If you click ‘Yes’ on that last prompt, you will see the dataset that was read in a new window,
After you close that window, a new popup will ask if you want to see the descriptive statistics about this dataset. If you click ‘Yes’, then you will see something like this,
After you close that window, another popup will ask if you want to see a sample plot. If you click ‘Yes’, then you will see something like this,
This app is to show the ability to create a dynamic plot with user interaction. The script is here. When you run it, a window will pop up with a simple button which you can push as many times as you want, generating a random scatter plot every time. It uses Numpy and Matplotlib in the background, of course.
Data scientists and analysts are always fitting curves or analytical functions to some data. It is easy to create a simple app where you can demonstrate this using PySimpleGUI.
Here is the script. When you run it, a window will pop up with buttons for generating and fitting a 2nd-degree polynomial function. What’s more, we also have put a little slider to adjust the level of Gaussian noise that’s added to the data. In short, this example shows you how to integrate the essential features of data fitting in a lightweight GUI.
Finally, we come down to a real machine learning example. We show how to build a simple app that lets you load the Pima Indians diabetes dataset and fit a Random Forest model to this data using Scikit-learn in the background.
The script is here. The picture below illustrates the running of this GUI program. Note that it covers the standard processes as you expect from a machine learning program.
loading a dataset
options for dropping NaN values and indicating if the columns have headers
displaying the dataset
displaying descriptive stats
selecting the output column from a list-box which is populated automatically when the dataset is loaded
a simple error checker for the output column
running a machine learning classification algorithm in the background and displaying the accuracy on the GUI
We described a simple and powerful Python library for building GUI programs and discussed its advantages as compared to more established libraries. Many excellent tutorials have been written about GUI programming with Python but in this article, we showed focused examples related to data science and mathematical programming, an aspect that is missing from the traditional tutorials.
Here is the official cookbook for the library.
pysimplegui.readthedocs.io
In this article, we showed examples covering
various types of widgets for user control
data input and output — file reading and plot generation
traditional data science libraries e.g. Numpy, Pandas, Matplotlib, and Scikit-learn.
Obviously, so much more can be built upon these examples to build intuitive data science GUI apps. I plan to continue adding such examples in my Github repo which you can fork or star if you like.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.