Introduction
Using python and pandas in the business world can be a very useful alternativeto the pain of manipulating Excel files. While this combination of technologiesis powerful, it can be challenging to convince others to use a pythonscript - especially when many may be intimidated by using the command line.In this article I will show an example of how to easily createan end-user-friendly GUI using the Gooey library. This interface is basedon wxWindows so it looks like a 'native' application on Windows, Mac and Linux.Ultimately, I believe that presenting a simple user interface to your scripts can greatlyincrease the adoption of python in your place of business.
- While OS X comes with a large number of Unix utilities, those familiar with Linux systems will notice one key component missing: a package manager. Homebrew fills this void. To install Homebrew, open Terminal or your favorite OS X terminal emulator and run $.
- Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. The name Pandas is derived from the word Panel Data – an Econometrics from Multidimensional data.
- Suggesting that the issue is with readcsv and not tocsv. The versions I using are: INSTALLED VERSIONS - commit: None python: 2.7.10.final.0 python-bits: 64 OS.
The Problem
I will be basing the example in this article on my prior post - Combining Data From Multiple Excel Files.The basic concept is that there is a periodic need to combine data from multipleexcel files into a 'master file' and perform some additional manipulations.
Unfortunately this process is error prone and time consuming when someone tries todo a lot of manual copying and pasting in Excel. However, it is relativelystraightforward to create python + pandas scripts to perform thesame manipulations in a more repeatable and robust format. However, as soon asyou ask the user to type something like the line below, you will lose them:
Instead, you could present them with a simple GUI that looks something like this:
Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! Asking for help, clarification,. Panda Ultra WiFi (b/g/n) 150Mbps Wireless-N 2.4GHz USB Adapter - Windows XP/Vista/7, Mac OS X, Ubuntu 12.04 TLS, Fedora 17 and Puppy 5.3.3 Compatible Cudy WU1300S AC 1300Mbps WiFi USB 3.0 Adapter for PC, USB WiFi Dongle, 5Ghz /2.4Ghz, WiFi USB 3.0, Wireless Adapter for Desktop/Laptop, Compatible with Windows 7/8/8.1/10, mac OS, Linux.
The nice thing about this example is that you have standard windows directory andfile chooser dialogs along with a standard date picker widget. It will be a muchsmoother transition for your users to use this UI than to try to remember howto use the command line with all the various arguments shown above.
The rest of this article will explain how to create this UI with very minorchanges to the base code you would build using argparse
. If you arenot familiar with argparse
then this article might be helpful to referencebefore you go much further. As shown in the article, argparse
(and friends)are very sophisticated libraries but I have found that you can create verymeaningful and useful tools with the very basic options I'll show in this post.
Install
Gooey requires wxPython for its interface. wxPython can be a littletricky to install but if you are using the Anaconda or Miniconda distributionthe install is as simple as:
I highly recommend using conda for the install process - especially on Windows.
Gooey can then be installed using pip:
Building The Script
This notebook shows the basic idea for this program. What I will do next isbuild a simple version of this using argparse
to pass in the sourceand destination directories as well as a location for the customer-status.xlsx file.
I am going to create a parse_args
function to set up the following required inputs:
- data_directory
- output_directory
- Customer account status file
I will add an example of an optional date argument as well but for the purposesof this example, I do not actually use the value. The golden scrolls online. As they say, that is an exerciseleft to the reader.
The simplest example of argparse would look something like this:
When you are ready to access your arguments, you can get them like this:
One other unique aspect to this code is that I added a simple json dump of thearguments and restore them as the default next time the script is executed. Ioriginally did this to streamline the testing process but realize that this wouldbe helpful in the real world too. Here is the full code sample with the defaultvalues filled in based on the previous execution of the script.
This code allows us to do a basic command line interface that looks like this:
The main section of the code would look like the section below. The basic flow is:
- Get the command line inputs
- Pass the appropriate ones to the input and processing functions
- Save the data to the desired location
Add a Gooey GUI
The command line solution shown above is very familiar to many but I imagine thereare people in your organization that would instantly turn away from a solution thatlooks something like what I have shown above. However, Gooey makes it as easy as two lines ofcode to make a UI for this script.
The most basic steps are to import Gooey and add the decorator in front of thefunction that processes your arguments. Here is what it would look for our example:
Add a Gooey GUI
The command line solution shown above is very familiar to many but I imagine thereare people in your organization that would instantly turn away from a solution thatlooks something like what I have shown above. However, Gooey makes it as easy as two lines ofcode to make a UI for this script.
The most basic steps are to import Gooey and add the decorator in front of thefunction that processes your arguments. Here is what it would look for our example:
When you run this, you would see a simple UI like this:
I think we all agree that this is fairly intuitive and would be something youcould easily explain to your most non-technical users. The other nice thing isthat it runs the same on Windows, Mac or Linux (as illustrated above).
The one challenge would be that users would probably expect to have some nicewidgets to allow them to select directories and dates. If you would like to do thatthen you can substitute the GooeyParser
for your ArgParser
andadd the widget information to the parser code.
Change How to download facebook in desktop.
Ati 102 a924b driver windows 10. to
And add your widget
:
Here's what it looks like to use the DirChooser1
, FileChooser
andDateChooser
widgets:
Pandas Microsoft Sql Server
Now you have some native widgets the look very customary for the host OS:
The other nice feature is that when you execute the program you have a simplewrapper around the display and reasonable error windows if there is anunderlying error in your program.
Pandas Mac Os Catalina
One other handy component is that there is a 'Restart' button at the bottom of thescreen. If you select that button, you can go back to your input screen and adjust anyvariables and re-execute the program. This is really nice if you need to run theprogram multiple times with different inputs.
Part of what I really like about this solution is that there is very littleadditional overhead in your code. A traditional GUI (tkinter, QT, wxWindows etc)would require a lot of code to show this UI. This example showshow unobtrusive the solution can be.
The Final Program
I have not gone into the example of the actual pandas code but you can see in thefull sample that it is relatively straightforward to use the arguments asinputs into your various python function calls. If you would like to see thecode on github, here it is.
Pandas Ma Crossover
I think you will agree that this can be a really useful solution for distributingsmall standalone programs to users that are not comfortable running python from thecommand line. I have built this example around pandas but it would work for prettymuch any python script using argparse. Obviously if you need a more robust solutionthen you'll need to evaluate other options but I would argue that there is a lotof power in this potential solution.
Edit History
- 9-14-2015: Corrected typos
- 9-17-2015: Updated example code imports to include GooeyParser