Listing Items
00:00 In the last lesson, you implemented the add to-do functionality. In this lesson, you’d explore the list to-do functionality and add it to your command-line application.
00:10 And before writing code for listing to-do items, I’ll just add a few more items to my database.
00:23 The list command will allow users to list all the current to-dos in the database. And before adding the list command to the CLI, you need a way to retrieve the entire to-do list from the database.
00:35
And to achieve this, you would add a get_todo_list()
to the Todoer
class. So head over to the todo.py
module. And here you would add a get_todo_list()
method.
00:48 This will be a short method that takes no parameters and would return a list of dictionaries with keys that are strings and values of any data type. You can add a little documentation with a docstring.
01:05
And using the DB handler, you’d call the read_todos()
method and assign this value to a variable called read
. And as you know from the database handler, this read_todos()
would return an instance of DBResponse
, which will contain a list of to-dos and an error code.
01:22
And if there are a list of to-dos in the database, you just return from the instance of the DBResponse
return the to-do list.
01:30
Now that you have this method in your Todoer
class, you can now go ahead to implement a list CLI type of command for your application. And to do that, you head over to the cli.py
module and under the add command, define a function called list
.
01:46
_all
. This function will take no arguments. Add a little documentation using a docstring.
01:55
And as you know, this is just a function until you convert it to a Typer command. And to do that, you use the @app.command
decorator. Since this is a long name, you can provide a shorter alias to reference this command.
02:09
And for that, you can either just pass list
or say name="list"
and list
will now be the name for this command. To confirm that, you can head over to your terminal and if you run python3 -m rptodo
--help
.
02:29 This should now list or show you your list command added to the list of commands available in your CLI application. Back to implementing its functionality.
02:41
The first thing you need is an instance of the Todoer
class. To get that, to a variable called todoer
, use the function get_todoer()
.
02:50
Remember, this will give you an instance of our utility class Todoer
that helps you interact back and forth between the database and the CLI.
02:59
And with this todoer
instance, you can now call the get_todo_list()
method and assign that to a variable called todo_
list =
the instance of your Todoer
class get_todo_list()
method.
03:14 Here you’re expecting the list of to-dos, but if there are no items in a database, you can let the user know that there are no items in the to-do database.
03:22
And for that, you check the length of the list returned. If using the len()
method, check the length of the list.
03:31
This equals zero, meaning no to-do items in a database. You can let the user know using typer
.secho()
, there’s a message. You can just say There are no to-do items in the database yet
.
03:46
And you can style this to have red text with typer
.colors.RED
. If this happens, at this point, it’s safe to exit the operation using typer.exit()
.
03:57 But if there are, you are going to start to build up a way to display the list of to-dos to the user. This will be printed out in the console. So now you try to construct a fancy little to-do table.
04:09
You can start with typer.secho()
ahead of first and say new line.
04:18
The table will have a text color blue and for that, you can use typer.colors.BLUE
. And you can make this with bold text using the option bold=True
.
04:31
Next, you define the columns for the table into a variable called columns
you’ll assign a tuple and in it, in quotes, ID
all uppercase and double space.
04:44
Next, a pipe symbol. This will be a divider for the columns, space. Priority
double space, next pipe space Done
double space, comma.
05:00
Next pipe space Description
double space, comma.
05:07 The reason these double spaces are introduced is so that the table column header can be formatted properly. You’d see this when the table’s printed in the terminal.
05:16
Next, you join all this to a variable called headers
.
05:20
You design the value using the .join()
method for i
passing columns
. You print out these headers to the console using typer
.secho()
passing the headers and then you style that with fg=typer.colors.
BLUE
for blue text.
05:39
And then for the styling, bold=True
.
05:44
Then you could add a little column header base with typer
.secho()
into that you pass the value in quotes, a single dash * len(headers)
.
05:55
This will provide a series of dashes and then you style that with fg=typer.colors.
BLUE
.
06:04 Then you start to iterate through the to-do items from the database passed from a list and then print them out to the console. And for this, I’ll be pasting the code for a fancy styled printout for the to-do items.
06:18 This is just to help space them out accordingly. You can pause the video and type out the code as I have it on the screen. Or alternatively, the full source code for this project is already provided to you in the additional resources section.
06:32
You can download the code files, go through them and copy the needed code. This will be under the rptodo
package cli.py
file. And then look for the list or Typer CLI command or function in what you have for id to do.
06:51
In running through the to-do list using Python’s enumerate()
while starting from integer one. Then you get the description, priority, and done status for each of the to-do items.
07:03
Knowing these are Python dictionaries, you can obtain them using the .values()
method of a Python dictionary. And then you start to print each of them in the console, giving them appropriate spacing for readability.
07:16 And after that, you end it with a base border, which is just a series of dashes. And with this, you’ve just implemented the list command. And to give this a try, you can now head over to your terminal.
07:29
You can now run python3 -m rptodo
list
and running that will fetch and list the to-do items from your database and have them printed out in your console start accordingly. The output shows all the current to-dos in a nicely formatted table.
07:47 Next, you’ll explore the to-do completion functionality where users will be able to mark the to-do item as completed with a done column that is true if completed and false if not.
Become a Member to join the conversation.