Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Here we have a simple database application that displays a list of random testimonials or quotes on a web page. If you look at my web page, I actually used this for testimonials. And every time the page gets reloaded, it pulls up three different quotes from the database at random. The database itself is managed with this application that allows you to go through and add, edit and delete entries in the database. We'll look at that in a separate movie in this chapter. In this chapter, we're going to look at how this application works ,which retrieves the quotes from the database and displays them at random.
So in the Projects folder, in your Exercise Files, in the testimonials folder and you'll find testimonials.py. And this is actually exactly the same script that is on my web site. This is the script that reads the quotes from the database and displays a number of them at random. It's a very short script. It's just 73 lines long, including the white space and comments and everything, and here's how it works. The first thing it does is it initializes some variables.
It reads the configuration file to find out where the database is and everything, and opens up the database. And all that happens here in this init function. Here it reads the configuration file there and it opens up the database here. And this uses libraries that I've also included in the Lib folder. bwDB for my normalized CRUD oriented database functions, and bwConfig for reading the configuration file.
bwConfig is a very simple library. It reads these configuration files in a format that I had already been using for a long time with my Perl work from before I was working on Python. And so I just wanted something that would read the same configuration file, so I could start replacing a lot of my Perl scripts with Python scripts, and it was just very easy for me to write that. And the application itself mostly takes place in this one function, in main. The first thing it does is it goes out to the database and it grabs a list of all the IDs, and it does this with this sql_query method which returns a generator which iterates through the results.
And iterating through results, I simply take all of these IDs and add them to this list here. I'm using a list because I need to be able to delete things from it later. So a tuple would have been immutable. Then I want to know how many records I'm going to be displaying and so I look for the query string and if there isn't a query string, that just default to five. And we'll notice, if we look at the HTML for the page that has those quotes on it, you'll notice this is called with this little bit of server side include and so it's called testimonials.py with a query string set to 3.
And so, that's why we have three of them displaying on that page. And then, I check and see if we have enough in the database to be able to display a decent number randomly. I like further to be at least four times as many records in the database as those that I am displaying. Otherwise I really don't want to bother doing something random because it's not going to look random. Now we simply take the list of IDs and using the random library, which gets imported up at the top here.
This is in the standard Python library. It creates a random integer based on the size of the list minus one, and it grabs a random item from the list of IDs, and then it deletes that from the list and adds it to the list of result IDs, which is initialized here. Then once it has that list of result IDs, it simply iterates through that list and prints the record with that ID. And printing a record with that ID, it uses the CRUD function getrec and then it simply grabs the data out of the resulting row factory, indexing on it as if it were a dictionary.
We'll go ahead and run this here. Well for our purposes, we're going to need to reduce this little default down to a three. So I'll save that and run it, and here we have our result. We have three quotes, and if we run it again, we get three different quotes and different quotes and you'll notice that it's all coming out in HTML. So, that's how this application works. Once we have done the back end work of creating the CRUD, once we have done the back end work of creating a decent library with easy to use database interface, writing something like this becomes relatively trivial.
And so, here we have it on the webpage with our random quotes.
Get unlimited access to all courses for just $25/month.Become a member