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