Example 3 - Rainbow Flask

Follow the same steps as the first example, but instead use this code:

import random
from flask import Flask

app = Flask(__name__)

def random_color():
    return "#{:06x}".format(random.randint(0, 0xFFFFFF))

def rainbow():
    return f"""
        body {{
            background-color: {random_color()}
    <p>Rainbow Flask</p>

Here, we import the random module, and we define a function that returns a random hex color.

This route also contains a bit of HTML, something the previous examples have very little of, because we want to style the background an arbitrary color, this will be covered in more detail later, but for now, if you run this, you will get a page that each time you refresh, will display a piece of text like before, but with a random colorful background!