Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

553
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda