Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

541
Views
How to return render_template in Flask?

The following code works perfectly from the .py file but I want to separate the HTML and put it in templates/index.html.

I suppose I have to use the render_template function in Flask to be able to return the same results.

# File dynamic_website.py
from owlready2 import *
onto = get_ontology("bacteria.owl").load()
 
from flask import Flask, url_for
app = Flask(__name__)
 
@app.route('/')
def ontology_page():
    html  = """<html><body>"""
    html += """<h2>'%s' ontology</h2>""" % onto.base_iri
    html += """<h3>Root classes</h3>"""
    for Class in Thing.subclasses():
        html += """<p><a href="%s">%s</a></p>""" % (url_for("class_page", iri = Class.iri), Class.name)
      
    html += """</body></html>"""
    return html

I created a folder template and a file index.html. I used return render_template('index.html') but it doesn't work. What arguments do I have to add to the return_template function? "for Class in Thing.subclasses():" have to be in the .html file or .py file? What about the url_for function?

If you could edit the .py code and let me know what should I write exactly in the index.html file to have the same results it would be great.

UPDATE:

What I have done:

Python code

from flask import Flask, render_template
from owlready2 import *
from flask import Flask, url_for

onto = get_ontology("bacteria.owl").load()

app = Flask(__name__)

@app.route("/")


def ontology_page():
    for Class in Thing.subclasses():

        return render_template('index.html')

Html code

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>{{ Class.name }}</h1>
    
  </body>
</html>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can't return a function multiple times. Whatever is returned, is the value of the function. This tutorial is in JS, but it implies the same concept as python does.

If you want the user to see a list of things on the html, do this. render_template('index.html', things=Thing.Subclasses()) This will give Jinja a list, where it can then for loop.

For html you can do this {% for s in things %} {{ s }} is something {% endfor %}. Do anything you want with the s though, s is one subclass from the list.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!