top of page

How to create a web app using Flask in Python (Part 1)

Flask is a lightweight web application framework written in Python. Other examples of popular web frameworks are Ruby on Rails, Django, and Spring. Flask's no-frills, minimal design makes it a good choice for beginners to get started with web application development. Web frameworks are by no means a requirements to build a website, but they're useful for developing in any language other than JavaScript and for more complicated sites.


The first step to using Flask is to install Flask. To achieve this, make sure pip is installed. Then, run the following command on your terminal:

pip install flask

To confirm Flask has been installed, open up a new Python file called flask_web_app.py. If you haven't set up your computer to run Python code, refer to this beginner-friendly article. Then, add the following code:

import flask
print(flask.__version__)

Go ahead and run the file. If everything is set up properly, you'll see the Flask version print to the screen.


Okay, so great. What we've just done is installed the Flask module so we can use it in our Python code. Now, we're going to create a very simple web app. Backspace everything you have in flask_web_app.py and write:

from flask import Flask

app = Flask(__name__)

What the above code achieves is importing the Flask class from the flask module. These are two different things. The flask module, which we just installed, contains a capital-F Flask class which we will instantiate and use to create our web app. Next add, the following code to your file:

@app.route("/")
def home_page():
    return "<h1>This is my web app.</h1>"

The first line adds the route decorator to let Flask know for which page the associated Python function should be triggered. Since we've only added a single forward slash, we're telling Flask that this function is for the home page. Inside the function, we're returning the content to be displayed in the browser. This is HTML by default.


Great, at this point you should have the following code in flask_web_app.py:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home_page():
    return "<h1>This is my web app.</h1>"

All that is left at this point is to go to the command line and run your newly-created Flask web app. To do this, make sure to navigate to the directory your Python file is located, and execute the following command:

flask --app flask_web_app run

In the command line output, Flask will provide a test server for you to view and interact with your web app. It should be something like http://127.0.0.1:5000. Simply paste that address into your browser like any other website. If everything worked properly, you should see the web app you've created.

Since that was so easy, you're probably eager to learn how to create much more complicated and exciting web apps with Flask. And to that, I say: "cool your jets". You need to learn to crawl before you can learn to walk! And anyway, didn't you see the "Part 1" in the title??? More is coming. Stay tuned. In the meantime, sign up for some coding lessons on CodeConda.


Comments


bottom of page