由于某些原因,当我在Flask中使用带模板的子模板时,我的HTML内容将呈现为重复内容。我正在使用Bootstrap和Bokeh进行图形可视化。我的Dashboard.html子模板中的每个div都使用重复项进行渲染。有谁知道为什么会这样?我是否错误地设置了模板?
以下是我的layout.html文件:
的layout.html
<!doctype html>
<html lang="en">
<head>
<link type="text/css" href="index.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.css" type="text/css" />
<script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.js"></script>
<title>Embed Demo</title>
</head>
<body>
{% block body %}{% endblock %}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
这是我的孩子模板文件:
Dashboard.html
{% extends "layout.html" %}
{% block body %}
{% for plot in plots %}
{% for part in plot %}
<div class="container-fluid">
<div class="row">
<div class="col" id="graphblock">
{{part | safe}}
</div>
<div class="col" id="graphblock">
{{part | safe}}
</div>
</div>
</div>
{% endfor %}
{% endfor %}
{% endblock %}
这是我的Flask服务器:
Server.py
import random
from flask import Flask, render_template
from bokeh.plotting import figure
from bokeh.embed import components
app = Flask(__name__, static_folder='../static/dist', template_folder='../static/client')
@app.route('/dashboard/')
def show_dashboard():
plots = []
plots.append(make_plot())
return render_template('dashboard.html', plots=plots)
def make_plot():
plot = figure(plot_height=300, sizing_mode='scale_width')
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [2**v for v in x]
plot.line(x, y, line_width=4)
script, div = components(plot)
return script, div
if __name__ == '__main__':
app.run(debug=True)
答案 0 :(得分:1)
您的模板循环不正确。 plot
列表中的plots
类似于:(script, div)
。你遍历plot
中的所有plots
,但是你也会这样做:
{% for part in plot %}
<div class="container-fluid">
<div class="row">
<div class="col" id="graphblock">
{{part | safe}}
</div>
<div class="col" id="graphblock">
{{part | safe}}
</div>
</div>
</div>
{% endfor %}
部件的第一次迭代包括脚本两次,然后下一次迭代包括div两次。这会导致事情重复。