Anvil: Web apps with nothing but Python Transcripts
Chapter: Databases: Storing and querying data
Lecture: Server modules for protected data access
0:00 Now that we have our table
0:01 and we've talked about security a little bit
0:03 let's go and actually save a record.
0:06 Now, it turns out this is basically done all
0:09 but for the database.
0:11 So remember, we've already set up our account
0:14 memory and all that kind of stuff.
0:15 We come over here, we can pick a date.
0:17 Like, let's pick today
0:19 let's say I'm 178 pounds today
0:22 and my resting heart rate is 72.
0:24 We say add measurement and that sort of worked
0:27 it says, well, we would have saved
0:29 what you typed in to the database had we had one.
0:33 Well now we do
0:34 and so our job is to go and implement that over here.
0:37 So remember, that is in the add measurement part.
0:42 So here's where we say we would've saved it
0:43 so we're going to write the code that actually does the saving.
0:47 Now we're going to do this in two parts.
0:48 We're going to do it directly
0:50 working with it in these components
0:52 which is probably not the best way
0:54 to factor that kind of stuff.
0:56 And then we're going to split it off
0:57 kind of like we did with navigation
0:58 into another client side module
1:01 we're going to call that data access.
1:03 That's going to be later
1:04 let's just work about getting this into our database.
1:08 Remember, if we look at the database
1:09 here we have forms, no access, server modules, read write.
1:13 What is a server module?
1:15 A server module is like a regular module
1:20 it runs literally on Anvil's back-end servers
1:23 over there on Linux.
1:24 So what we can do is we can add one of these
1:28 and let's give it a name.
1:33 So we're going to call that measurement_service
1:34 and it gives us a cool little tip here.
1:37 It says, hey, all you have to do is something like this.
1:40 If you have a method
1:41 it has arguments and it returns arguments
1:43 you can just say it's server callable.
1:46 Okay, so that's pretty easy.
1:47 We could actually go and get this to run
1:50 just so you all see what is up, let's go do that.
1:53 Let's go, just went in the home details loads.
1:58 We'll go over here and we'll say anvil.server.
2:02 When we say call, it takes the function name and the arguments.
2:05 Watch, this is pretty good integration here.
2:07 So notice, I just hit enter
2:09 and it is listing all the server callable methods
2:12 that is knows about.
2:13 Say hello, that's pretty cool.
2:16 And then it knows that we need to give it a name
2:18 so I'll put my name, Michael
2:19 and it's going to return.
2:21 42, so let's print this out.
2:23 Let's go ahead and just run this real quick
2:24 to see what's happening.
2:26 It's running, oh, something happened over here, let's see.
2:29 Now, check this out.
2:30 It says hello, Michael, and then it said 42.
2:34 But, more importantly, look at the colors.
2:36 The color here, white, means this happened
2:41 in the client side Anvil code.
2:43 The yellow means it ran on the server.
2:46 How cool is that?
2:47 So, if you look at this over here
2:48 if we look at this, it has the white background.
2:51 And we look at this, it has the yellow background.
2:52 Just remind you, server, server, server
2:55 client, client, client.
2:56 And the output works the same way.
2:58 So all we had to do is make this little function
3:00 and we could call it.