Adding a CMS to Your Flask Web App Transcripts
Chapter: Advanced markdown
Lecture: Custom storage engine

Login or purchase this course to watch this video and the rest of the course contents.
0:00 we saw that marked on September lit, threw up its hands and said, I have no idea how to find this URLs content that you're asking for.
0:08 So let's do the final step. Remember, we talked a little bit about this before. We have to implement a class and link it together,
0:14 and it turns out to be super easy, but it is required. This step is done because that's our page class. I went off to do anything there,
0:22 but for this one, we're going to need to provide some sub templates. Storage engine okay, turns out to be easy,
0:30 but required. All right, so let's go over here and make a new file called Template Storage Engine and just don't put down here and put this class,
0:41 and I'll just call it sub template. Just call it template storage engine like that. This we can have PyCharm import above,
0:48 and we go over here and have PyCharm. Do most of the work that we have to look up or are seeing the documentation So if we go over here to say,
0:56 implement abstract methods, select them all, and that's it. We just got to write a little tiny bit of code for each one of these. First of all,
1:05 for this last one, there's not really anything we can do. In this case. This is more for the file version that's used some of the
1:11 time, but not here. This one Super easily it needs to know is the underlying thing that I'm going to talk to.
1:18 Would it work? If I were to ask the database, give me this thing or asked this thing?
1:23 You give me the markdown. If we haven't initialized the secret alchemy stuff, this won't work. So we look over here,
1:31 that's under this DB session thing, and you notice it will do things like we try to create session and it's not set up, it's gonna crash.
1:41 And the way we record that as we set, this is initialized. All we got to do down here. It's a return D B session thought is initialized,
1:51 is it? Those two are done. This one's pretty easy as well. We come over here and say We're looking for a page from the CMS Service like
2:01 that, and it knows how to get a page based on a euro and template Path is the URL and that's it. We just need to return the page of we have one.
2:11 One caveat, though. Notice here, returning a string and this is a page. So what we need to do is return paged, aunt content. That's that. However,
2:20 there might not be a page that they could ask for something that doesn't exist, in which case this line will crash. We got to do a little test,
2:25 if not page return. None. And technically, this really should be optional.
2:33 There's a open issue to change the specified return type in the abstract class. There to be optional indicate, You know, if you go to the database,
2:42 there's no guarantee you're going to get something back asking for it now. We also have this shared one, and at the moment it's exactly the same.
2:50 But I'm gonna copy this over important and basically zero again have a different mood. If a page is shared versus not shared,
2:59 in which case that we can, you know, pass that over like is shared, shared equals true, something like that.
3:06 But for now, we're just gonna have the same code until we get that in place. Okay, let's see if this works.
3:12 Oh, that's step one. There's one more thing we gotta do before this is gonna work. Let's go over here a little anxious to show you how cool it
3:19 is. We go over here to our main. Remember this. We're doing things like setting up the D B. And now we need to say,
3:28 set up templates or mark down or let's call it marked down. Then we go in past log in. All right, so let's go and put this down here in a look.
3:40 Basically the same. So what we're gonna dio is very, very simple. We're gonna create, some like this. I'll call it a store,
3:52 and all we gotta do is create one of those template stores TV store like that and then we're going to go to the storage,
4:02 which comes from they're gonna say, set, engine are set storage store like that.
4:10 And I passed the logging so we can say something to the effect of notice. Something like that. Just so when we started up,
4:17 we can see what we said it to go. Set it to a template storage engine as opposed to your files or whatever. All right now. Moment of truth.
4:26 Let's go back year where we had our error. Now the storage engine is initialized. Let's see if this works. Run it.
4:34 Yes. How awesome is that of a click around? You can see all of our pieces are just nice and quick so we can see this is working super Super Well now.
4:47 Before we had this problem where if there was a lot of content on this page it was slow. Right now,
4:53 it's super fast. You can see that's not so impressive because what is fast me Let's go over here and let's tell it that if you ask for this page
5:04 you're actually going to get 20 times that. That's what we did before. Right now,
5:12 when I first click this, you'll see that this is gonna take a second for the Ceron. You'll see the little spinning up there going okay,
5:20 and now this should be much longer if I click it again. Oh, it's that you don't even see this page.
5:26 Refresh. Let's go and do a quick inspect element here. Go to the network. Good HTML,
5:35 and I'm going to restart this because it's already done its magic I want you to see it from scratch. So when we first request this,
5:41 it has to generate the markdown right click here. Notice it took seven almost 706 159 milliseconds to do that.
5:49 And you can look in the logs and it says mark down. Sub template has an info message for you. It is generating contents from help our from this,
5:59 like slash help with new data. Right. So it has different caches for what you passed It then took 635 milliseconds But if I refresh it,
6:08 if I go over here first time, it's a little bit slower. Canasta. Read the file, but seeing 13 milliseconds. If I goto log in for go back.
6:19 Good one of these. Hit it a few times. Very quickly. 12 milliseconds. Remember, we don't want the 800 milliseconds or 600 milliseconds.
6:28 We want the 12 or less. Check this out. Seven seven strife. You more 54 Can we keep going down that the awesome six notices is insanely vast
6:41 This thing that was a huge blight on our site. It was like really slow. And it would like walk up the server.
6:48 While requests are being made to it has now become the fastest or one of the fastest pages on our site. It is fastest log in,
6:55 which does nothing. And it has all this content, right? All this stuff here. So this is super awesome. And that's just one of the things.
7:02 One of the cool things that this mark down sub template takes care of forests at the moment. It does that by cashing it in memory in process,
7:10 and that may be totally fine. You have to do anything about it. You can see as we request,
7:14 over and over. There is no more generating content once it's done once it's done But you might want to store it in other places.
7:20 You can look at the X sensibility, and you can actually store that in the database.
7:24 That's what we do over talked by phone training for our sites that use this infrastructure And you could put it in readiness.
7:31 Things like that, right? So there's a lot of options for away restore that That way, if you restart the site, you don't to regenerate the content,
7:36 which may or may not be a big deal but nonetheless, it's cached somewhere and we don't have to worry about it. It takes care of it for us,
7:43 which makes it insanely fast. How cool is that?


Talk Python's Mastodon Michael Kennedy's Mastodon