You can download this code by clicking the button below.
This code is now available for download.
This function creates a Sanic web application that can generate a specified number of random user data, which is then rendered into a Jinja template. User data includes name and age.
Technology Stack : Sanic, Jinja2, random, json
Code Type : Web API
Code Difficulty : Intermediate
def random_user_data(num_users):
from sanic import Sanic, response
from sanic_jinja2 import SanicJinja2
import random
import json
app = Sanic(name="RandomUserDataApp")
jinja = SanicJinja2(app, loader=app.jinja_loader)
@app.route('/random_data')
async def random_data(request):
users = [{'name': f'User {i}', 'age': random.randint(18, 65)} for i in range(num_users)]
return await jinja.render_async('random_user_data.html', users=users)
return app