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()
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
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
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()
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()
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
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
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()