Creating a Sanic Web App with Jinja2 Templates

  • Share this:

Code introduction


This function creates a simple Sanic web application that uses the Jinja2 template engine to render HTML pages.


Technology Stack : Sanic, Jinja2

Code Type : Web Application

Code Difficulty : Intermediate


                
                    
def random_sanic_function():
    import random
    from sanic import Sanic, response
    from sanic_jinja2 import SanicJinja2

    app = Sanic(name='Random Sanic App')
    jinja = SanicJinja2(app)

    @app.route('/')
    async def index(request):
        return await jinja.render_async('index.html', request, title="Home Page")

    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=8000)                
              
Tags: