Saltar al contenido
Back to home
Session 03 Python

Bring Your Terminal to Life: Colours and Animations with Python

We'll go beyond print() with colours, animations, progress bars and spinners. We'll finish by building a terminal deployment simulator. Basic knowledge of variables, loops, conditionals and functions is required.

Bring Your Terminal to Life: Colours and Animations with Python

🎯 Session goals

  • Add colour and style to text.
  • Rewrite the same line to create animations.
  • Build a progress bar with a function.
  • Build a simple spinner.

📋 Prerequisites

  • Python 3 and an editor (recommended: VS Code).
  • Comfortable with variables, for loops, if and functions.
  • Run your scripts from the terminal with python file.py.

💡 Windows: add os.system("") at the start of the script so colours work.


1. Coloured text

The terminal understands special codes starting with \033[. They are not displayed: they change the colour of the text.

import os
os.system("")  # Enables colours on Windows

RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BOLD = "\033[1m"
RESET = "\033[0m"

print(f"{RED}Error: something went wrong{RESET}")
print(f"{GREEN}✔ All good{RESET}")
print(f"{YELLOW}{BOLD}⚠ Important warning{RESET}")

⚠️ Always finish with RESET, or everything after it will stay coloured.

You can also use any RGB colour. This is The Dev Hub red:

DEVHUB_RED = "\033[38;2;255;50;40m"
print(f"{DEVHUB_RED}{BOLD}THE DEV HUB{RESET}")

2. Rewriting a line

If we go back to the start of the line with \r and do not add a new line, we can write over it:

import time

for i in range(101):
    print(f"\rLoading... {i}%", end="", flush=True)
    time.sleep(0.03)
print()
  • \r → goes back to the start of the line.
  • end="" → does not add a new line.
  • flush=True → shows it immediately.
  • time.sleep(0.03) → waits 0.03 seconds.

3. Progress bar

We write a function that returns the bar as text:

import time

def bar(progress, total, width=30):
    filled = int(width * progress / total)
    drawing = "█" * filled + "░" * (width - filled)
    percentage = progress / total * 100
    return f"[{drawing}] {percentage:5.1f}%"

for i in range(51):
    print(f"\r{bar(i, 50)}", end="", flush=True)
    time.sleep(0.05)
print("\nDone!")

🔍 {percentage:5.1f} shows the number with 1 decimal place, always taking 5 characters, so the bar does not move.

4. Spinner

A spinner is a list of symbols that take turns. The % operator takes us back to the start when we run out:

import time

SPINNER = "|/-\\"

for i in range(30):
    symbol = SPINNER[i % len(SPINNER)]
    print(f"\r{symbol} Connecting...", end="", flush=True)
    time.sleep(0.1)
print("\r✔ Connected      ")

The trailing spaces erase what was left of the previous line.


🏋️ Exercises

  1. Log messages: write a function log(level, message) that shows INFO in green, WARN in yellow and ERROR in red.
  2. Countdown: show a countdown from 10 to 0 on the same line and, when it ends, 🚀 Lift-off! in green.
  3. Coloured bar: change bar() so it is red below 50% and green from there on.

🏆 Final challenge: deployment simulator

Create a script deploy.py that simulates deploying an application:

  1. Show a title in The Dev Hub red.
  2. Use a list of steps, for example: Installing dependencies, Running tests, Building, Uploading to server.
  3. For each step, show a spinner for a few seconds on the same line, formatted as [2/4] / Building....
  4. When it finishes, replace the line with [2/4] ✔ Building in green.
  5. At the end, show the progress bar and the message 🚀 Deployment complete.

Bonus: make each step able to fail at random (random.random() < 0.1). If it fails, mark it with a red ✘ and stop the deployment.


📌 Summary

Concept Example
Colour "\033[32m" … "\033[0m"
RGB colour "\033[38;2;255;50;40m"
Rewrite a line print("\r...", end="", flush=True)
Wait time.sleep(0.1)
Cycle symbols SPINNER[i % len(SPINNER)]
Random number random.random()