issue143:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue143:python [2019/03/31 15:28] – créée auntiee | issue143:python [2019/04/11 13:58] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | A GUI for Pandas | + | **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. | 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. | ||
Ligne 5: | Ligne 5: | ||
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:// | 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:// | ||
- | Farrell, D 2016 DataExplore: | + | Farrell, D 2016 DataExplore: |
- | 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... | + | Une interface graphique pour Pandas |
+ | |||
+ | Nous continuerons notre série sur l' | ||
+ | |||
+ | 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 une interface graphique. Vous pouvez récupérer tout le code source de pandastable à https:// | ||
+ | |||
+ | Farrell, D 2016 DataExplore: | ||
+ | |||
+ | **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 | pip3 install pandastable | ||
Ligne 13: | Ligne 21: | ||
(If, when you try to run the below program, you get an error message saying something about “from pandas.tools import plotting - ImportError: | (If, when you try to run the below program, you get an error message saying something about “from pandas.tools import plotting - ImportError: | ||
- | I will be using the latest version of Page (4.21) that was released on March 1, 2019 and can be downloaded from https:// | + | I will be using the latest version of Page (4.21) that was released on March 1, 2019 and can be downloaded from https:// |
+ | |||
+ | Maintenant, commençons. Pour utiliser le gadget pandastable, | ||
+ | |||
+ | pip3 install pandastable | ||
+ | |||
+ | (Si, quand vous essayez de lancer le programme ci-dessous, vous recevez un message d' | ||
+ | |||
+ | J' | ||
- | 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 " | + | **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 " |
We will add two frames, one " | We will add two frames, one " | ||
Ligne 21: | Ligne 37: | ||
The first frame will be called " | The first frame will be called " | ||
- | The second frame will be called " | + | The second frame will be called " |
- | Next, place a standard Tk button within the toolbar frame. Set the Alias to " | + | Maintenant que nous avons pandastable et Page, nous pouvons commencer la conception et la forme [[https:// |
+ | |||
+ | Nous ajouterons deux cadres, un bouton Tk « standard » et un gadget Page personnalisé. C'est tout ce dont nous avons besoin. | ||
+ | |||
+ | Le premier cadre sera appelé « frameToolbar » (widget alias - alias de gadget) et devrait être placé tout en haut de la forme. J'ai utilisé X=2 et Y=2, une hauteur de 40 et une largeur de 1000. | ||
+ | |||
+ | Le deuxième cadre sera appelé « frameCustom » et contiendra notre gadget personnalisé (pandastable). Je l'ai placé quelques pixels sous le cadre de la barre d' | ||
+ | |||
+ | **Next, place a standard Tk button within the toolbar frame. Set the Alias to " | ||
Finally, scroll down to near the bottom of the Widget Toolbar and select " | Finally, scroll down to near the bottom of the Widget Toolbar and select " | ||
- | Here is what the GUI looks like on my system at this point... | + | Here is what the GUI looks like on my system at this point...** |
+ | |||
+ | Ensuite, placez un bouton Tk standard dans le cadre de la barre d' | ||
+ | |||
+ | Enfin, descendez vers le bas de la barre d' | ||
+ | |||
+ | Voici à quoi ressemble le GUI sur mon système actuellement : | ||
- | That's it. Save the .tcl file (File | Save), and save it into a convenient folder calling the file " | + | **That's it. Save the .tcl file (File | Save), and save it into a convenient folder calling the file " |
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. | 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... | The first thing we need to do is import the pandastable widget... | ||
+ | |||
+ | from pandastable import Table** | ||
+ | |||
+ | C'est tout. Sauvegardez le fichier .tcl (File | Save - Fichier | Enregistrer) dans un répertoire convenable en appelant le fichier « pandastabledemo ». Ensuite, sélectionnez la ligne de menu « Gen_Python » (générer Python) et générez le fichier du GUI et le module de Support (ils auront le même nom de base que notre fichier .tcl, mais ils seront nommés pandastabledemo.py and pandastabledemo_support.py). | ||
+ | |||
+ | Maintenant, vous pouvez fermer Page et ouvrir les deux fichiers Python dans votre environnement de développement (IDE) préféré. Vous n' | ||
+ | |||
+ | La première chose que nous avons à faire est d' | ||
from pandastable import Table | 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 " | + | **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 " |
destroy_window() | destroy_window() | ||
Ligne 46: | Ligne 84: | ||
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... | 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** | ||
+ | |||
+ | Notez que nous n' | ||
+ | |||
+ | destroy_window() | ||
+ | |||
+ | Ceci fermera notre programme proprement. Maintenant descendez en bas du fichier source et trouvez la ligne qui dit : | ||
+ | |||
+ | Custom = tk.Frame | ||
+ | |||
+ | Habituellement, | ||
Custom = Table | Custom = Table | ||
- | This creates a pointer to the pandastable import that we set a few lines ago. Now we'll edit the " | + | **This creates a pointer to the pandastable import that we set a few lines ago. Now we'll edit the " |
csvfile = " | csvfile = " | ||
Ligne 65: | Ligne 115: | ||
pt.update() | pt.update() | ||
- | That’s all the code changes we need to do. Save your file and we'll discuss what these lines do. | + | 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 " | + | Ceci crée un pointeur vers l' |
+ | |||
+ | csvfile = " | ||
+ | |||
+ | pt = Table(w.frameCustom, | ||
+ | |||
+ | # Show the table (montrer la table) | ||
+ | pt.show() | ||
+ | |||
+ | # Import the CSV into the widget (importer le CSV dans le gadget) | ||
+ | |||
+ | pt.importCSV(csvfile) | ||
+ | |||
+ | # update the widget (mettre à jour le gadget) | ||
+ | pt.update() | ||
+ | |||
+ | Ce sont les seuls changements de code dont nous avons besoin. Enregistrez le fichier et nous présenterons ce que font ces lignes. | ||
+ | |||
+ | **The first line, assigns the name of our CSV file to " | ||
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. | 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. | ||
Ligne 73: | Ligne 141: | ||
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. | 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. | + | 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, | + | La première ligne assigne le nom de notre fichier CSV à « BreadBasket.csv » (assurez-vous de copier votre fichier csv dans votre répertoire de travail, ou ajoutez-lui le chemin complet), qui est le fichier que nous avons utilisé précédemment dans cette série. Ensuite, nous initialisons le gadget pandastable. Nous lui créons un alias nommé « pt ». Ensuite, nous lui disons que son parent est (w.frameCustom) et que nous voulons montrer à la fois la barre d' |
+ | |||
+ | Ensuite, nous appelons la méthode .show() de pandastable et importons le fichier csv. Enfin et surtout, nous appelons la fonction .update() du gadget de la table. | ||
+ | |||
+ | C'est tout. Tout le reste est contenu dans pandastable lui-meme. Au total, ce sont 8 lignes (sans compter les commentaires) que nous avons ajoutées au code que Page nous avait fourni. | ||
+ | |||
+ | Maintenant, lançez votre programme et vous devriez voir quelque chose comme ce qui est présenté ci-dessous. | ||
+ | |||
+ | **Even on my old and slow machine, the form pops up almost immediately, | ||
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. | 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. | ||
Ligne 81: | Ligne 157: | ||
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. | 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. | + | 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.** |
+ | |||
+ | Même sur ma vieille et lente machine, la forme s' | ||
+ | |||
+ | Nous pouvons redimmensionner les colonnes pour qu' | ||
+ | |||
+ | Sur le côté gauche se trouvent les entêtes de lignes. Un clic droit dessus affichera un autre menu contextuel qui vous permet de trier, de remettre à zéro et d' | ||
- | 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 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. | 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. | ||
Ligne 89: | Ligne 171: | ||
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 " | 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 " | ||
- | From here, I simply selected the bar graph from the plot type dropdown, and grouped by date. You can see the resulting image. | + | 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. | + | En bas, la barre de statut montre le nombre de lignes et de colonnes, vous permet d' |
+ | |||
+ | À droite, la barre d' | ||
+ | |||
+ | J'ai chargé un autre petit fichier csv, pour montrer la fonction de traçage. C'est en gros une liste des pas qu'un ami, qui se remettait d'une opération était capable de faire, par jour. Il l'a fait comme un simple fichier texte, mais je l'ai converti en CSV juste pour ce projet. En sélectionnant les colonnes Date et Steps (pas), puis en cliquant sur le bouton « plot » (tracer) de la barre d' | ||
+ | |||
+ | À partir de là, j'ai simplement sélectionné le graphique à barres dans la liste déroulante des types de graphique, et regroupé par date. Vous pouvez voir l' | ||
+ | |||
+ | **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:// | 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:// | ||
Ligne 97: | Ligne 187: | ||
Of course, there is one other option. You could simply follow the instructions on Dr. Farrell' | Of course, there is one other option. You could simply follow the instructions on Dr. Farrell' | ||
- | Until next time, have fun playing with your new app and learning more about Pandas. | + | Until next time, have fun playing with your new app and learning more about Pandas.** |
+ | |||
+ | Je ne peux pas dire au Dr Farrell combien j' | ||
+ | |||
+ | Ainsi, le but de cet exercice est de vous montrer qu'en quelques minutes dans Page, avec 8 lignes de code et le gadget pandastable, | ||
+ | |||
+ | Bien sûr, il existe une autre option. Vous pouvez simplement suivre les indications sur la page du Dr Farrell pour installer avec snap l' | ||
+ | |||
+ | Jusqu' | ||
issue143/python.1554038906.txt.gz · Dernière modification : 2019/03/31 15:28 de auntiee