Wednesday, May 15, 2019

Python - Flask Templating


Python Flask is a web framework and widely used in the application where we require web pages to display our content.

In Flask Introduction , we have learnt how to create a simple Flask application.In this we will try to learn how to create a Flask application through the help of the template.

Templating is like connecting Python flask with HTML.

We need to create a folder called templates mainly in the path where we are keeping our code flask code.

We have designed a simple index.html code for templating purpose

<html>
<p>Welcome to the index page </p>
</html>

Write the above code and save it with the name of index.html


Code Snipppet :

from flask import Flask, render_template

app =Flask(__name__)

@app.route('/')

def index():
return render_template('index.html')

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


This will be our main code and and we have imported render_template whch will be used to call our template file.

def index():
return render_template('index.html')

The index function will have a return type and here we are having index.html .We can replace it with any html file which we want to display.

When we run the code , we will get the below as our output :-



https://github.com/sangam92/python_tutorials

No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...