2014-10-23 1 views
2

플라스크에서 자바 스크립트로 데이터 (정확하게 경로 또는 URL)를 전달하려고합니다. 하지만 그것은 내 변수를 인식하지 못합니다. 서버 쪽에서 : 파일을 업로드하고이 파일을 처리 한 다음 많은 이미지를 만들어 저장합니다. three.js 라이브러리를 사용하는 javascript 파일에 경로를 전달하려고하면 그렇지 않습니다.Python (플라스크 프레임 워크)에서 자바 스크립트로 데이터 전달

@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; 
      } 
+1

필요한 데이터를 txt 파일에 저장 한 다음 Ajax 호출을 통해 액세스 해 보았습니까? 그것은 약간이지만, 다른 것은 효과가 없다면 적어도 우리가 알고있는 것입니다. 또한 플라스크에는 장고와 같은 스크립팅 기능이있어 뷰에서 서버 측 변수를 인쇄 할 수 있습니까? –

답변

2

자바 스크립트는 템플릿 파일에 있어야하며 포함되지 않은 스크립트 소스가 있어야합니다. 가 포함 된 경우

, 당신은 함수 또는 "클래스"를 가지고 있는지 확인하고 다음과 같이 사용할 수 있습니다 :

(function($) { 
    var texture = new LoadTexture(
       '#filter_form', 
       {{ cube_faces | tojson | safe }} 
      ); 
    })(jQuery); 

당신이/cube_fase을 통과해야 할 수 JSON을 가지고 있으며, Jinja2에 안전에 필터링 할 수 있습니다.

관련 문제