flask.templating render_template_string Example Code

render_template_string is a Flask function from the flask.templating package. render_template is used to generate output from a string that is passed in rather than from a file in the templates folder.

Note that render_template_string is sometimes imported from the flask package instead of from flask.templating. It is the same function that is imported, but there are less characters to type when you leave off the .templating part.

render_template is another callable from the flask.templating package with code examples.

You should read up on these subjects along with these render_template_string examples:

Example 1 from Flask-User

Flask-User (PyPI information and project documentation) is a Flask extension that makes it easier to add custom user account management and authentication to the projects you are building. The extension supports persistent data storage through both relational databases and MongoDB. The project is provided as open source under the MIT license.

Flask-User / flask_user / tests / tst_app.py

# tst_app.py
import os
import datetime
from flask import Flask, render_template_string, request
from flask_babelex import Babel
from flask_user import login_required, roles_required, UserManager, UserMixin

ORM_type = 'SQLAlchemy'   # SQLAlchemy  or MongoEngine

app = Flask(__name__)

class ConfigClass(object):
    SECRET_KEY = 'Test with short key'  # Less than 32 bytes

    SQLALCHEMY_DATABASE_URI = 'sqlite:///tst_app.sqlite'    # File-based SQL database
    SQLALCHEMY_TRACK_MODIFICATIONS = False    # Avoids SQLAlchemy warning

        'db': 'tst_app',
        'host': 'mongodb://localhost:27017/tst_app'

    MAIL_USERNAME =           os.getenv('MAIL_USERNAME',        '[email protected]')
    MAIL_PASSWORD =           os.getenv('MAIL_PASSWORD',        'password')
    MAIL_DEFAULT_SENDER =     os.getenv('MAIL_DEFAULT_SENDER',  '"TestApp" <[email protected]>')
    MAIL_SERVER =             os.getenv('MAIL_SERVER',          'smtp.gmail.com')
    MAIL_PORT =           int(os.getenv('MAIL_PORT',            '465'))

## ... source file abbreviated to get to render_template_string examples ...

    assert user_manager.USER_EMAIL_SENDER_EMAIL=='[email protected]'

    db_manager = user_manager.db_manager

    token = user_manager.generate_token('abc', 123, 'xyz')
    data_items = user_manager.token_manager.verify_token(token, 3600)
    assert data_items is not None
    assert data_items[0] == 'abc'
    assert data_items[1] == 123
    assert data_items[2] == 'xyz'

    user = db_manager.add_user(username='member', email='[email protected]',
            password=user_manager.hash_password('Password1'), email_confirmed_at=datetime.datetime.utcnow())

    user = db_manager.add_user(username='user007', email='[email protected]',
    db_manager.add_user_role(user, 'secret')
    db_manager.add_user_role(user, 'agent')

    def home_page():
        return render_template_string("""
            {% extends "flask_user_layout.html" %}
            {% block content %}
            <h2>{%trans%}Home Page{%endtrans%}</h2>
            <p><a href="{{ url_for('user.login') }}">{%trans%}Sign in{%endtrans%}</a></p>
            {% endblock %}
            {% extends "flask_user_layout.html" %}
            {% block content %}
            <h2>{%trans%}Profile Page{%endtrans%}</h2>
            <p> {%trans%}Hello{%endtrans%}
                {{ current_user.username or current_user.email }},</p>
            <p> <a href="{{ url_for('user.change_username') }}">
                {%trans%}Change username{%endtrans%}</a></p>
            <p> <a href="{{ url_for('user.change_password') }}">
                {%trans%}Change password{%endtrans%}</a></p>
            <p> <a href="{{ url_for('user.logout') }}?next={{ url_for('user.login') }}">
                {%trans%}Sign out{%endtrans%}</a></p>
            {% endblock %}
            {% extends "flask_user_layout.html" %}
            {% block content %}
            <h2>{%trans%}Admin Page{%endtrans%}</h2>
            {% endblock %}

## ... source file continues with no further render_template_string examples...

Example 2 from Datadog Flask Example App

The Datadog Flask example app contains many examples of the Flask core functions available to a developer using the web framework.

Datadog Flask Example App / python/flask/app / blueprint.py

# blueprint.py
from ddtrace import Pin
from flask import abort, Blueprint, render_template_string

from .limiter import limiter

bp = Blueprint('bp', __name__, url_prefix='/bp/')

Pin.override(bp, service='flask-bp', app='flask', app_type='web')

def bp_before_request():
    print('Hook: bp_before_request')

def bp_before_app_request():
    print('Hook: bp_before_app_request')

def bp_before_app_first_request():
    print('Hook: bp_before_app_first_request')

## ... source file abbreviated to get to render_template_string examples ...

    print('Hook: bp_after_request')
    return response

def bp_after_app_request(response):
    print('Hook: bp_after_app_request')
    return response

def bp_teardown_request(response):
    print('Hook: bp_teardown_request')
    return response

def bp_teardown_app_request(response):
    print('Hook: bp_teardown_app_request')
    return response

@limiter.limit('10 per second')
def index():
    return render_template_string('<h1>Blueprint</h1>')

def unknown():

def bp_not_found(e):
    return 'oh no....', 404

## ... source file continues with no further render_template_string examples...

Sponsored By

Sentry logo

Software errors are inevitable. Chaos is not. Try Sentry for free.

AssemblyAI logo

The most accurate speech-to-text API. Built for Python developers.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2021