Skip to content

ChristinaVoss/flask-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flask Templates

App structure

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.

Screenshot 2022-05-12 at 09 37 39

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 %}

Setup

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

Run the app

$ flask run

You should now be able to see the output in your browser window (at http://127.0.0.1:5000)

Screenshot of browser with app running

Screenshot 2022-05-12 at 17 59 56

Releases

No releases published

Packages

No packages published