Is there a flask function called @make_bold?? I am very confused as I am not able to find the answer anywhere

Viewed 261

2 Days ago, I ran into a very unusual error regarding Python Flask. I was simply coding along when I ran into an error which confused me. I was doing a Python Course which showed that we can make our code easier to read if we add some decorators to the code. When I added the same decorator as the one shown in the video, I ran into an error which said

Name error: Name @make_bold is not defined 

Here's the code


app = Flask(__name__)

@app.route('/')
def func():
    return 'Hey'

@app.route("/bye")
@make_bold
@make_emphasis
@make_underlined
def func1():
    return 'Bye'

if __name__ == "__main__":
    app.run(debug=True)
1 Answers

Flask is a web server, meaning it can provide webpages or other info to a browser, but that doesn't mean it is designed to create webpages. In the long run you'll be much better off having Flask return a proper HTML document, but in the mean time, try including the HTML tags required for making the text bold within the actual text, since I presume that that is what the decorators (which don't appear to be built-in) are doing under the hood.

@app.route("/bye")
def func1():
    return '<b><i><u>Bye</u></i></b>'

Another option could be to create the decorators yourself.

def make_bold(func):
    def wrapper(*args, **kwargs):
        return '<b>' + func(*args, **kwargs) + '</b>'
    return wrapper

@app.route("/bye")
@make_bold
def func1():
    return 'Bye'
Related