Render HTML file in Flask

In the last tutorial, we studied about the routers in Flask. If you are a new visitor, kindly check the Flask series and follow all the tutorials.

In this tutorial, we are going to learn to template in Flask and learn how to render HTML templates.

Step 1:

First, create a new folder in the project directory called templates. Create a new file in the templates folder naming “home.html”.

Copy/paste this simple code.

<h1>Welcome to the HomePage!</h1>

Step 2:

Now open and add the following code.

from flask import Flask, render_template
app = Flask(__name__)

def home():
   return render_template('home.html')
if __name__ == '__main__':

We are importing render_template function provided by the Flask and then rendering our HTML template in the home route.

Run the app using the command.

flask run

Render HTML file in Flask

Now, navigate your browser to localhost:5000 to see the output.

Render HTML file in Flask

Awesome! You have just rendered an HTML template in the Flask.

In the next tutorial, we will perform the dynamic rendering using Jinja templating engine.


Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Articles: 298