Home > Software design >  How to annotate a function in python that returning an html file
How to annotate a function in python that returning an html file

Time:11-02

Here is my code I also tried it without quotes but it gives me ('html' is not defined) error

@app.route('/entry')
def entry_page() -> 'html':
    return render_template('entry.html',
                           the_title='Welcome to searchtools on the web!')
app.run()

CodePudding user response:

You can really set anything, but in these scenarios I find it useful to add a comment as a return type. In my case PyCharm also seems to understand that i'm adding a comment and it removes the warnings, as it knows that is indeed fine to do.

Eaxmple:

def entry_page() -> 'some kind of valid html':
    return ...  # something here

CodePudding user response:

That's not how annotations work -> is a sytnax meant to mention the type of data being returned. To annotate functions there are various methods.

Method 1: Commenting (also known as single line comment)

def function(a): # Annotation
    return a

Method 2: String Literal (also known as multi line or docstring comment)

def function(a):
    """
    Returns the value passed
    """
    return a

Method 1 is recommend for small comments (for your case) Method 2 is recommended for big comments

  • Related