Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Application first steps
Lecture: Validation user inputs for measurements
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well we've got our UI in place, but if I run it and I click on this Add Measurement, you can see not too much is happening.
0:09
Let's go and actually have that button click do something and makes sure we have the right kind of data and then almost save it back to the database.
0:17
We haven't created that yet, but when we do, it will just be a one stop, one line, to finish this off. So far, we've been in this visual bit
0:25
on the design side of the form. Let's go to the code behind over here and hide that so we have the most room.
0:32
Actually, we need one quick thing before we do that. We need to hook into this event when somebody clicks this.
0:37
So we can just double click it, make sure the name is set to something you would like to have because it's going to be part of the method here.
0:45
And then, we are going to go write what code we need to do. So we're going to have a couple of steps here. Let's separate it out. Let's actually
0:54
you can override as many functions as you want these are just Python classes right? So I can come over here and write one called sync_data.
1:00
The idea is it's going to take all the elements, UI elements like the text box's text and turn that into a number
1:07
or given us an error message if it's impossible. So for a moment I write pass. Let's go down here what we are going to do is we are going to say
1:15
and be an error that maybe, maybe has returned and we are going to auto-complete on that, that's awesome.
1:21
And we'll say if error is return, which means just stay on the form, this sync data is going to set the error
1:28
let's go and actually do it this way, let's say self. it was a label_error_message.text = error and let's do a visible = True.
1:41
And then we're going to return. So if there is an error, we're going to show it. And also let's go at the beginning before we do any of this, let's say
1:52
also we are going to hide it but in case there is an error we are going to show it but again and set the error. And then, later, I'll just print
2:01
would have saved the data on the measurement. The other thing I want to do is have a way to easily communicate what those values are we got.
2:10
So let's just go over here, we'll have a self.weight = 0 self.rate = 0, and self.date = None. The date is the day we said we recorded the measurement.
2:23
And we can even come down here and say what measurements or what values we are going to put and we can do a little format, just like standard Python.
2:32
So we can do it like this. Why am I so fascinated that I can write this code? Because this code runs in your browser
2:40
on top of a Javascript engine. It's super cool. But we get a right Python which is the way we like it. This is good.
2:47
Let's go over here and just put an error message for now. This will always fail. Try to run it. It will go click this, it's going to return an error
2:56
the system will see there and error, it's going to show the error message and set it to whatever we return. that didn't work so well, did it?
3:04
What step did we get here? Ah, because I forgot the word return. You know, details. Okay, try again. This will always fail.
3:13
Okay, so our validation is working whatever message gets back, if there is any from that sync_data, it's going to show that as an error.
3:20
So let's go and actually to the validation here. So there are several layers of what we need to do. First, if there is no data
3:27
we can say if not self.text_box_rate.text alright if it's empty then we can return some kind of message like heart rate is required.
3:42
We can do something similar for the others. Let it fall through but return none is probably good here.
3:51
We're going to say, if you haven't filled out the heart rate that's required. If you haven't filled out the weight
3:54
that's required and if you haven't selected a measurement date that's required. Let's just check those real quick.
4:01
So click that heart rate is required, so put a 79 weight, 170, whatever it is. Measurement and date is required pick one of those.
4:11
Boom, would have actually saved this it didn't really work because we didn't actually get the data but we're on the right path.
4:18
So that's working well. The next thing we need to do is we need to try to get those values because maybe this is like some text value but
4:25
it's not a number, right? Not a proper integer. So over here, we're going to say self.weight and let's just do Exception
4:37
and we'll return a known error here alright so we're going to return the error message here but probably the most common thing is it's possible
4:45
we get some kind of conversion error that's going to show up as a type error swool. And then, in this one we are going to return invalid format.
4:57
Okay, so in here, what are we going to do? We're going to convert from a string to an integer. So we're going to take the string and
5:02
pass it to the end constructor self.text_box_weight.text, and you know what? We're going to do something real similar for the rate.
5:18
For the date it's already in the right format. So we just go to date_of_measurement.date as the selected date, and there we go.
5:25
So if those all work right we should have those set when we get back here and we'll have a go. So, I think we'll have it right.
5:31
Let's give it a shot here. We'll put this away for a minute and come over here and we add a measurement, a heart rate is required, okay
5:38
so let's say the heart rate is 71 and let's say the weight, 170 or whatever it is measurement date is required, let's say it's today
5:50
You ready? This should work. It should convert those all out and then print them into this little output window.
5:55
Boom. Notice the red dot in the upper left. That means there is new output that would have gone to the terminal or somewhere.
6:02
Go look back at it and you see we would have saved the measurement, it would have been 170 pounds
6:07
on August 2nd with a heart rate of 71 beats per minute. It works! How cool is that? Now we haven't saved that to the database
6:15
we just printed it with the console you're going to see it's actually about the same amount of work.
6:20
Right, so there it is our add measurement component is done and we're ready to start using it.