Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 6: LOLCat Factory
Lecture: Showing LOLCats on OS X
0:00 So our cat app is basically working, the last thing we need to do is display the cats to the users. Now we were looking to them over here in PyCharm
0:10 but obviously the users won't be running this in pyCharm and we do kind of say hey we downloaded your cats
0:15 but let's show them to them. Now we have to keep in mind we are working in a terminal app
0:20 so we kind of can't really show it like on a cool web page or in some kind of window, the best I think we can hope for is maybe showing
0:28 the finder window or the explorer window and letting them browse with the operating system file browser.
0:33 So the last thing we are going to do is write this display cats method, let's do that. And the only thing we are going to need to know is
0:40 what folder do we have so let's go write this at the bottom, and we'll define this method we'll say pass just for a second,
0:47 now let's think how do we open the finder window so it turns out that you can just type open and give it a folder name
0:55 so you could like type open. in the terminal, in OS X that would launch the finder window in whatever window you are at.
1:02 Now on Windows, that doesn't work, you have to say start. and that would then launch the Windows Explorer to that location as well and in Ubuntu
1:13 neither of these work you actually do xdg-open and then the folder so something like . to open the current one. We can run these three programs
1:23 and let's go ahead and write it just for OS X first since that's what I am on, and then I'll show you how to actually determine
1:30 what platform we are on and choose the right commander run based on that. So the command we are going to use is open folder,
1:36 something like this, we want to call this open process, we want to launch another process from our application
1:43 and we do that with the sub process module, now like all of these modules we have to import them, PyCharm will do that for us thank you very much,
1:50 and then it has a call() method, and call takes you can see there is a few options here, call takes an array strings,
1:57 the first string is going to be the name of the program, the second one is going to be the argument that we are going to pass to open
2:04 so we just say open, folder and that should do it, let's test it out and maybe put a little print statement here
2:10 just to say displaying cats in OS window so let me come out of full screen so this doesn't look weird when it launches,
2:21 boom, there are some cats check that out, let me make them a little bigger for you, oh yeah,
2:26 those are good cats there has been a failure on the internet, of course there has right, so we can sort of flip through our cats like this,
2:32 I kind of like the dune cat, he is a good one. So we've successfully displayed our cats to the user, granted we have pretty simple mechanism for that
2:41 because we have a simple terminal app, but nonetheless, pretty cool, it shows you how to work with sub-processes, in an incredibly easy way.
2:48 Now the last thing to do is to say well, if I run this on Windows, or I run this on Linux, this is not going to work so well,
2:56 let's actually take it and run it on Linux and watch it crash and then we'll fix it over there.