Initial set-up for Flask

To run a Flask application on Educative, we do not need to set up anything. All code will run inside the special environment within the browser. It’s as easy as that!

Educative is designed in such a way that we do not get distracted by dependencies and get right into what is essential: the learning.

Writing our first application

The simplest Flask application can be made using only one script! Let us call this file app.py. We will break down the program into steps and discuss one.

Step 1: Importing modules

For this application, we only need the Flask module from the flask package. So let’s import that first.

from flask import Flask

Step 2: Creating a Flask object

We need to make an object with the imported Flask module. This object will be our WSGI application called app. As discussed before, the WSGI aspect of the application is taken care of by the Flask module.

app = Flask(__name__)

Step 3: Run the application in main

To run our application, we need to call the run() function of our application object.

if __name__ == "__main__":
    app.run()

The run() function has some optional parameters. For complete documentation, refer here.

Step 4: Create a view function

Before we run the application, we need to tell the application to show something as output in the browser window. Thus, we create a function called hello() which returns the string "Hello World!". The output returned from this function will be shown in the browser.

def hello():
    return "Hello World!"; 

Step 5: Assign a URL route

Finally, we need to tell the Flask app when to call the view function hello(). For this purpose, we will create a URL route. A URL route is associated with each view function. This association is created by using the route() decorator before each view function.

@app.route("/") 
def hello():
    return "Hello World!"; 

Complete implementation

The following program shows the complete implementation of a “Hello World” application in Flask!

📌 Note: In the run() function below we have specified debug = True, host = "0.0.0.0" and port = 3000. Due to the platform configurations, we will be using these same values for host and port.

Create a free account to view this lesson.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy