Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to show python data in my HTML and the output is not what I expected

Here’s a function in my application that returns a number from a database and I’m trying to display it on my webpage using flask

def handler():
    increment_visitor()
    return retrieve_visitor_count()
    
@app.route('/')
def home():
    handler()
    return render_template("index.html", count=handler)

I assign the name count to handler and try displaying count in my index.html file like so:

<p>{{count}}</p>

When I load my webpage, here’s what the output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<function handler at 0x7f8c70069a60>

When I print my handler function, it outputs the appropriate number, so how do I get that number to display on my webpage correctly?

>Solution :

count is handler, which is a function. Add parentheses to call it in the template like so:

{{count()}}

Looking at your logic though, I think you may want something like this:

@app.route('/')
def home():
    result = handler()
    return render_template("index.html", count=result)

Otherwise you would presumably increment the visitor count twice

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading