Learn to Program @ Home
Day 9 - Flask Tutorial
Flask
Flask is a framework for making web sites. Here is the code for the simplest website you can build with Flask:
A route is a local web address. “/” is the index route. A route like “/pricing” might be another route in some app. When this app is running, and someone goes to the index page of the app, it returns a little snippet of HTML.
Get and Post
Your first Flask app will handle two types of requests. A GET request is the kind you’re already used to. Someone clicked on a link that goes to your page.
A POST request generally comes from someone filling in a form and clicking the submit button.
Your Flask app can tell whether the request is a GET or a POST.
if request.method == “POST”:
Jinja
In the super simple app, a snippet of HTML is returned when the request is made. But more likely you’ll want to create a more complete website, maybe with a nav bar, images, and links. Jinja is a templating language that lets you create those pages using HTML, but also lets you insert variable values into the page using {{my_variable}} tags. You can even use control statements like {% if my_variable < 7 %} to control what HTML is sent to the user. Jinja uses syntax very similar to Python, but not the same.
Doing A Tutorial
To learn more about Flask and Jinja, you’ll do a tutorial. Don’t just copy the code and run it! To get the most out of a tutorial, you should type the code yourself and think about what is going on.
Remember, your Flask app is really a Python program that is running all the time, listening for GET and POST requests and responding to them.
A Quiz
To practice your new Flask skills, you’ll be making an online ‘quiz’. Maybe a “what Disney princess are you?” quiz. Or maybe a “what Hogwarts house are you sorted into” quiz.
You decide what questions are on your quiz - and you decide what algorithm your quiz will use to decide the answer.
An Algorithm
Lets say your quiz has 10 questions. The algorithm takes the answers the user gives as input, and provides the answer as output.
Answers to Questions
ALGORITHM
Result
Algorithms
One idea for an algorithm is to make a variable for each possible outcome.
Griffindor = 0
Ravenclaw = 0
Hufflepuff = 0
Slytherin = 0
Then each answer adds a point (or more) to one of these variables. At the end, whichever is the highest is the result.
Always have a result
What if there is a tie? Your algorithm should always provide some answer! Maybe ties always go to Hufflepuff. You decide!
The best quizzes don’t feel random though - they feel like your answers determine the outcome.