2016-11-09 97 views
0

如何創建單獨的函數(在controller.py中的索引函數內),這樣我就可以創建兩個獨立的繪圖,並具有獨立加載的兩個獨立文件?Flask中的兩個函數獨立繪製數據

換句話說,我希望用戶採取以下步驟:

  1. 上傳的培訓文件
  2. 單擊「繪圖」按鈕,繪製
  3. 現在上傳的測試文件
  4. 點擊'Plot'按鈕進行繪圖。

這兩個圖都應該出現在同一個HTML模板上。這兩個文件將使用相同的繪圖功能(或現在)。理想情況下,我想使用不同的功能。

這裏是輸入HTML的樣子:

enter image description here

繪製函數如下:

def compute_mean_std(filename=None): 
    # data = np.loadtxt(os.path.join('uploads', filename)) 
    df = pd.read_csv(os.path.join('uploads', filename)) 
    sns.set_style('darkgrid') 
    fig, ax = plt.subplots(3, sharex=True) 
    ax[0].plot(df.index, df.x, color="red") 
    ax[1].plot(df.index, df.y, color="blue") 
    ax[2].plot(df.index, df.z, color="teal") 
    ax[0].legend(numpoints=1, loc=1) 
    ax[1].legend(loc=1) 
    ax[2].legend(loc=1) 

    # Check static folder: 
    if not os.path.isdir('static'): 
     os.mkdir('static') 
    else: 
     for plotfilename in glob.glob(os.path.join('static', '*.png')): 
      os.remove(plotfilename) 
    plotfile = os.path.join('static', str(time.time()) + '.png') 
    plt.savefig(plotfile) 
    return plotfile 

當控制器具有下面的代碼:

from compute import compute_mean_std as compute_function 
@app.route('/', methods=['GET', 'POST']) 
def index(): 
    form = Average(request.form) 
    filename = None # default 
    if request.method == 'POST': 

     # Save uploaded file on server if it exists and is valid 
     if request.files: 
      file = request.files[form.filename.name] 
      if file and allowed_file(file.filename): 
       # Make a valid version of filename for any file ystem 
       filename = secure_filename(file.filename) 
       file.save(os.path.join(app.config['UPLOAD_FOLDER'], 
             filename)) 

     result = compute_function(filename) 
    else: 
     result = None 

    return render_template("view.html", form=form, result=result) 

現在都繪製相同的情節。

enter image description here

+3

它看起來像JavaScript/Ajax – furas

+0

的工作也許他們繪製相同的,因爲你畫samr形式的兩倍,但同時應該有兩個不同的字段names.Show HTML。 – furas

+0

@furas是對的。當ajax調用或POST響應時,你必須區分'訓練'和'測試' –

回答

1

那麼,我們如何區分這兩個文件上傳?該選項是我們要創建兩個文件輸入...

<input type="file" name="file_training"> 
<input type="file" name="file_test"> 

,並在您的視圖功能,你必須檢查哪些文件被用戶上傳

# Inside POST method 
file_training = request.files.get('file_training') 
file_test = request.files.get('file_test') 
return jsonify({"file_training": file_training, "file_test": file_test}) 

HTML

在你的Ajax成功跟蹤哪些具有價值。

相關問題