2017-04-09 190 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

相关问题