1
00:00:00,000 --> 00:00:03,990
Here we have a simple database application that displays a list of random

2
00:00:04,000 --> 00:00:06,990
testimonials or quotes on a web page.

3
00:00:07,000 --> 00:00:09,990
If you look at my web page, I actually used this for testimonials.

4
00:00:10,000 --> 00:00:14,990
And every time the page gets reloaded, it pulls up three different quotes from

5
00:00:15,000 --> 00:00:16,990
the database at random.

6
00:00:17,000 --> 00:00:21,990
The database itself is managed with this application that allows you to go

7
00:00:22,000 --> 00:00:25,990
through and add, edit and delete entries in the database.

8
00:00:26,000 --> 00:00:28,990
We'll look at that in a separate movie in this chapter.

9
00:00:29,000 --> 00:00:33,990
In this chapter, we're going to look at how this application works ,which

10
00:00:34,000 --> 00:00:37,990
retrieves the quotes from the database and displays them at random.

11
00:00:38,000 --> 00:00:42,990
So in the Projects folder, in your Exercise Files, in the testimonials folder

12
00:00:43,000 --> 00:00:45,990
and you'll find testimonials.py.

13
00:00:46,000 --> 00:00:48,990
And this is actually exactly the same script that is on my web site.

14
00:00:49,000 --> 00:00:53,990
This is the script that reads the quotes from the database and displays a

15
00:00:54,000 --> 00:00:53,990
number of them at random.

16
00:00:54,000 --> 00:00:56,990
It's a very short script.

17
00:00:57,000 --> 00:01:01,990
It's just 73 lines long, including the white space and comments and everything,

18
00:01:02,000 --> 00:01:02,990
and here's how it works.

19
00:01:03,000 --> 00:01:08,990
The first thing it does is it initializes some variables.

20
00:01:09,000 --> 00:01:11,990
It reads the configuration file to find out where the database is and

21
00:01:12,000 --> 00:01:14,990
everything, and opens up the database.

22
00:01:15,000 --> 00:01:18,990
And all that happens here in this init function.

23
00:01:19,000 --> 00:01:23,990
Here it reads the configuration file there and it opens up the database here.

24
00:01:24,000 --> 00:01:29,990
And this uses libraries that I've also included in the Lib folder.

25
00:01:30,000 --> 00:01:37,990
bwDB for my normalized CRUD oriented database functions, and bwConfig for

26
00:01:38,000 --> 00:01:38,990
reading the configuration file.

27
00:01:39,000 --> 00:01:42,990
bwConfig is a very simple library.

28
00:01:43,000 --> 00:01:46,990
It reads these configuration files in a format that I had already been using for

29
00:01:47,000 --> 00:01:50,990
a long time with my Perl work from before I was working on Python.

30
00:01:51,000 --> 00:01:53,990
And so I just wanted something that would read the same configuration file, so I

31
00:01:54,000 --> 00:01:57,990
could start replacing a lot of my Perl scripts with Python scripts, and it was

32
00:01:58,000 --> 00:02:00,990
just very easy for me to write that.

33
00:02:01,000 --> 00:02:05,990
And the application itself mostly takes place in this one function, in main.

34
00:02:06,000 --> 00:02:09,990
The first thing it does is it goes out to the database and it grabs a list of

35
00:02:10,000 --> 00:02:16,990
all the IDs, and it does this with this sql_query method which returns a

36
00:02:17,000 --> 00:02:20,990
generator which iterates through the results.

37
00:02:21,000 --> 00:02:29,990
And iterating through results, I simply take all of these IDs and add them to this list here.

38
00:02:30,000 --> 00:02:33,990
I'm using a list because I need to be able to delete things from it later.

39
00:02:34,000 --> 00:02:35,990
So a tuple would have been immutable.

40
00:02:36,000 --> 00:02:39,990
Then I want to know how many records I'm going to be displaying and so I

41
00:02:40,000 --> 00:02:45,990
look for the query string and if there isn't a query string, that just default to five.

42
00:02:46,000 --> 00:02:53,990
And we'll notice, if we look at the HTML for the page that has those quotes on it,

43
00:02:54,000 --> 00:02:58,990
you'll notice this is called with this little bit of server side include and

44
00:02:59,000 --> 00:03:04,990
so it's called testimonials.py with a query string set to 3.

45
00:03:05,000 --> 00:03:08,990
And so, that's why we have three of them displaying on that page.

46
00:03:09,000 --> 00:03:12,990
And then, I check and see if we have enough in the database to be able to

47
00:03:13,000 --> 00:03:14,990
display a decent number randomly.

48
00:03:15,000 --> 00:03:18,990
I like further to be at least four times as many records in the database as

49
00:03:19,000 --> 00:03:21,990
those that I am displaying. Otherwise I really don't want to bother doing

50
00:03:22,000 --> 00:03:26,990
something random because it's not going to look random.

51
00:03:27,000 --> 00:03:35,990
Now we simply take the list of IDs and using the random library, which gets

52
00:03:36,000 --> 00:03:37,990
imported up at the top here.

53
00:03:38,000 --> 00:03:41,990
This is in the standard Python library.

54
00:03:42,000 --> 00:03:48,990
It creates a random integer based on the size of the list minus one, and it

55
00:03:49,000 --> 00:03:54,990
grabs a random item from the list of IDs, and then it deletes that from the list

56
00:03:55,000 --> 00:04:00,990
and adds it to the list of result IDs, which is initialized here.

57
00:04:01,000 --> 00:04:03,990
Then once it has that list of result IDs,

58
00:04:04,000 --> 00:04:07,990
it simply iterates through that list and prints the record with that ID.

59
00:04:08,000 --> 00:04:12,990
And printing a record with that ID, it uses the CRUD function getrec and then

60
00:04:13,000 --> 00:04:17,990
it simply grabs the data out of the resulting row factory, indexing on it as if

61
00:04:18,000 --> 00:04:18,990
it were a dictionary.

62
00:04:19,000 --> 00:04:20,990
We'll go ahead and run this here.

63
00:04:21,000 --> 00:04:27,990
Well for our purposes, we're going to need to reduce this little default down to a three.

64
00:04:28,000 --> 00:04:33,990
So I'll save that and run it, and here we have our result.

65
00:04:34,000 --> 00:04:39,990
We have three quotes, and if we run it again, we get three different quotes and

66
00:04:40,000 --> 00:04:45,990
different quotes and you'll notice that it's all coming out in HTML.

67
00:04:46,000 --> 00:04:48,990
So, that's how this application works.

68
00:04:49,000 --> 00:04:52,990
Once we have done the back end work of creating the CRUD, once we have done the

69
00:04:53,000 --> 00:04:58,990
back end work of creating a decent library with easy to use database interface,

70
00:04:59,000 --> 00:05:02,990
writing something like this becomes relatively trivial.

71
00:05:03,000 --> 00:05:13,000
And so, here we have it on the webpage with our random quotes.

