How to annotate a function in python that returning an html file

Question:

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()
Asked By: James Scott

||

Answers:

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
Answered By: rv.kvetch

That’s not how annotations work
-> is a syntax 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

Answered By: The Myth

I think Html comes under File catogary which is object so right thing to do is.

@app.route('/entry')
def entry_page() -> object:
    return render_template('entry.html',
                           the_title='Welcome to searchtools on the web!')
app.run()
Answered By: Bhargav
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.