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