#100DaysOfWeb in Python Transcripts
Chapter: Day 52: Anvil - Full Stack Web Apps
Lecture: Add doc form finale
0:00 It's great the we created this document
0:01 but the experience wasn't super,
0:02 like it didn't tell us the thing was created,
0:05 we put a bunch of code right into our form,
0:09 things like that.
0:10 So let's clean this up a little bit
0:11 and let's begin by going over here
0:13 and creating a new module.
0:15 I'll call this...
0:18 I'll just call this utilities.
0:20 Now over here we right code, this runs on the client-side
0:24 and it's going to do...
0:25 This basically lets us put these methods wherever we need.
0:28 So one the things we're going to need to do,
0:30 this will take a moment before it's obvious why,
0:32 but we need to, when we're over here
0:35 When we want to go home, we need the home form,
0:37 which we don't have access to,
0:39 to basically run this code again, okay?
0:43 How do we do that?
0:45 Well what we can do is we can come over here
0:47 and say, import utilities
0:52 We can go the the utilities and say,
0:53 home_form = self
0:56 so that we can always get back to it
0:58 from within code over here.
1:00 So we'll have a home_form
1:04 as None
1:05 but of course it will be set soon the application starts.
1:08 And let's just add a method, "Go home".
1:12 How do we do that?
1:13 We say,
1:16 Now it doesn't know what this is.
1:19 We want to call this function here.
1:26 Like that.
1:27 That's all we got to do, super easy right?
1:29 We just wanted to call this function
1:31 go here from wherever.
1:32 So we need this import utilities over in
1:35 our add_doc_form as well.
1:41 And let's go over here and we can just say,
1:45 We've already created the document but
1:46 let's take some of this code and make a little simpler.
1:49 Let's go over here and say, utilities.create.
1:59 And let's pass that in there, okay?
2:02 So we basically need to call this function
2:04 over in this utilities.create_doc,
2:06 but there's a reason I want to do this.
2:07 You'll see in just a second that it makes sense
2:09 to try to put these together.
2:10 So it's find this.
2:14 Remove that one simple call over here.
2:19 Like that.
2:20 And it's going to accomplish the same thing,
2:22 but we also would like to have one more function.
2:29 Now what I want to store is I want to store
2:34 the docs as a list and the categories as a list.
2:45 And when we call this, I'd like to write that code
2:47 that we had over here again.
2:49 Remember I said I didn't like this?
2:50 Let's take this code and get rid of it
2:53 and put it over here as well.
3:00 So here we're going to go to the server
3:01 and we'll also do this for the documents.
3:03 If you had a ton of documents that's not a good idea,
3:08 but since we only have a few we'll go like
3:13 Okay, so we're going to set the categories and the documents
3:17 and that means over here we can just go...
3:21 We've got our list, let's say
3:27 not categories.
3:29 Now how do we know this is refreshed?
3:31 Let's call it once when the app starts up
3:33 and that's all we're going to do.
3:40 Alright so that should refresh the data
3:42 and then any of the sub-forms will have access
3:45 to the utilities dot categories or documents.
3:50 And here we'll call create_document
3:52 and then we'll call go_home.
3:53 And within create_document, final thing,
3:54 the reason it's nice to have this here
3:56 is we can call refresh_data again and make sure
3:59 that whenever the data is modified,
4:01 we automatically get it updated
4:03 but otherwise, it just stays like it is.
4:06 Let's go and test this, that it's still working.
4:12 Apparently spelling is hard.
4:19 Alright, what happens when I click this?
4:20 It's going to go to the utilities client-side module,
4:24 call the create_document.
4:26 On the server it's going to create and insert the document,
4:28 it'll come back and then it'll call Refresh
4:30 so if for some reason the categories,
4:32 definitely the documents will have changed,
4:34 we'll get those new ones.
4:35 And then it's going to go to the home form, call go_home,
4:37 it should reload this and it will all work.
4:39 Let's try it.
4:43 On the server we created this new document, now we're home.
4:46 We can add more of course.
4:48 I think it's about time to do the other ones
4:50 where we can actually see the documents right?