This project builds on the hello-flask repo but adds some super simple templates. We will just demonstrate the intruduction of them here, but will add more complex examples in other demonstrations.
Key parts
The templates directory - Flask will look for a templates directory, so the easiest way to add them is to include this directory.
In app.py:
Import render_template from flask
from flask import Flask, render_template
Then you can use render_template in your controller and pass it variables:
@app.route("/")
def index():
user = "Christina"
return render_template('index.html', user=user)
In index.html, write normal html, and where you need dynamic features use Jinja2 (in later demos we add inheritance and more). {{ ... }}
will output experssions to the browser. Use {% ... %} for Python statements:
<h1>Hello {{ user }}</h1>
{% if user == "Christina" %}
<p>You like cats!</p>
{% else %}
<p>You like dogs?</p>
{% endif %}
First create and activate some form of environment to store your dependencies. I like Anaconda:
$ conda create -n myenv python=3.7
$ conda activate myenv
Then install Flask
$ pip install Flask
$ flask run
You should now be able to see the output in your browser window (at http://127.0.0.1:5000)