Как добавить динамичное количество строк tr в Flask?
Подскажите пожалуйста, как в зависимости от количества имеющих строк в файле - добавлять в шаблоне?
main.py:
with open("ping.txt", "r") as file1:
notavailable = (file1.read())
print(notavailable)
app = Flask(__name__)
@app.route('/', methods=["POST", "GET"])
def index():
return render_template("index.html", noavailable=notavailable)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8080)
index.py:
{% extends 'base.html' %}
{% block title %}
Главная страница
{% endblock %}
{% block body %}
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Доступность комплекса: </th>
</tr>
</thead>
<tr>
<td>{{noavailable}}</td>
</tr>
</table>
{% endblock %}
notavailable в консоле выводится построчно, а на странице все в одной строке содержится(
Ответы (1 шт):
Автор решения: Sinelnikov Alexander
→ Ссылка
Можно сделать так:
main.py
from flask import Flask, render_template
with open("ping.txt", "r") as file1:
notavailable = (file1.read())
print(notavailable)
app = Flask(__name__)
@app.route('/', methods=["POST", "GET"])
def index():
return render_template("index.html", notavailable=notavailable.splitlines())
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8080)
index.html
{% extends 'base.html' %}
{% block title %}
Главная страница
{% endblock %}
{% block body %}
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Доступность комплекса: </th>
</tr>
</thead>
{% for line in notavailable %}
<tr>
<td>{{ line }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
P.s.
Добавил .splitlines(), когда передаю содержимое файла в render_template(), и for по строкам в index.html.