Azad Rasul
SmartRS

Follow

SmartRS

Follow

4- Templates in Flask

Azad Rasul's photo
Azad Rasul
·Jan 3, 2023·

1 min read

Play this article

Each template contains HTML which is the standard language of the web. The template files will be stored in the templates directory inside the project folder. Flask uses the Jinja template library to render templates.

Firstly, create a directory for your project templates using cmd:

(venv) mkdir C:\Users\gardi\flask-tutorial\templates

For example, let's create an HTML file and write Hello World of Programming! inside it in blue color:

<html>
    <body>
        <h1 style='color:blue'>Hello World of Programming!</h1>
    </body>
</html>

Now let's create a python script and save it as hello1.py.

At the beginning of the script we have to import Flask and render_template:

from flask import Flask, render_template

Then create app:

app = Flask(__name__)

We define route hello to return hello.html template using render_template:

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

Finally, we run the app:

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

Inside cmd run the Python script with command:

python hello1.py

Open your localhost:

http://localhost:5000/

A web page like the following should be opened:

* If you like the content, please SUBSCRIBE to my channel for the future content

 
Share this