Outils pour utilisateurs

Outils du site


issue143:python

Ceci est une ancienne révision du document !


A GUI for Pandas We will continue with our learning series on Pandas. So far, most all of our work has been done on the command-line. Now, we will create a GUI application using Page, Tkinter and a third party widget called pandastable. Pandastable was created by Dr. Damien Farrell, and is based on an older project of his called tkintertable. It is a wonderful widget for dealing with the things we have learned about Pandas in a GUI format. You can get the entire source code for pandastable at https://github.com/dmnfarrell/pandastable. Dr. Farrell asks that the following citation be included, so here it is… Farrell, D 2016 DataExplore: An Application for General Data Analysis in Research and Education. Journal of Open Research Software, 4: e9, DOI: http://dx.doi.org/10.5334/jors.94

Une interface graphique pour Pandas

Nous continuerons notre série sur l'apprentissage de Pandas. Jusqu'à maintenant, la plus grande partie du travail que nous avons fait l'était en ligne de commande. Maintenant, nous créerons une application d'interface utilisateur graphique (GUI - Graphical User Interface) en utilisant Page, Tkinter et un gadget tiers appelé pandastable.

Pandastable a été créé par le Dr Damien Farrell et il est basé sur un de ses anciens projets appelé tkintertable. C'est un gadget merveilleux pour traiter les choses que nous avons apprises sur Pandas dans un format de GUI. Vous pouvez récupérer tout le code source de pandastable à https://github.com/dmnfarrell/pandastable. Le Dr Farrell demande que la citation suivante soit incluse, donc la voici…

