Flask:
We can return multiple types of responses using flask like HTTP Responses , XML responses and Templates responses.
Consider the use case like you wanted to save html pages which are dynamically rendered
Packages required:
1. jinja2
2. Flask
Create a Folder structure:
Template
|- image
|- fonts
|- js
index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>{{name}}</h1>
<p>{{address}}</p>
</body>
</html>
code:
#imports
from jinja2 import Environment, select_autoescape, FileSystemLoader
#fetch the index.html file using the FilesystemLoader
path = os.path.dirname(os.path.abspath(__file__)) |
env = Environment(loader=FileSystemLoader(path), |
autoescape=select_autoescape(('html', 'xml')), |
trim_blocks=True) |
path = os.path.join("/".join(path.split('/')[:-1]), "Template") | |||||
template = env.get_template('index.html')
# Apply the dynamic content
template.stream(name="google",address="www").dump(path + '/dynamic.html') |
This .dump fucntion will save the dynamically rendered html page to give path
Comments
Post a Comment