Adsense Ad

Monday, 23 March 2020

Python Flask: Calling a html page using FLASK

Templates are required for any web app. Therefore I create a folders in my Flask project named as "Templates" in which I will save all templates in .html file format. Also I used Sublime Text 3 to work on my Flask web. Sublime Text 3 is a very powerful and user friendly tool for development.

Now I create a "index.html" file in my templates folder with following code:
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
 <h1>Hello! My Name Is Hasan Jawaid</h1>
          <p>This my first page</p>
            <p>Using Flask - Python</p>
</body>
</html>

Now I create a .py file using my PYCHARM IDE with following code:

from flask import Flask, escape, request, render_template

app = Flask(__name__)

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

app.run(debug=True)

Above code will call the hello() function which returns me index.html page. First render_template must be import from flask library in order to enable calling of .html page.


No comments: