2017-04-07 160 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,或者它可能具有哪些属性。只要确保你发送了你的函数需要的数据,并且你的函数被设置为接收你发送数据的类型。

相关问题