2017-02-26 7 views
0

ファイルをWebアプリケーションにアップロードして、ファイルの各列を最初の列の値に対してプロットできるようにします。この特定の例では、yに対して誤差をtに対してプロットし、2つの曲線を生成する必要があります。FlaskファイルをアップロードしてPythonで曲線を作成する

 
t  
0.0000 
1.0000  
1.2300  

y 
1.2345 
0.9871 
0.5545 

error 
1.4E-4 
-4.9E-3 
8.2E-3 

私は、アップロード一部が正常に動作し得ることができたが、私は、曲線をプロットしたファイルからの情報を使用することができますどのように失われています。

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF_8"> 
    <title>Uploader</title> 
</head> 
    <body> 
<h1>Upload your file</h1> 
     <form id = "upload-form" action = "{{ url_for('upload') }}" method = "POST" 
     enctype = "multipart/form-data"> 
     <input type = "file" name = "file" accept = "file/*" multiple/> 
     <input type = "submit" value ="upload"/> 
     </form> 

    </body> 
</html> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Uploaded</title> 
</head> 
<body> 
Uploaded Successfully 
</body> 
</html> 
import os 
from flask import Flask,render_template,request 
from bokeh.plotting import figure 
from bokeh.io import output_file,show 



app = Flask(__name__) 


APP_ROOT = os.path.dirname(os.path.abspath(__file__)) 

fg = figure(x_axis_label ="x",y_axis_label = "y") 
x = [1,2,3,4] 
y = [1,2,3,4] 

fg.circle(x,y) 

@app.route("/") 
def index(): 
    return render_template("view.html") 

@app.route("/upload", methods=['POST']) 
def upload(): 
    target = os.path.join(APP_ROOT,'files/') 
    print(target) 

    if not os.path.isdir(target): 
     os.mkdir(target) 

    for file in request.files.getlist("file"): 
     print(file) 
     filename = file.filename 
     destination = "/".join([target,filename]) 
     print(destination) 
     file.save(destination) 
    lines = file.read().split('\n') # a list, each element is a line 
    for line in lines: 
     x_raw, y_raw, error_raw = line.split(' ') # split the line into three, assign them to variabes 
     x, y, error = float(x_raw), float(y_raw), float(error_raw) # parse each part into a floating point number 

    return render_template("uploaded.html") 


if __name__ == "__main__": 
    app.run(debug=True) 
+7

、と注意してください。人々があなたに手伝ってくれるのに多くの時間と労力を費やしたときに質問を削除します。あなたはそれらの人々が彼らが入れている仕事の認知の機会を否定しています。 –

答えて

3

あなたは次に文字列を解析file.read()

Read file data without saving it in Flask

とそれを保存せずに文字列としてアップロードされたファイルの内容を取得することができます。あなたのファイルは、次のようにした場合:

0.0000 1.2345 1.4E-4 
1.0000 0.9871 -4.9E-3 
1.2300 0.5545 8.2E-3 

あなたは値を抽出するためにこれを使用することもできます。

lines = file.read().split('\n') # a list, each element is a line 
for line in line: 
    x_raw, y_raw, error_raw = line.split(' ') # split the line into three, assign them to variabes 
    x, y, error = float(x_raw), float(y_raw), float(error_raw) # parse each part into a floating point number 

のstring.Split()は次のように動作します:ねえ

>>> "a,b,c;d,e,f".split(';') 
['a,b,c', 'd,e,f'] 
+0

私はあなたが何をしているのかを正確にコードに実装する方法を知っています。アップロード機能のreturn文の直前ですか? – YawdMan

+0

コードを実装してファイルをアップロードすると、次のようになります。TypeError:バイトのようなオブジェクトが必要です。 'str'ではありません。 – YawdMan

+0

@YawdMan質問のコードを更新し、完全なエラースタックトレースを提供してください。 – aitchnyu

関連する問題