Hey, welcome back.
In yesterday's bonus example, we built this gooey.
This is just the interface of the file compressors.
So today we're going to actually implement all the functionalities of the program, which is that when
the user selects the files and they select the folder, when they press compress, we're going to compress
their files to create a zip file.
Let's do that.
So what we need to implement first is the world loop.
We need to read.
All the time.
We need to read the event and the values which are equal to that.
So let's see what we get with events and values.
Let's run the program.
So let's choose some files and see what happens when we do that.
So A and B?
Let's choose a folder.
Such as that.
Here.
Press compress and let's see what we get now and analyze what we can do further.
So when I press the button, we got the compress event.
So that's the name of this button.
So that means a compress button event happened to the button was pressed, and we also got this from
values.
So the variable values is equal to this dictionary.
This dictionary is made of this key and that value for that key.
And also this other key.
And that value for that other key.
So you see that the key choose which points to the first button.
This button here contains this list of files file paths.
So that's an absolute file path of the A, the text file, which I selected as a user, and also the
other file be the text.
We also have another pair of key and value.
So one is the key and that is the value that is the directory.
And then we have this choose zero.
So we had to choose and choose zero.
This points to the absolute directory, the destination folder, which I selected using that second
button.
So we have a dictionary of four pairs.
As you see, this valley here, which points to the zero key is the same with this valley here.
So we get zero from the input.
Widgets.
So the input widget has this value, but also the choose widget.
This button has the same value.
You can also remove this widget If you don't like, you can remove the input widget, but this is a
common practice to show the paths of the chosen files in a text box.
However, I wanted to tell you that even if you don't have this, things will work.
So now let's close this.
And now we know that we can get.
I'll let me close the program.
We can get the.
Five paths using values.
Choose.
So the key.
Of that pair of values is the dictionary, and that key will give us this list of.
File paths.
However, note that this is not actually a list.
This is just a string.
And this file paths are divided by this semicolons.
So that character divides, separates those file paths.
Therefore, in order to extract the actual file paths, you need to do something here.
So let's say that we have this string.
Right.
That's a string.
Let me remove this.
No aid that split with semicolon there.
So the split method is going to return a list.
So it splits that string at the semicolon.
And so we have this list of separate strings.
We have two items in this list, the first file path and the second file path.
So now we have a structured.
Data object, which is a list, and we can do further operations using that.
So A in our codes is file paths so we can apply the split.
A semicolon.
Right.
And that will give us a list of file paths.
Now let's also get the folder path, which is equal to values.
This would be choose zero.
So that should give us.
So this key should give us the string pointing to the destination folder.
We can improve this because I think this is not so readable.
The code is not so readable.
So we see that values choose doesn't make much sense.
So I think we should be using keys here.
So we go to the Choose button and here we add a key argument which is equal to.
Files, for example.
And for folder we could say key equal to Fulda.
So we're working on the folder.
Browse and files.
Browse.
Objects.
And then we can write, see your files and folder, because you'll see that if I run this again and
I select.
Two files I select.
A folder, whatever folder it is, I press compress.
Now you see that the key changed from choose to files.
So that's better because the folder key also changed from zero to folder.
So this is more readable and clear.
It's better to use keys rather than letting Python.
Use the name of the widget, the label of the widget as a key implicitly, so it's better to be explicit.
Let's terminate this.
What's next?
Well, now that we have the list of five paths and we have the destination folder, now it's time to
actually create a zip file out of those file paths.
So let's do that.
And it makes sense to create a separate file.
So a python file, let's call this zip creator.
So zip created that p y.
And you do remember that we used shuttle the library, which was used to create a zip file.
We did that a couple of days ago in the standard libraries.
However, shuttle is not as powerful as the zip file standard library.
So for this lecture we're going to use zip file.
Zip file gives you more flexibility to work with zip files.
For example, shuttle is limited.
It asks you to specify the name of a file and then the name of the compress the compressed file.
But it doesn't allow you to select any folder where you put your compressed files.
So zip file is much more flexible, and that's what we're going to use.
So let's create a function, make archive, and this function is going to be called by this front end
interface.
So the function will get a file paths, arguments and a destination directory argument as well.
So it's going to iterate over these files and create a zip file for each of them.
So the function is going to iterate over these file paths and it's going to put them inside this destination
folder.
The way zip file works is this you say with zip file, dot, zip file.
You see, this is the standard library name.
And this zip file is with Capital Z and Capital F, So that's a type.
Now this creates a zip file type object.
Just like we have file types.
We also have zip file types.
The open function creates those generic files.
But now this zip file type creates this more specific zip file types.
This expects the destination directory as inputs and the modes in this case its right mode.
We are creating a zip file.
If you wanted to extract an already an existing zip file, if you want to extract its files, then you'd
want to write read here.
But now we want to create a new zip file.
Let's create a variable as archive or as file, whatever you prefer.
And then we iterate for file path and file paths.
And then we point to archive, which is that variable which represents the zip file object instance
dot.
Right.
So that type of object has a right method which explains the file path.
So each file path of the file paths list.
That's about it.
Let's before we connect it to our interface, we want to test the function.
So I'm going to create this.
If name is equal to Main, I've explained to you what this does previously, in the course, actually
it's equal, not an assignment operator.
So this basically says that if this script is executed directly as the main script, then we want to
call the function.
Otherwise, if this file is imported from another file, then this will not be executed.
This line will not be executed.
So make archive.
Five paths.
Let's.
Typing the list manually.
So I'm going to.
Create that file path here, or you can just provide some local files.
For example, this zip creator file is in the same directory with bonus one, bonus to etc. So I'm just
going to provide some python files here.
Bonus one dot pie.
I can just write the name again because zip creator is in the same directory with those files.
Bonus one as a string comma, another set of quotes.
Bonus 2.1. p 2.1.
That's p y.
Like that.
And.
The destination directory is going to be.
A folder inside bonus.
So this can also be a relative.
So this folder is going to be in the same directory with zip creator.
Let's call this dest.
So dest and zip creator are inside.
Bonus.
Therefore I can just write here dest.
The name of that folder and to run this file.
Now let's take a look at the error we got on.
This is saying that it's a directory error, which means dest is a directory.
This one here.
And that's true.
So this this argument here should be the path to the zip file that will be created, not the path to
the directory.
So we need to do something here.
We need to see this deer Plus.
Backslash.
Plus the name of the file, which is compressed.
That's zip.
Right.
But another a better way to create a path is to use the Python path Leap Standard library.
So you don't need to install this library.
It's already available in your Python installation.
So the way it works is this you can create.
A new file path.
Lets say a is equal to path lib dot path.
If this was dust.
And then you say compressed that zip.
What a is going to be is this path.
So this path type is designed to get several arguments and merge them to create a complete path as you
see it.
Added this slash in other operating systems, this slash could be a backslash.
So this is intelligent, this type is intelligent.
It knows how to concatenate different parts of a path to make a path which works on that operating system
where the code is running.
So therefore, we need to aim to construct a destination path which is equal to path, live that path.
So we need to import path, live here in port and then we use it here and the path will be dust, dirt
which should represent this dust coma.
Let's give a name such as Compress that zip.
And then in here you can provide the best path.
Just like that and run again.
Process finished.
So we should see now the compressed file.
If I go to dust.
Yeah, that's the file.
If I open that.
Own my file system, I double click it.
And those are the files like compressed.
So that is a zip file.
And that's how the function works.
So this was a good demonstration, I think, to show you the way you create back end functions.
So you create the function and then you can test it using this.
BLOCK here.
So you wrap it up inside this if name equal to main object.
Now, you know that this is working so it gets a list as input for the file paths and then it gets a
string as input for this.
So we go to bonus 16.
And first we import from the Creator.
Or you can say import zip creator and then point to zip creator.
But this is also fine since the file contains one function only, we can just import that one function.
So it was make archive, make archive.
And then now we have file paths, we have the folder and we say make archive.
File paths and folder rights.
File paths is the first argument.
Yes.
So that variable is going to go in here and it's going to replace that one and it's going to be used
as a value.
So that's the actual list and the folder is going to be used as the destination directory.
And now we're ready to test the app.
So let's run it first.
I'm going to see to make sure I have some empty folders here.
So my files, this is an empty folder and in downloads I have these files I want to compress.
So run.
Bonus 16 Select files to compress A, B and C press enter Select Destination folder.
That's an empty folder.
Press on.
Choose Press and Compress.
Now let's go to.
The destination folder which was in here so compressed that zip was generated successfully.
And now we see a problem here.
So the entire file path has been included.
In the zip file.
You see that the full file, the absolute file paths of this files was something like users as downloads,
etc. So we don't need these subfolders.
Therefore we need to fix this issue to fix it.
All you need to do is go to archive.
That's right in your zip creator file and provide an arc name.
This should be a string now.
And it's going to be filed path that name.
What is file path that name?
Well, file path is a path in that file paths list.
Right.
So if I show you the console, we had this.
Path to leap object's
now filed path is currently just a list here, right.
For example, is the path to this bonus one dot pi.
Or it could be an absolute path.
But just like we did for this deer, we also need to convert each file path to a path.
Special object.
So we override that variable using this file path.
So basically what we're doing is this, yeah, we have this file path here and then we say file path
is equal to.
Painfully the path five path.
That's what we do.
So what we get now is this specific instance.
So before we had just a string, a plain string.
Now this is a more specialized type of object, which is better designed to work with paths.
So POSIX path bonus one dot p one and file path.
That name extracts only the name of that path because you know if the path was
like longer for example users.
In my case it was.
As downloads.
As downloads a dot txt.
So if the file path was an absolute path, no file path, that name gives only the file name.
So that's exactly what we're doing here.
We provide file path at name, so we extract only the file name from that file path.
And actually we actually need to do that under the loop.
So as soon as we iterate, we do that.
And we extract the name from that.
Stop the program and let's go to bonus 16 and round again the gooey so choose.
And downloads these three files, then choose the destination folder.
And here my files choose compress.
Now I go to that destination directory.
And that's the compressed zip file which has replaced the previous file, which was here already.
So if I compress it, you see that now all of these files are just inside this compressed folder instead
of being inside a whole path of subdirectories.
And just before we close the video, we want to add to one more feature.
We want to add here a message.
Compression successful, telling the user that the compression was successful so that the user knows
that something happened and they can check the files on their system.
Otherwise, without messages, it can be confusing for users.
So let's add that label in here.
So we're talking about adding the label next to the compress button and we're talking about a label.
So that would be an SG dot text.
For instance, the instance is going to be empty for now.
And then we're going to update that with value.
So you can either just place an empty string like that or just leave it empty like this.
Either way, it would work.
So we store that into a variable and then next to the compressed button in the same list, we add that
output.
Let's call it output label to be more clear.
So output label here, output label there.
And then once we make the compression using the make archive function, then we want to update the widget
of the window, which has a key.
So this widget, it doesn't have a key yet.
So let's add a key key, let's say output.
So our new label widget has this output key.
Therefore we access that widget using this expression.
So this thing here now is the actual label widget.
It's empty for now, but once this executed, then this will also be executed.
Updating that widget so that update the value is going to be.
Compression completed.
So let's run the code.
It's like the fast compress.
These are the input files.
Select the folder.
This is the output folder.
The destination directory could press.
And we get this message compression completed.
If you want a different colour for this, then you will have to add.
A text color argument to the text.
For instance.
So you can say green.
And if you run it now, your text will have a different color.
And that marks the end of this video for today.
Thanks a lot for following.
See you later.