Doing Data Entry5:10 with Kenneth Love
Let's define our model and get our database connection set up. We'll also make our script executable so we can run it in a nicer manner.
TextField()- a field that holds a blob of text of any size
DateTimeField()- a field for holding a date and a time
If you're not sure what to put after
/usr/bin/env, test it out in your terminal program.
/usr/bin/env python and you should get a Python shell like normal. If it says 2.7 or something other than the 3.4 you should be expecting, try
/usr/bin/env python3. Whichever of these gets you the correct Python shell is the one you should put at the top of your file.
Here are the official docs on this. It should just work, but if it doesn't, I'm sure the official docs will help you troubleshoot it.
Since we're using Pee Wee, 0:00 connecting to our database isn't that big of an issue, but we still need to do it. 0:01 We also need to make a model for our entry in our diary. 0:05 Let's get into work spaces and get this going. 0:08 Our entry model is gonna be what holds our entries, kind of obvious from the name. 0:10 anyway, we're currently planning on there being two fields. 0:16 Our content field, the one that we're kind of thinking about right now, 0:19 should be able to hold any amount of text that we want to put into it. 0:23 And pee wee happens to have a field type that is named text field, so 0:26 let's use that ,. 0:31 Okay. 0:35 And then for our time stamp field here, wow. 0:36 We will need something that will hold a date and a time. 0:42 That's what a time stamp is and Pee Wee has a field just for 0:45 that and it's called date time field. 0:48 And we're gonna give this a default so 0:52 that we don't have to set it ourselves every single time. 0:55 So the default will be datetime.datetime.now. 0:58 I'll point out something here in just a minute, but 1:03 this means that we need to import date time, of course. 1:05 So I want you to notice, that I didn't put parentheses on the end of this. 1:09 Whenever it goes to create the default, it sees that it's a function and it will call 1:15 it, and that way we get the date time now, whenever the entry is created. 1:19 We wouldn't get that if we put the parenthesis on there. 1:25 It would make it to whatever the time was when we ran the script. 1:28 Obviously we don't want it to be one date time for all of our entries. 1:32 So leave off those parenthesis in there. 1:35 And the reason that we're using a text field, instead of a varchar field. 1:39 Varchar fields have to have a maximum length. 1:44 Now it can be a gigantic, huge length, but it has to have a length. 1:46 Text fields, 1:51 on the other hand, just hold whatever blob of text that we want them to hold. 1:52 So that's what we wanna have. 1:56 All right, so now let's go down here and let's give 1:57 ourselves a little function, that will initialize everything for us. 2:03 We will call this initialize and 2:11 we will say create the database and the table if they don't exist. 2:14 All right, and then just like before we are going to do db.connect and 2:24 db.create tables. 2:28 And we're gonna pass in Entry, and safe is equal to True. 2:30 Okay. 2:36 Now, maybe I'm confused, but 2:37 I'm pretty sure we wanna run this, whenever the script is run. 2:41 We wanna make sure we always have our initialization. 2:44 So let's go ahead and do initialize down here at the bottom, in our loop. 2:46 So this way, or not in our loop, before our loop. 2:52 So this way we know that the database and 2:55 stuff exists before we ever get around to like running the the app. 2:57 So let's give that a try. 3:02 diary.py. 3:04 All right. And if we do an LS, there's our diary.db. 3:07 And let's go in there again real quick, just to see what's going on. 3:12 We can do .tables. 3:18 There's our entry table. 3:19 Select star from entry. 3:21 There's nothing there. 3:23 And exit. 3:25 All right, so that's just like we want. 3:26 So I'd like to make this to where I can run it. 3:28 You shouldn't have to do this if you're on Windows. 3:32 Python, when you install it, it's supposed to make it to where everything. 3:35 like, you can run a Python script right away because Windows knows that 3:40 they're Python scripts and what to run them with. 3:43 But if you're on Linux or 3:45 Mac or Workspaces, then you'll have to do this or a variation of this. 3:46 So on Workspaces, what we have to do, come up here to the top. 3:51 And we have to add what's called a shebang line. 3:55 So it starts with a hash and then an exclamation mark. 3:58 Hash, bang, shebang. 4:00 And then we say /usr/env/bin. 4:01 And what oops, sorry. 4:06 User, bin, env. 4:07 And so what this does is uses the env program to find 4:10 whatever executable we're about to tell it. 4:15 Now on work spaces we have to specify python3. 4:17 There's a good chance you'll have to do this on your system as well. 4:20 Otherwise it's gonna run it with Python2, 4:25 because that's what /usr/bin thinks we want for Python. 4:26 So, /usr/bin/env python3. 4:29 Then we come back down to our console, and we do chmod, or 4:33 change mode, plus x for diary.py. 4:37 And this is the part that I love, I can now do [SOUND] that and it runs. 4:42 Now, we didn't see anything because don't have any being printed out. 4:49 But it run without giving me errors. 4:52 At some much nicer way of interactive with our program, 4:54 then having to type python and the name out every single time. 4:58 Hi, I think we're ready to actually build the logic of our application. 5:01 Lets start with the different way to make a menu that what we've done on the past. 5:05 I think a lot of you will really like this approach. 5:08
You need to sign up for Treehouse in order to download course files.Sign up