Learnmonkey Learnmonkey Logo
× Warning:

The tutorial you are reading is currently a work in-progress.

Your First Flask Website

Writing the Code

The code will be as follows:


from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello, World!'

app.run(port=8080)
    

Running the Code

We can run the code like we run a normal Python program. Then, you should be able to go to localhost:8080 and see Hello World! in the browser window.

Code Breakdown

Now, it's time for a line by line breakdown of the code.

from flask import Flask tells Python that we want to import the Flask object from the Flask module.

app = Flask(__name__) tells Flask to make a Flask app. This will be the object that represents our entire web app.

@app.route('/') is a decorator. Decorator changes the function of the next function. In this case, it modifies the next function to be returned whenever we visit the home page of our website.

def hello_world(): defines a new function called hello_world that takes on the decorator.

return 'Hello, World!' means that when we go to the home page of the website, we see Hello, World!.

app.run(port=8080) tells Flask to actually run our website and put it on port 8080. Whenever you make web servers, they are hosted by default on localhost, so we can now access our page on localhost:8080.