Making Websites with Flask/Getting Started

Installation
Flask is a Python library, so you first of course need Python installed. Then, you can use the pip package manager to install Flask: To make sure you installed Flask correctly, run the following Python script: If it runs without any errors, you have successfully installed Flask!

Hello World!
In programming, it is a tradition to make a program that displays "Hello World!". So, we will make a website that returns "Hello World!" when we visit it.

In your code editor, write the following code: Now, save the Python code and run it as you would any other Python program. Then, when you visit localhost:7070, you should "Hello World!".

Code Breakdown
Now, let's break down the code that we just wrote line by line.

First of all, we need to import the things we need from the Flask module (line 1): Then, we need to create an instance of the  object, which represents our web app (line 3): Then, we create a decorator. Decorators are functions that modify the next function. In this case, the decorator shows the user whatever is returned by the next function when the user visits the root page (line 5): Then, we actually create the function which will be modified by the decorator and make it return "Hello World!" (lines 5 and 6): We could also make the function return some HTML: Then, we run the Flask object at port 7070 (lines 9 and 10):

Adding More Routes
Of course, there's nothing stopping us from having more routes. For example, let's add the following code: For the sake of completeness, here's the entire code with the new code block: Now, whenever we visit localhost:7070/about, we see "This is my first Flask website" in headings (notice that we added HTML tags to the output of the function).