2017-04-07 158 views
0

我想從一個函數返回一個值到另一個函數,並用它來過濾我的Excel。 下面就是我試圖做的事:傳遞返回參數值一個函數到另一個瓶

@app.route('/bzrules') 
def show_tables(): 
    rules = pd.read_excel('static/dummy_data.xlsx') 
    # User will select value in drop down in view.html. I want the user selection to be passsed to my second function below to filter my excel sheet 
    subarealist = rules['Subject_Area'].unique().tolist() 
    return render_template('view.html', subarealist=subarealist) 

@app.route('/postfields') 
def postfields(): 
    # this is were user selection from first function to pass and help select sheet from my excel 
    dt = pd.read_excel('static/AllDataSheets.xlsx', sheetname='User selection from subarealist') 
下面

是View.html部分代碼:

<form action="{{ url_for('show_tables') }}" method="POST"> 
    <div class="form-group"> 
     <div class="input-group"> 
      <select name='fieldnames'onchange="this.form.submit()"> 
       {% for val in subarealist %} 
        <option value='{{val}}' selected="search_key" {% if search_key==val %}{% endif%}>{{val}}</option> 
        <option selected="selected"></option> 
       {% endfor %}        
        
+0

您必須將HTML中的表單配置爲將表單數據發佈到第二個端點。 – Miguel

+0

我附上了我的html代碼,能否請您建議必要的更改@Migel – totalzoom

+0

將action屬性更改爲指向您的'postfields'路線,然後在該路線中添加對錶單args的處理。 – Miguel

回答

0

我想你想要的是這樣的:

<form action="{{ url_for('postfields', sheet_name=val) }}" method="POST"> 
    <div class="form-group"> 
     <div class="input-group"> 
      <select name='fieldnames'onchange="this.form.submit()"> 
       {% for val in subarealist %} 
        <option value='{{val}}' selected="search_key" {% if search_key==val %}{% endif%}>{{val}}</option> 
        <option selected="selected"></option> 
       {% endfor %} 

而且然後將您的功能更改爲:

@app.route('/postfields/<sheet_name>', methods=['POST']) 
def postfields(sheet_name): 
    # this is were user selection from first function to pass and help select sheet from my excel 
    dt = pd.read_excel('static/AllDataSheets.xlsx', sheetname=sheet_name) 

您可能需要稍微更改sheet_name=val部分,因爲我不確定可能是什麼類型的對象val,或者它可能具有哪些屬性。只要確保你發送了你的函數需要的數據,並且你的函數被設置爲接收你發送數據的類型。

相關問題