Jonnie Grieve Digital Media: Blog

Home
by on 24th June, 2021 - 3:04pm (0)

Blog: A Command Line App in SQLAlchemy (More Posts)

This blog post continues on in my learning journal series about SQLAlchemy. I will be attempting to create a CLI Application based Megan Amendola’s excellent teaching on Treehouse but changed ever so slightly to expand the scope of the database to create records for all kinds of media, not just books. This is her wisdom I’m putting into text form to try and help me understand it better. To this end, there’s a “media” model and database file. And to be honest, this is probably the easiest bit of the project. By the end of this post, there will be a simple, usable application that runs what the user chooses at the CLI and can be exited at the press of a single key character.

The first thing to do is to make sure all the key imports are in place in an app.py Python file.

# import models
from models import (Base, session, Media, engine)

This allows us to access the database setup code and the model which is located in a file, in the same directory called models.py.

At this stage, there are 2 functions in this application python file. Those functions are menu() and app().

The menu function introduces the CLI interface, the menu of options that guides the user into how to use the app. The user is asked a question via the input() method and a response by a single character is returned. Which is where the app() method takes over.

def menu():
while True:
print('''
\nMEDIA LIBRARY MENU
\r1) Add Item
\r2) View All Books
\r3) Search Media Item
\r4) Media Item
\r5) Exit the Application ''')

# store a variable for the menu coice
choice = input('What would you like to do? ')
if choice in ['1','2','3','4','5']:
return choice
else:
input('''
\rError: please try one of the choices above
\rA number from 1 to 5.
\rPress Enter to Try Again. ''')

The job of the app() method to keep the application running until user exits.

def app():

app_running = True

while app_running:

choice = menu()

if choice == '1':
#add menu item
pass
elif choice == '2':
# view book
pass

elif choice == '3':
# view book
pass

elif choice == '4':
# analyse - media item details
pass

else:
# default option to exit the application
print('GOODBYE')
app_running = False

app_running is a boolean variable that governs the running state of the application. If the variable is set to false, that’s the end of the program; the user has purposefully ended the program.

If however the variable is assigned the boolean value True then all the other options remain available and the script keeps running.

Each condition block is assigned a number as a string and that number dictates which code block is run.  The default block exits the program. But since it is the default and ‘5’ is the only other option in the menu method that is the only other string that is accepted as the default. Otherwise, the custom error message is returned.

Finally, in the main “dunder” method we use SQLAlchmey to add any new data and call the menu method which displays the menu options.  You can call any method you want in this function but the one we need in this case is menu()


if __name__ == '__main__':
Base.metadata.create_all(engine)
# menu() - call menu function
# app()

And that’s all for this post. This is what is needed to get the application started.  It’s going to get much more complicated and involved from here on in and I’m looking forward to seeing how it all fits together.

This post has been assigned to the following categories

    Leave a Reply

    Your email address will not be published. Required fields are marked *