Jazz up a CLI project with color and sound!
Tags: webdev, beginners, python, sql
Published: 2023-04-02 15:50:06

I had a fun time wrapping up phase 3 at Flatiron School in Denver, CO with a CLI project that combined Python with SQLAlchemy. I chose to make a game inspired by Monty Python's Quest for the Holy Grail. If you'd like to check it out and attempt to join King Arthur's Round Table, here is the [github repo](https://github.com/abbyesmith/phase3_quest.git). If you try it out, let me know in the comments what you thought!

![poster of Monty Python and the Quest for the Holy Grail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bplpgt2ipcanb4v0uwq0.jpeg)

While you play the game, you may notice that color is used in the command line to make the game more user friendly and sound is used for additional feedback for the user. 

Sound and color were the last add ons to the project before I presented on Friday March 31 and I'm glad I did! Those finishing touches helped my project stand out from some of the others.

---

**How to add color to Python CLI**

There are many different ways to add color into your command line in Python. I decided to try out [Colorama](https://pypi.org/project/colorama/). This model will allow the developer to change the foreground color, background color, and weight of the command line text. 

To get started, install Colorama in your pipfile. Open up your Python project's terminal and type in the following command:

`$ pipenve install coloarama`

Check out your pipfile to ensure it was successfully added:

![pipfile with colorama installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aiwezfxx2d9lqwo1wuvy.png)

(Note - alternatively, you could use the command 
`$ pip install colorama` if you do not have a pipfile, this method will make it more difficult for the end user to start up your project because they must manually install colorama)

Now that you have colorama installed in your Python file, choose a file that you would like to add color. At the top of your file, import colorama.

`import colorama`

You must do this on any file that uses colorama.

Before you get started adding your color, I suggest making a cheat sheet for yourself. Decide when and why color is used. I decided on the following rules for my project:

- Pictures: Magenta Text
- Press Enter: Cyan Text
- Pick a number or text input: Green & Bold Text
- A or B input: Yellow & Bold Text

The following colors are available through Colorama:

- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white

To make the text a specific color, use to following in the print or input line:

`print(Fore.MAGENTA + 'the text that should be magenta')
`
To add background colors:

`print(Back.GREEN 'the text that should be highlighted in green')`

To change the weight of the text:

`print(Style.DIM +'this text will have a lighter weight'`
`print(Style.NORMAL +'this text will have a normal weight'`
`print(Style.BRIGHT +'this text will have a bold weight'`

You can combine the commands to give a font color, background and/or weight in one line:

`print(Fore.YELLOW + Back.BLUE + Style.BRIGHT 'this text will be bright yellow with a blue background`

One pesky aspect of Colorama is the style persists until you reset the style. When you are ready for the styling to stop, insert this line into your code:
`print(colorama.Style.RESET_ALL)`

---

**How to add Sound in a Python file**

Small sound effects with mp3's added a lot of fun moments in my project and it took relatively little work on my behalf. I used the [playsound model](https://pypi.org/project/playsound/).

To get started, install playsound so it is added to your pipfile:

`$ pipenv install playsound`
(or `$ pip install playsound` if you do not have a pipfile)

I noticed that some people must also install [pyobjc](https://pypi.org/project/pyobjc/). Read the messages when you install playsound to see if you need to do this too.

At the top of any file that you'd like to use sound, import playsound.

`import playsound from playsound`

Add whatever mp3 file that you'd like to play when the user is engaging with the command line. I used [this site](https://pixabay.com/sound-effects/search/mp3/) to find small sound effects when the user answered a question correct or incorrect.

To insert the sound, decide when you would like the sound effect to play and insert the following line of code:

`playsound("path to mp3")`
for example:
`playsound("./failure-drum-sound-effect-2-7184.mp3")`

This is a great and quick method if you have a quick sound effect. The program will play through the entire sound clip before it processes the next line.

If you would like the sound to play in the background while the program continues to run, you'll need to import threading at the top of the file (no need to install anything! Threading is part of the Python package.)

`import threading`

I decided to create the variable 'play_sound' to simplify my code later on.

`def play_sound():
    playsound('./the-knights-at-camelot-singing-the-entire-knights-of-the-round-table-song.mp3')`

Going into the program, I found when I wanted the song to start and called the following:

```
sound_thread = threading.Thread(target=play_sound)
sound_thread.start() 
```

The threading method allows the rest of your code to continuously run while the mp3 plays in the background.

---

**Last Thoughts**

I hope this quick tutorial helped you spice up your CLI project. I cannot believe that I am already 60% of the way through the Flatiron program - it's been a whirlwind and I have the amazing Flatiron team of David, Stephen, Sam and Yesenia to thank for their support in addition to my amazing cohort! I don't know if I would be successful if it weren't for this gracious community.