#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Refactoring our web code for single responsibility

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You may be just fine having your entire website crammed into a single file, I'm not. It really drives me crazy to have everything jammed in here.
0:09 And this is a super simple web app, I got to tell you, this was in the training website, for example,
0:14 this would be many thousands of lines of code here. And it's just not great. We can do so much better. So, let's create two files here,
0:26 one that holds just the API stuff, and one that holds just the web view, and then we're going to leave the sort of start up stuff in app.
0:33 Now, you don't have to do this. If you don't want to do this, it's fine. But I'll show you how to do it. So, I'm going to go over here and
0:37 create a directory called views and something called a game_api and also be home views, just called home.
0:55 So what we're going to do, try and come up here to the top, and we're going to say, from views import game_api and home
1:04 And what we need to do, is we need to basically provide it this object, and then use that object to define methods like this.
1:13 It's going to look a little bit funky. It's probably not obvious, but this is what I like to do. So, I'm going to say game_api.build_views
1:24 Now, notice this does not exist, PyCharm says "Cannot find a Reference", but if I hit Alt Enter, it will create that function, okay?
1:32 And then when I'm going to do is I'm going to go down here to all skip, skip, skip here, from this bit down.
1:46 And just cut those and put them right here, indent it. Indent it. So when we call the function the buildviews function we do these things.
1:55 I'm probably going to go through and make sure some of this stuff is imported, like import flask.
2:22 Whew, okay. So I had to go through and import all the stuff, or rather, let PyCharm import the stuff that is needed here,
2:30 but let's go back to our app. Do a little cleanup, and notice this is looking better. We don't need this. Actually, we need that one
2:39 in just a second. But these things we don't need, this is looking a little cleaner. So let's do exactly the same thing, but for home.
2:50 And what's going in there, well, let's give it this index Apytest travel delete. And this. Now again, we got to import flask at the top.
3:06 We have our index and we have our not found. Whew, now if you look at this page, if you look at this app file, what does it do?
3:14 It'll just start update it runs the app. Um, we should probably make this method like so like build_views. Because again, this is simple now,
3:30 later this is going to be not so simple. Is it still running? Let's see. It is. Does it still work? Um, play around is not going to work.
3:43 But we can get Tom's scores now. It works, awesome. And let's see then, 4, 4. This works, and if we just go here,
3:53 test our other API stuff, hello world. Awesome. So we've cleaned up our code, in my opinion, dramatically. You know exactly where to go
4:00 to work with the main home page webviews. Here's the API just for the game. Here's your startup code, how you config your app.
4:08 To me, this just is like, ah, so much better, so much cleaner. If it's not that way for you, then, you know, put it some other way. Right?
4:16 But this is a pattern that I like to use.

Talk Python's Mastodon Michael Kennedy's Mastodon