Skip to main content

HTML Generator

Updated Oct 28, 2019 ·

Problem

You want to generate HTML dynamically using a Python script.

So far, you’ve created two decorators that wrap a function’s return value with bold and italic HTML tags. However, both decorators follow almost the same structure.

def bold(func):
@wraps(func)
def wrapper(*args, **kwargs):
msg = func(*args, **kwargs)
return '<b>{}</b>'.format(msg)
return wrapper

def italics(func):
@wraps(func)
def wrapper(*args, **kwargs):
msg = func(*args, **kwargs)
return '<i>{}</i>'.format(msg)
return wrapper

Instead of repeating similar code for every HTML tag, use a single decorator that can accept any pair of opening and closing tags.

Solution

Create a reusable decorator, html(), that takes HTML tags as parameters and applies them to the function output.

The full code can be found here: Github repo