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