Your First Flask Website 12


Why not making a website before you even learn how to make a website? Just something as simple as this:

basic flask website in python

Yes, it’s just a page with some plain text, but it is still called a website and that’s how it starts. Python is concise so it takes only seven lines of code to build what you see in the screenshot. Let’s do it.

Insert the following lines of Python code inside an empty text file, and name the  file something like hello.py.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hey there!"

if __name__ == '__main__':
    app.run(debug=True)

As you can see we are importing the flask library in the first line. If you don’t have that library installed, you will get an error. To install flask, simply type in pip install flask in your computer terminal/command line. Once you have made sure flask is installed, simply run the hello.py script.

Once you run the script, the website should  now be up and running on your local machine and it can be viewed by visiting localhost:5000 in your browser. That’s a good start.

Let’s move on!


Navigation


Leave a comment

Your email address will not be published. Required fields are marked *

12 thoughts on “Your First Flask Website