-1

I've built a working web page and python/flask program but cannot figure out how to read the state of a webpage checkbox in the python code. I've figured out how to set a checkbox from python, just not how to read it. Here is the html for one of the checkboxes:

<div class="slideThree">    
    <input type="checkbox" value="None" id="chk_01" name="chk_01" {{chk_01_checked}}/>
    <label for="chk_01"></label>
</div>

The html code is in a separate file from the python program. It looks like I need to use some combination of 'request' or 'get' instructions but cannot find clear examples on how to do it. I'm a novice in both python and html, so any help appreciated.

1
  • Please check, Possible duplicate of link Commented Nov 10, 2015 at 17:48

2 Answers 2

0

The checkbox exists in the browser, the python code runs on the server. Basically the server will have no knowlegde of the checkbox state. If you want to change that, you need to connect the two.

Two options there:

(1): the checkbox is included in a form and the state is included in the form data when the user clicks submit.

(2): you assign an event handler to the state of the checkbox. On the event handler, you post the new state to some AJAX resource. In Flask you add a route for the AJAX URL and receive the state changes.

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

5 Comments

Thanks for the reply. (1) Don't want to use a submit form button. (2) What is meant by "post the new state to some AJAX resource. In Flask you add a route for the AJAX URL and receive the state changes." Again, I'm a novice, trying to find my way through this so a lot of the terminology is unfamiliar to me.
The HTML code needs some javascript, to detect the check/uncheck events and call an HTTP post to update the state on the server. This is called AJAX. The receiving site is a route on your Flask app. Maybe this link will help: flask.pocoo.org/docs/0.10/patterns/jquery
Thanks for the reply again, but no that link doesn't really help me. I don't understand or know how to: Write java code to “to detect the check/uncheck events”; Know how to “call an HTTP post to update the state on the server” - is this code in html or also java? I think I understand how to program the route in flask based on the examples I've seen. But I cannot find any clear instructions or examples for the html parts. Didn't think this would be that obscure, I guess it's not as common to do this as I thought.
Are you using a javascript framework (jquery, react, angular, ...) or vanilla javascript? Frameworks have the AJAX functions built-in with simple notation. It's just a matter of (1) connecting the click event to the function in html and (2) implementing the function in javascript.
I don't think I'm using any of that. I'm using basic html and python editors to write the code. I've added code in another answer, hoping to make it clear what I'm trying to do.
0

Here is what I am trying to do. I added a action (form action) line in html, then a function in python to process it. But the python function never executes so I clearly don't have something right.

here is the html:

<td style="width:auto">
<form action="ManToggle">
<div class="slideThree">
    <input type="checkbox" value="None" id="chk_01_Man" name="chk_01_Man" />
    <label for="chk_01_Man"></label>
</div>
</form>
</td> 

Here is the python:

@app.route('/ManToggle', methods=['GET'])
def ManToggleUpdate():
    print ("function executes")
    test = request.form['chk_01_Man']
    if test == True :
        # do something
    else :
        # do something else

Comments

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.