将返回参数值的一个函数传递给另一个瓶子 [英] pass return argument value one function to another flask

查看:147
本文介绍了将返回参数值的一个函数传递给另一个瓶子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个函数返回一个值到另一个函数,并用它来过滤我的excel。
下面是我正在尝试做的:

$ $ $ $ $ $ $ $ $ $ $ $ def show_tables():
规则= pd.read_excel('static / dummy_data.xlsx')
#用户将在view.html下拉列表中选择值。我希望用户选择被传递给我的第二个函数来筛选我的Excel表格
subarealist = rules ['Subject_Area']。unique()。tolist()
return render_template('view.html' ,subarealist = subarealist)

@ app.route('/ postfields')
def postfields():
#这是用户从第一个函数中选择来传递和帮助选择来自我的Excel
dt = pd.read_excel('static / AllDataSheets.xlsx',sheetname ='用户选择来自于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 subarealist%}
< option value ='{{val}}'selected =search_key{%if search_key == val%} {%endif%}> { {VAL}}< /选项>
< option selected =selected>< / option>
{%endfor%}


解决方案

<

 < 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 subarealist%}

然后将您的函数更改为:

  @ app.route('/ postfields /< sheet_name>',methods = ['POST'])
def postfields(sheet_name ):
#这是用户从第一个函数中选择来传递并帮助从我的Excel中选择工作表
dt = pd.read_excel('static / AllDataSheets.xlsx',sheetname = sheet_name)

您可能需要稍微更改 sheet_name = val ,因为我不确定究竟是什么样的对象 val 可能是,或者它可能有什么属性。只要确保你发送了你的函数需要的数据,并且你的函数被设置为接收你发送数据的类型。

I would like to return a value from one function to another function and use it to filter my excel. Below is what am trying to do:

@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')

below is part of View.html code :

<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 %}       
           

解决方案

I think what you want is something like this:

<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 %}

And then change your function to:

@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)

You may need to change the sheet_name=val section a bit, because I'm not sure exactly what kind of object val might be, or what properties it could have. Just make sure that you're sending the data your function needs, and that your function is set to receive the type of data you're sending it.

这篇关于将返回参数值的一个函数传递给另一个瓶子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