Farrell, D 2016 DataExplore: An Application for General Data Analysis in Research and Education (Une application pour l'analyse générale des données en recherche et en éducation). Journal of Open Research Software (Journal du logiciel de recherche libre), 4: e9, DOI: http://dx.doi.org/10.5334/jors.94

Now, let's get started. In order to use the pandastable widget, we need to install the library. This can be done using pip at the terminal level. Since we will be using Python3 for this project, we'll use pip3. If you are using Python2.x, use “pip”. The command is… pip3 install pandastable (If, when you try to run the below program, you get an error message saying something about “from pandas.tools import plotting - ImportError: No module named tools”, this is likely due to the version of pandas that you have installed being version 0.19 or lower. Try updating your pandas library (pip3 install –upgrade pandas)). I will be using the latest version of Page (4.21) that was released on March 1, 2019 and can be downloaded from https://sourceforge.net/projects/page/ .

Maintenant, commençons. Pour utiliser le gadget pandastable, nous devons installer la bibliothèque. Ceci peut être fait en utilisant pip au niveau du terminal. Comme nous utiliserons Python3 pour ce projet, nous utiliserons pip3. Si vous utilisez Python 2.x, utilisez « pip ». La commande est…

pip3 install pandastable

(Si, quand vous essayez de lancer le programme ci-dessous, vous recevez un message d'erreur disant quelque choses comme « from pandas.tools import plotting - ImportError: No module named tools » (de pandas.tools, suivi d'importation - Erreur d'importation : pas d'outils nommés du module), c'est probablement dû à la version de pandas que vous avez installé, qui serait la 0.19 ou moins. Essayez de mettre à jour votre bibliothèque pandas (pip3 install –upgrade pandas)).

J'utiliserai la dernière version de Page (4.21) qui a été publiée le 1er mars 2019 et peut être téléchargée depuis https://sourceforge.net/projects/page/ .

Now that we have pandastable and Page, we can go ahead and start designing the form. Start Page and move the new Topmost form to somewhere near the center of the screen. You can size it to any dimensions you wish, but I used 1004 for the width and 785 for the height for this demo. Set the title to “Pandastable Demo” in the Attribute Editor. We will add two frames, one “standard” Tk button and one Page custom widget. That's all we need. The first frame will be called “frameToolbar” (widget alias), and should be placed at the very top of the form. I used X=2 and Y=2, a height of 40, and a width of 1000. The second frame will be called “frameCustom”, and will hold our custom widget (pandastable). I placed it a few pixels below the toolbar frame at X=2, Y=43, and set the width to 1000 and the height to 735.

Next, place a standard Tk button within the toolbar frame. Set the Alias to “btnExit”, the text to “Exit” and set the command attribute in the Attribute Editor to “on_btnExit”. This will create a callback function for when the button is clicked. Finally, scroll down to near the bottom of the Widget Toolbar and select “Custom”. Then click within the frameCustom widget to place our custom widget placeholder. In the Widget Tree, right-click the entry that says “Custom: Custom1”, select Widget from the popup menu and select “Fill Container”. This expands the custom widget placeholder to fill the frame. Here is what the GUI looks like on my system at this point…

That's it. Save the .tcl file (File | Save), and save it into a convenient folder calling the file “pandastabledemo”. Then, select the “Gen_Python” menu item and generate the GUI file and the Support module (these will have the same base filename as our .tcl file, but will be named pandastabledemo.py and pandastabledemo_support.py). You can now close Page and open the two Python files in your favorite IDE. You won't need to edit the GUI file (pandastabledemo.py). All our work will be done within the file pandastabledemo_support.py. The first thing we need to do is import the pandastable widget… from pandastable import Table

Notice that we are currently only importing the Table portion. That's ok, it's not limiting us. Next, let's finish the code for our callback function “on_btnExit()”. We'll just add one line to the bottom of that code (don’t forget to indent this line to match the rest of the function)… destroy_window() This will close our program correctly. Now scroll down to the bottom of the source file and find the line that says… Custom = tk.Frame # To be updated by user with name of custom widget. I usually copy this line and comment out the original, then paste the line and change it to what I need. In this case, we will use… Custom = Table

This creates a pointer to the pandastable import that we set a few lines ago. Now we'll edit the “init” function with our code. After the line “root = top”, put in the following code. csvfile = “BreadBasket.csv” pt = Table(w.frameCustom, showtoolbar=True, showstatusbar=True) # Show the table pt.show() # Import the CSV into the widget pt.importCSV(csvfile) # update the widget pt.update() That’s all the code changes we need to do. Save your file and we'll discuss what these lines do.

The first line, assigns the name of our CSV file to “BreadBasket.csv” (be sure you copy the csv file into your working directory, or provide a full path with it), which is the file we used earlier in this series. Next, we initialize the pandastable widget. We create an alias to it named “pt”. Then we tell it what its parent is (w.frameCustom), and that we want to show both the toolbar and statusbar. Next, we call the .show() method of the pandastable and import the csv file. Last but not least, we call the .update() function of the table widget. That's all there is. Everything else is contained within pandastable itself. There are a total of 8 lines (not including comments) that we have added to the code that Page has given us. Now run your program and you should see something like that shown below.

Even on my old and slow machine, the form pops up almost immediately, and the table is already loaded, all 21,293 rows. Now, let's see a few of the things we can do. You can resize the columns to suit your needs just like in any spreadsheet application. If you right-click within the table, you will get a context popup menu. Select “Table Info” and you will see something like what is shown in the image below. On the left side is the row headers. Right clicking on that will bring up a different context menu that allows you to sort by, reset, toggle and rename indexes as well as add, delete and duplicate rows and more. If you right-click a column header, it brings up yet another context menu that allows you to sort, set as index, delete columns, fill a column with data, add columns, and more.

The status bar along the bottom shows the number of rows and columns, allows you to zoom in and out, and expand or contract the columns. The toolbar on the right gives the ability to plot, aggregate, pivot, merge tables, and much more. Pretty much everything you need to analyse your data. I loaded another small csv file – to demonstrate the plot function. It's basically a list of the number of steps that a friend, who was recuperating from surgery, was able to do, by day. He had it as just a simple text file, but I converted it to a CSV just for this project. By selecting the Date and Steps columns, then clicking on the “plot” button on the toolbar, it will bring up the following window after a few seconds of thinking. From here, I simply selected the bar graph from the plot type dropdown, and grouped by date. You can see the resulting image.

I can’t tell Dr. Farrell how much I appreciate his kind permission to use his widget to show how easy it is to create a very powerful program using Page and his pandastable widget. It was completely painless. So, the purpose of this exercise is to show you that a couple of minutes in Page, 8 lines of code, and the pandastable widget, gives us pretty much everything that we need to be able to create a program that handles pandas data. As always, I have uploaded my code (the three Page files) to pastebin at https://pastebin.com/rEKWYBey, https://pastebin.com/2KUPXN7s, https://pastebin.com/prctf9bZ. Of course, there is one other option. You could simply follow the instructions on Dr. Farrell's page to install the Dataexplore app using snap, which is based on the pandastable widget (Dataexplore is MUCH more powerful than this simple demo, but what do you want for 8 lines of code?) and not have to do anything else. But what fun is that? Until next time, have fun playing with your new app and learning more about Pandas.

issue143/python.1554709563.txt.gz · Dernière modification : 2019/04/08 09:46 de d52fr