Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

542
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda