2017-04-09 188 views
-1

我想從一個函數返回一個值到另一個函數並使用它來過濾我的excel。下面是什麼我試圖做的事:從一個函數傳遞返回參數另一個函數

@app.route("/bzrules") 
    def show_tables(): 
     rules = pd.read_excel('static/dummy_data.xlsx') 
     df = pd.read_excel('static/Book1.xlsx') 
     valid=pd.read_excel('static/rules.xls') 
     rules.set_index(['DR_ID'], inplace=True) 
    rules.index.name=None 
    allfields3=rules.loc[rules['DQ_Dimension']=='Accuracy'] 
    allfields2 = allfields3[['Subject_Area','Field_Name']] 
    if request.method == 'GET': 
     subarealist = rules['Subject_Area'].unique().tolist() 
     validvalues=valid['Field_Name'].loc[valid['Rule_Type']=='Valid Value Check'] 
     # this is were user selection from first function assigned to seach_key variable. i want to pass this variable to another function below. 
在我先前的職位subarealist

是用戶的選擇。

在這一請求,我現在的儲蓄在一個變量我的用戶選擇,並試圖變量傳遞給另一個函數

 search_key = request.args.get('fieldnames') 

@app.route('/postfields') 
def postfields(): 
    # i tried doing like below, but getting errors 
    search_key=show_tables() 
    dt=pd.read_excel('static/AllDataSheets.xlsx',sheetname=search_key) 
+0

請不要轉發問題。如果您已更新有關問題的信息,請編輯現有問題以顯示該問題。 – davidism

+0

哪個變量有你需要傳遞的用戶選擇?字段名是變量還是字符串? – brennan

回答

0

我不能肯定,如果SEARCH_KEY在你的例子是一個參數或期望的結果,也許:

@app.route("/bzrules") 
def show_tables(): 
    # .. lost of stuff here 
    result = postfields(search_key) 
    # more stuff here 
    return 'ok' # or maybe render_template 

def postfields(search_key): 
    dt = pd.read_excel('static/AllDataSheets.xlsx',sheetname=search_key) 
    return dt 

無論哪種方式,很明顯,你需要確保你的功能return東西。這裏有一個完整的例子:

@app.route('/<string:search_key>') 
def index(search_key): 
    search_result = check(search_key) # call another function 
    return search_result 

def check(search_key): 
    things = ['foo', 'bar', 'foobars'] 
    if search_key in things: 
     result = 'found it :)' 
    else: 
     result = 'not found :(' 
    return result # return value to caller 

而且非視功能不應該得到@app.route裝飾。

+0

search_key是我的預期結果 – totalzoom

相關問題