Create Your First Application In Flask

Create Your First Application In Flask

Creating a web application in Flask is actually a lot more simple than people make it out to be. To give you the gist of it, you have a folder called "templates", this is where you will house all of your HTML files. Then you have a single python file, "app.py". All that python file is doing is taking you from 1 page to the other using routes. Now what do you mean by routes? Well routes are like the path to the HTML file you just created in your "templates" folder. Now, with that out of the way, let's get into creating your first Flask web application.


Creating Files

Before we can start coding, we first need to set up our files and folders. First go ahead and create a folder with the name of your project.

mkdir myProject

Then create a file called "app.py" in your project folder like this:

cd myProject
touch app.py

Almost done. Now create a folder called "templates" and go into it. After that create a file called "index.html" like this

mkdir templates
cd templates
touch index.html


Starting To Code

Now that we have all of our files created, we can begin developing our application. Go ahead and open this project up in your favorite code editor (If you are looking for ways to set up your code editor check out my last post!!).


I want you to open your HTML file from the templates folder and just add some boiler plate code in there with a title and p1 tags like this:

<!doctype html>
<html>
    <head>
        <title>Flask App Tutorial</title>
    </head>
    <body>
        <p1>Hello, World</p1>
    </body>
</html>


Now open your app.py file and lets first add a import statement like this: This allows you to use Flask and some of the other important libraries we need to create a web application.

from flask import Flask, requests, render_template


Once we have imported these libraries, we now need to create a variable called "app" which we will call at the end of the program to run our app on the server. It looks like this:

app = Flask('app')


The next step is to now add a route so that we can access all the different templates or html files we created in the templates folder. routes are created by first adding a "@" symbol. Then a function and in that function we return a render_template with that html file.

@app.route('/')
def first_route:
    return render_template("index.html")


Finally, we need to call the "app" variable we created earlier and add a run function to it and specify the host and port number and that looks like this:

app.run(host="0.0.0.0", port=8080)


Now go ahead into your terminal and run python3 app.py and enjoy your new flask web application!!

python3 app.py


I really hope you guys found this helpful and if you did, hit the thumbs up and subscribe to my mailing list so you are the first to know when I put out more cool content like this!! Also major thank you to all who have supported me throughout this whole journey on Hashnode and for getting me to over 100 views on my blog!! Always appreciate the support!! Peace!!




Github: github.com/rohankewal
Instagram: @rohankewal