#100DaysOfWeb in Python Transcripts
Chapter: Day 52: Anvil - Full Stack Web Apps
Lecture: Server-side code
0:00 We have our data and we know we can get to it from the server modules, but not the forms cause we want to make sure that's safe.
0:07 How do we write a server module? Let's go and add one. So it's called server_module1. Not a super name. Let's give it something better.
0:15 Let's call it data_layer or something like that. Now check this out over here. It says okay we are going to import some server and data stuff.
0:23 And all you really have to do is just create any function that is callable, has parameters, and return something and we can call it.
0:31 So for example to say hello, how would we get to that? Let's go to our home form really quick. And let's just in this lib we'll say print.
0:39 anvil.server.call Now look what just happened here. It takes a string, the server name, but Anvils integrated to know what server
0:50 methods are available and is helping us right here. So say, say hello. And then I could put my name, which if you look back here it accepts a name.
1:00 It should say, "Hello there." So that'll be the input in return 42 as it should. So let's run that. See some output. Server is yellow.
1:12 Client is clear or white. It says, "Hello Michael 42." How awesome is that? Alright that is an incredibly simple way to have a service.
1:20 Put that on there, done. Then already host a service, host a client, connects them, done. So we don't want this.
1:25 We want code that talks to our database. We put that here and I'll talk you through it. So we're going to come over here and we're going to talk to our
1:34 app tables and we say dot. You see we have our 2 databases that we created. Our 2 data tables and the data table service
1:41 we created, and we can go to them and we can say, "Search" and we can even do an order by. And I'm going to convert that to a list
1:48 and then we'll turn it back. Over here I'm going to do something similar for categories. And categories we're doing by name so it's alphabetical.
1:55 Also can find individual documents by name. Instead of doing a search, you can do a get. So here we're doing a where the name is equal to what we pass.
2:03 So we're going to leave 4 categories. Okay so these 4 functions are now available to our code. Let's try to add them into our add_doc_form.
2:11 So up here, we got our categories. Let's go over here and say, "Categories", call them all_categories. We're going to go anvil.server.call Now look.
2:23 All categories takes no parameters. Now these are going to return rows which are dictionaries which have things and so what I really want is categories
2:33 is going to be c, it's going to be one of the rows. And it's going to be named, remember we had that in there, for c in raw_cats.
2:42 So now we have Docs, Science, News, and Social, but remember we put like press releases and stuff. Let's see what we get now. Oh, whoops,
2:51 I got to take that out, don't I? It's really cool how you can click that to get back. Alright that was just test run, forgot about that.
2:57 Let's go to add a document, and look at that. That's now out of our database. How slick is this? Okay super, super cool. One of the challenges there,
3:07 this is going to keep reloading it if I do this, and do this. Hitting the database again. Turns out this is probably not going to
3:13 change that often, so we'll be able to do something slightly better, but this is really really cool. Add the name. Add some stuff here. Hit create.
3:23 And then we should be able to call one more function on that server.