1

This question could be a duplicate but I have checked all the answers of such related questions and I haven't been able to solve it.

I am trying to get the value from a dropdown menu which consists of numbers. Then I want to compare the numbers with a value and display a text based on the comparison.

Eg

if value_selected_from_dropdown >3
 display text

I am unable to get the text to display or even print the value of the option selected.

Here is the python file, web_plants.py

from flask import Flask, render_template,request, redirect, url_for

app = Flask(__name__)

def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text
        }
    return templateDate

@app.route("/threshold", methods=['POST'])
def threshold():
        tvalue= (request.form.get['tvalue']) #get value from dropdown
        msg= ""
        if tvalue>3:
          msg= "rating above 3"
         templateData = template(text = msg) #display text using template()
        #templateData = template(text = tvalue) #tried to print the value selected
        return render_template('index.html', **templateData)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

index.html:

<!DOCTYPE HTML>
<html>

<head>
 <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='style.css')}}" />
</head>

<body>

 <h2> {{ text }} </h2>
<form action= "{{ url_for('threshold') }}" method="post>"
             <p>
             <select name= 'tvalue'>
                 <option value="10">10</option>
                 <option value="11">11</option>
                 <option value="15">15</option>
                 <option value="2">2</option>
                 <option value="1">1</option>
              </select>
              </p>
    </form>
 </body>
</html>
2
  • 1
    method="post>" is a typo. Should be method="post"> Commented Apr 3, 2018 at 13:03
  • @noslenkwah Thanks for pointing that out. Commented Apr 3, 2018 at 13:09

2 Answers 2

3

There are several ways to achieve this. Either you can give logic to the template itself or you can add the logic in the function threshold.

index.html

<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>

     <input type="submit" value="Submit" />
</form>

OR,

{% if selected_tvalue > 3 %}
    <h2> Selected value is greater than 3 </h2>
{% else %}
     <h2> Selected value is less than or equal to 3 </h2>
{% endif %}   
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>
    <input type="submit" value="Submit" />
</form>

server.py

def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text,
        'tvalues' : getTValues(),
        'selected_tvalue' : -1
    }
    return templateDate

def getTValues():
    return (10, 11, 15, 2, 1) 

@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
    tvalue= -1 #default value
    msg = ''
    if request.method == "POST":            
        tvalue = int(request.form['tvalue'])
        if tvalue> 3:
            msg= "rating above 3"

    #generating template data
    templateData = template(text = msg)
    templateData['selected_tvalue'] = tvalue 

    return render_template('index.html', **templateData)

Then access your form at path /threshold. I hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I owe you the credits for helping me implement an important functionality in my final year project related to IoT and Raspberry Pi. The codes I posted provided just a basic and "simplified" means to get a threshold value to compare with sensor values I am getting(not added in the codes I posted). Based on the comparison(if tvalue> 3:), a bulb will turn on and off. Thank you so much for taking the time to write the answer :)
Hello, I need to have this function run as a background task so that when a user clicks on the Submit button, the value is constantly being checked by the if operator. Can you please help with it? Because I am getting working outside of request context when I tried to get it working through some ways
I posted a question on it here: stackoverflow.com/questions/49688824/…
1

In your html after your drop down block you may need something like

 <input type="submit">

which will trigger the submit. I am not sure, selecting a value alone triggers the form submit.

By the way where are you rendering your page initially ? I would have something like:

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

in the python code. There to get the value, I would try

tvalue= request.args.get('tvalue')

Well not 'form' but 'args', and normal brackets instead of squared ones. Finally the function where you are going to handle that 'templateData' might be missing too.

last note: you might need GET method too:

@app.route("/threshold", methods=['GET', 'POST'])

1 Comment

I am rendering it initially at index.html.. I just omitted that part in the codes I posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.