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