Python for the .NET developer Transcripts
Chapter: Deploying Python web apps
Lecture: Configuring the Python code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now you can use whatever file structure you want
0:03 but here's something I like to use.
0:04 I'm going to create a /apps folder and allow the app
0:09 to write logs and stuff like that.
0:11 Create a couple of log directories like this.
0:15 Let's just go run all of those over there.
0:19 We have tree, yes, we have tree.
0:21 We have this simple structure so far.
0:23 The rest of its going to be created by git itself.
0:27 So this is like so if you want to run this block
0:29 we just did that.
0:30 Now, we're going to create a virtual environment
0:33 for our web app.
0:34 Yes, we could just mess up Python on the server, right?
0:37 But let's have a dedicated isolated place
0:40 for our Python code.
0:41 That way no matter what happens to the system, Python
0:43 we're pretty much guaranteed
0:45 to have everything running the way we want
0:47 So now we have that environment.
0:49 Let's go and activate it.
0:54 Remember, pip and setuptools are always installed
0:57 and they're always out of date.
0:58 Let's test. Yup, they were out of date.
1:01 We had version nine. Oh, guess what?
1:03 Version 19 is out, only 10 versions out of date.
1:07 I don't know what's wrong with that.
1:08 I really wish you would update itself.
1:10 Then I want to setuptool, httpie, which is a great
1:12 it's like curl or wget, but it's much better
1:15 for testing command line stuff.
1:17 This way let's check of what our web servers responding to
1:21 that way we can like check uWSGI on local hosts
1:24 without exposing it.
1:25 Then glances is like Task Manager, or Process Explorer
1:28 or something like that, that lets us look at our server
1:31 in a almost graphical way.
1:38 Right, a bunch of stuff got installed
1:40 and now we can do things like run glances
1:43 which will give us a really cool view.
1:44 And if we make it bigger here, it's big as I guess it can be
1:48 a little progress bar up here
1:50 and see the servers super efficient.
1:52 We've been working on it and it's been running for a while
1:54 and it's even running this Python app
1:56 and it still is only using 287 megs of memory
1:59 including the operating system.
2:01 So yeah, thanks. It's pretty awesome like that.
2:04 CPU levels super low and most of its
2:06 just coming from glances. That's cool.
2:09 Here's our failed band server.
2:11 Happens to be running on Python
2:12 our monitoring agent from Digital Ocean and so on.
2:16 We don't have Nginx or uWSGI
2:18 because we haven't set those up yet.
2:19 But you know, we're getting there.
2:21 Then for HTTP, we could do something like over here
2:26 and do a request and notice we get
2:27 color coded HTTP response.
2:30 Let me do that again and so I just go to the top.
2:35 You can get information about what the encoding is
2:38 you know, headers and server like servers and Nginx.
2:40 No surprise, because Nginx is awesome.
2:43 Alight, so this will let us test our setup, good.
2:48 We have our virtual environment, and we have our logs
2:50 but we don't have our code yet.
2:52 So we're going to get that from GitHub.
2:54 But before we do it, let's also install uWSGI.
2:57 Now, Nginx we did before but it is a system type
3:01 of package installed with aptitude with apt.
3:04 But uWSGI is actually a Python thing we install with pip.
3:07 So I'm going to install that into our virtual environment
3:09 that's why we waited until now for it.
3:12 Actually takes a second to build and install
3:14 so we'll let it do its thing.
3:18 Super, so we have uWSGI, we'll even be able to type
3:20 that is a command. It's not going to run.
3:24 We haven't given it an app and so on.
3:25 But it looks like that's up and running.
3:28 Next, let's try to install our GitHub repo.
3:32 I'm going to notice I'm going to shorten this
3:33 because this is a great long name, and that's annoying.
3:35 So we're going to put it into app repo.
3:39 Very first time I have to enter my information here.
3:45 Oh, it looks like I used the wrong name
3:47 it's ended the organization not under my personal account.
3:54 There we go. Now we do our tree command.
3:57 We can see that we have our app in our app repo.
4:01 If we ignore our virtual environment
4:03 we can look a little bit further.
4:07 Decent stuff about the structure.
4:08 Okay, so we have our .NET version
4:10 we have our Python version
4:12 there's the code that we're going to try to run
4:14 and here are the config files that are now copied
4:16 to the server that we get to work with. Perfect.
4:20 Now we're going to need to make sure our server
4:22 has the Python requirements.
4:24 So let's go over here we go and run, you know
4:29 and go to our Guitary and we can, I mean, no, it's not there
4:33 sorry, it's back one. There we go.
4:35 pip install-r requirements.
4:37 Now that's going to install a little bit extra
4:39 because it's for the whole course.
4:41 Normally, it wouldn't work like that
4:42 but whenever we're just going to let it run.
4:46 Super how we have a lot installed there.
4:48 We can check it out with Pip List, it's all looking good.
4:53 It's time to get to our config files, so we're going
4:54 to do those next, but what we're going to need to do
4:56 is basically copy the config files to the location
5:00 where uWSGI and Nginx can pick them up.
5:03 So it looks like our code is all set up.
5:05 I guess we could even try to run it
5:06 couldn't wait, let's let's do that.
5:10 It's pretty cool, We can even come down here
5:12 and arrow around.
5:15 Love, love, Oh my zshell.
5:16 All right, we come down here.
5:20 Try to run this.
5:23 You know this one, We didn't update it with that thing
5:26 that path addition.
5:29 I believe we just did it right here. Let's look.
5:31 Remember this? We didn't do it in this one.
5:34 Let's update this one as well.
5:41 Let's see if this will work.
5:42 I think we could do it like so if we go to the terminal.
5:47 Yep, looks like it's going to work.
5:49 Okay, so if we check that in
5:57 we come back over here
5:58 and try our source, we got to get a git pull
6:04 and then just run it.
6:07 Beautiful. Now I can't interact with it in this terminal
6:10 because it's already blocked running.
6:12 So let's just go back here
6:14 and do an HTTP HTTP localhost Port 5000.
6:20 That's where it's running right there.
6:25 That did not work because we did not activate
6:26 our virtual environment.
6:28 Now one option and be well, we need to activate it
6:30 go over there and do that.
6:31 This is something I love to join my servers.
6:33 So I'm going to go and edit my startup script here
6:40 and I'm going to do an alias
6:41 not do an alias, I'll do a source.
6:44 So what's that going to do?
6:48 That means when we log back in, it's going
6:50 to automatically activate our web portal environment.
6:54 This is so nice because you almost never want
6:56 to change the system Python, you want
6:58 to change your app Python.
7:00 So I do this in all my servers, it's really, really sweet.
7:01 Now, let's try this again HTTP host.
7:03 Oh, look at that jQuery
7:07 and our Bootstrap our Talk Python Training.
7:09 All the magic. Here's our Guitary.
7:11 So it looks like it's working.
7:13 You can even see the successful git.
7:15 So we've got our code running here, but we need
7:17 to get it running, not in just our dev server
7:20 but in uWSGI, production uWSGI server instead.
7:24 But we're making it pretty far it's running right?