Effective PyCharm (2021 edition) Transcripts
Chapter: Server-side Python web apps
Lecture: Template tooling
0:00 Let's play around a little bit more with this template auto complete and template tooling in
0:05 PyCharm. I'd like to have a little bit more interesting data than a random
0:09 number. So I went over here to my twitter and I just saw well,
0:14 Anthony shaw said, but what if I am the leader of an underground organization and
0:18 Nick Mosh said he needs to get analysis,
0:20 paralysis and Brian Okkins said some things and so on.
0:24 So I took that data that very limited data and threw it into this JSON
0:30 file. So what I want to do is take this and put it into our
0:33 web page. So over here let's start by saying going to have another we div
0:39 here and I wanted to just have something like your recent tweets.
0:46 Not as in my tweets I've created but things I've seen.
0:49 So we come over here and we'll just a little a tab we slash tweets and
0:58 put this. And so if we run this we now have your recent tweets with
1:02 no styling but there it is nothing found.
1:06 So what we need to do is have something and show up for this.
1:09 Right, let's go over here and we'll have something real similar.
1:13 So we'll just 'cmd+d' that highlighted 'cmd+d' and say tweets all this tweets or
1:20 whatever you want again when you come over here and this is going to be tweets
1:28 right? This will be Yeah,
1:42 so there's some structure we're going to load up this page and somehow we need to
1:45 get that data from Json into something structured and then passed over here.
1:51 All right. So we don't need to do this.
1:53 We need to say tweets equals Well,
1:56 notice I've created this file here.
1:59 I'll call this recent tweets, create this file here and it's totally empty.
2:03 But what I want to do is create a class that we can then load up
2:07 a bunch of instances of those that matches something like this as a name and a
2:12 text. So this will let us see something really cool here.
2:14 Actually call this a tweet now.
2:17 Watch this. So if I say def type underscore you can see all the Built
2:23 in Special Magic Methods of Python and I Want This one.
2:26 And what does it have? Well,
2:28 it has a name which is string and it has text which is a string.
2:33 Notice those are gray, something super cool in classes we can go over here and
2:37 hit 'alt+enter' and it says, you know what that looks like a field that you're
2:41 passing into a constructor. Maybe you want to hang on to it?
2:44 Yes, please. Let's do that.
2:46 Same for this one and that cool.
2:49 Here we go. And let's also give this a static method here.
2:54 Maybe a class method and it will be load given a file.
2:59 Well, some sort of file.
3:01 Well, just we don't need to pass any files.
3:04 We'll just assume that that's the file name and it's going to return a list of
3:10 itself here when you're in a class.
3:13 You can't use the name so we can put this and the quotes and PyCharm will
3:19 still know what it means. All right.
3:21 So this is going to be super easy.
3:23 We're gonna say with open the file is going to be remember where we're running from
3:29 this directory. So some data slash tweet stream.
3:36 Read it, coding is UTF-8
3:42 Mhm. That's fine. And then we're going to get the data is going to
3:46 be the JSON gonna import that that load.
3:51 Then now we need to turn this remember what is this?
3:56 This is a list of dictionaries or the keyword arguments match the argument there.
4:04 So we can do something really cool here we can check this out.
4:06 We can return a list comprehension of creating a tweet where we take the data for
4:15 'd in_data' that each individual tweet or maybe t t better.
4:23 No it doesn't quite work. Remember.
4:24 This takes two things. But if we do a star star that's going to take
4:29 that and make it a keyword argument of that value that make that a keyword argument
4:33 with that value which is exactly what we need.
4:36 So a little cleanup on the spacing and actually this should be good to go let's
4:43 go back here. We've got our recent tweets and this will be tweet and we
4:47 have our tweet class. I need to import our tweets.
4:57 Yeah. All right. We got our class here?
5:01 So we just come down to our class and say load and should return all these
5:05 recent tweets And now we can just say the recent tweets are this We need to
5:13 change this. So now we have some options.
5:16 We can't control space and look we can choose we want this one and over here
5:21 for just a moment. Let's just put out recent tweets.
5:24 Look at the auto complete. So cool.
5:26 All right. Run it again.
5:28 So they're getting a four or four this time.
5:30 Let's see what we get Data.
5:32 Tweet data, tweet data tweet data tweet so Cool.
5:36 All right. Well that's not very useful.
5:37 But it's a good start. We've loaded up this data and granted it's really simple
5:42 but it gives you this sense of like a multi tier system.
5:45 Right? We've got our data it would be like the database data access layer that's
5:50 like this load and here's our or a model type thing,
5:53 pull that back to the server side and then we pass it over to our template
5:58 Our template renders it as some kind of html gives it back to the browser, cool!!.