python - 模态视图中的计算繁重方法
问题描述
我有一个简单的Person
类,它有一个name
属性和一些计算量大的方法,称为computationally_heavy_method
:
class Person:
def __init__(self, name: str):
self.name = name
def __repr__(self) -> str:
return self.name
def computationally_heavy_method(self) -> str:
# do lots of stuff and then return the result.
return ''
我有很多实例Person
:
people = [Person(name='Alex')] * 1000
我将其传递给 jinja 模板,如下所示:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', people=people)
index.html 文件看起来像这样:
<!DOCTYPE html>
<html>
<body>
{% for person in people %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">
{{ person | safe }}
</button>
<div class="modal fade bd-example-modal-lg" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myLargeModalLabel">{{ person }}</h5>
</div>
<div class="modal-body" style="background-color: lightblue;">
{{ person.computationally_heavy_method() }}
</div>
</div>
</div>
</div>
{% endfor %}
</body>
<html>
所以基本上我有一个代表人的按钮列表,当你点击它时会显示一个模式视图。
但是,由于对computationally_heavy_method
方法的重复调用,模态视图的计算量非常大。
有没有办法在需要时(即实际按下按钮时)而不是为所有 1000 个Person
实例制作模态视图?
感谢您在这里的任何帮助。
解决方案
推荐阅读
- django - 无法显示媒体文件 Django
- python - 每个 n 元素划分一个 Python 列表
- ios - 在设备上使用播客应用程序时,MPMusicPlayerController 未启动(在前台)
- django - 迭代时首先显示某些类别
- amazon-web-services - 将 SSL 添加到使用 aws eb cli 构建的 Web 应用程序
- html - 如果不将它们包围在 div 中,如何将兄弟元素固定到粘性元素的底部,以便它们都充当单个粘性元素?
- php - 未定义类型“Dotenv\Dotenv”。intelephense(1009)
- python - 不能使用美丽的汤只打印文本
- assembly - 查找 MIPS 系统调用的操作码和功能码
- teradata - 在 SQL 助手中输出列名而不是描述