Flask - (4)

Shipping context to the client side.

2019-12-28
by cS


Passing a single, or multiple values to the template is done by passing keyword arguments to the render_template() function.

@app.route("/", methods=("GET",))
def index():
    stuff = {
        "location" : "Uranus",
        "unit" : "NVM-1999",
        "AO" : "Blue Fields",
        "operating_assets" : 13,
        "not_ok" : 0,
    }
    return render_template("home.html", stuff=stuff)

home.html may look like this.

{% extends 'index.html' %}
{% block content %}
    <h3>Drone units status</h3>
    <p> Unit Location {{ stuff.location }} </p>
    <p> Unit Name {{ stuff.unit }} </p>
    <p> Area of Operation {{ stuff.AO }} </p>
    <p> Unit Count {{ stuff.operating_assets }} </p>
    <p> Unit Casualties {{ stuff.not_ok }} </p>
    <style>
        body{ color: white; background: black;}
    </style>
{% endblock %}