我尝试将数据(确切地说是路径或网址)从 Flask 传递到 JavaScript。但它无法识别我的变量。
在服务器端:它上传一个文件,处理该文件,然后创建并保存许多图像。当我尝试将路径传递给使用 Three.js 库的 javascript 文件时,它不会。
@app.route('/upload', methods=['POST'])
def upload_file():
print('coucou')
if request.method == 'POST':
file = request.files['file']
print(file)
# if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
path = UPLOAD_FOLDER + filename
fits_reader = FitsReader.FitsReader()
fits_reader.open_file(path)
cube_faces = ['/home/morban/essaiserver/cube3D/CubeX.PNG', '/home/morban/essaiserver/cube3D/CubeY.PNG', '/home/morban/essaiserver/cube3D/CubeZ.PNG']
return render_template('index.html', cube_faces=cube_faces)
for (var i = 0; i < 3; i++) {
switch (i) {
case 0:
texture[i] = new THREE.ImageUtils.loadTexture('{{ cube_faces[0] }}');
console.log('{{ cube_faces[0] }}')
break;
case 1:
texture[i] = new THREE.ImageUtils.loadTexture('{{ cube_faces[1] }}');
break;
case 2:
texture[i] = new THREE.ImageUtils.loadTexture('{{ cube_faces[2] }}');
break;
}
javascript 必须位于模板文件中,并且不包含脚本源。
如果包含,您可以使其具有函数或“类”并像这样使用它:
(function($) {
var texture = new LoadTexture(
'#filter_form',
{{ cube_faces | tojson | safe }}
);
})(jQuery);
你可以/应该传递cube_fase有json并将其过滤到Jinja2上的安全位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)