Skip to main content

Save Rendered Templates in Flask

 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)
template = env.get_template('index.html')
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

Popular posts from this blog

Swagger set up for FLASK

 Flask     A Micro framework , where everything is customized, if we want to use restful api's we can add flask-restful library, or we wanted to go by rest plus we can  use rest plus library   After writing the all api's we need something to test  the api's and it should contain all the required parameters like schema , for these things we can use swagger  project structure should be add service __init__.py below code for swagger load app = Flask(__name__ , instance_relative_config = True ) CORS(app) # swagger specific SWAGGER_URL = '/swagger' API_URL = '/static/swagger.yml' SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint( SWAGGER_URL , API_URL , config ={ 'app_name' : "service Name" } ) add the blue print app.register_blueprint(SWGGER_BLUPRINT, url_prefix=SWAGGER_URL)   Start the service python app.py  on the default port 5000 it will load and endpoint /swagger 127.0.0.1:5000/swagger    

CommandLine Args In Python

 Command line args in Python :      When the program comes to Python , it's all about libraries .Argparser is the library used for the taking inputs from the command line. Here you can validate the arguments which are entered by the user without writing the another function to validate from the if conditions. suppose give a filename like cmdprocess.py You can give the expecting arguments like below cmdprocess.py --help      it will give the total arguments with respective commands to represent Required =True :           When the required is true. You have to enter the argument.   Type = function name:          Type is very useful to validate the input entered by the user. so that you can send a message like user need to enter the defined arguments only. This function always expects the input argument . Custom-Exceptions In Python:    There are multiple exception...

Making a Geographic Heatmap with Python

      We have a requirement like need to create a map where user is traveled.There are many libraries available in python to draw a map if you have list of co-ordinates with you like(longitude and latitude)  To name few packages : vincent, bokeh, plot.ly and matplotlib and folium   Things to consider while choosing the library: 1. Open source 2. Painless to use 3. built in support for heat-maps 4. actively maintained Folium is the lib which will satisfy above all use cases Requirements: pip install folium pip install geopands (for adding other geo json files) pip install pands (to read csv of excel files which are having the co-ordinates data) Creating the map: import pandas as pd import folium from folium.plugins import HeatMap map_df = pd.read_csv('map.csv', sep='\t') max_amount = float(map_df['Amount'].max()) hmap = folium.Map(location=[42.5, -75.5], zoom_start=7, ) hm_wide = HeatMap( list(zip(map_df.lat.values,         ...