Search code examples
pythonhtmlflaskbutton

How to change the value of a button in Python (Flask)


I want to change the value of a button written in html to "records". I want to adress and change the button in python. This is in order to prevent to tap the record button twice as the request form changes and to get a visual feedback that it works.

this is what i have:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :  # <-------- here I ask what the value of the button 
            print ("is recording")                    #           is and it works fine
            p1 = threading.Thread(target=recording)
            p1.start()
    return render_template('camera.html')

this is what i want:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :
            print ("is recording")
            p1 = threading.Thread(target=recording)
            p1.start()
            request.form['record'] = 'records' :     # <----------------- Something like this
    return render_template('camera.html')

I am a newbie in python html flask and web development in general.


Solution

  • What you are looking to do should be done through JavaScript, because the most natural way to change the label of the button is through client-side programming.

    However, if you insist on doing this from server-side code, you can start by changing the template camera.html. Let's say that you have the following button in your template:

    <input type="submit" name="record" value="record here">
    

    This should be changed to include a variable:

    <input type="submit" name="record" value="{{ record }}">
    

    Now you can send the value of this variable using render_template:

    @app.route('/camera', methods=['GET', 'POST'])
    def camera(): 
        if request.method == 'POST':
            if request.form['record'] == 'record here' :
                print ("is recording")
                p1 = threading.Thread(target=recording)
                p1.start()
                return render_template('camera.html', record='records')
        return render_template('camera.html', record='record here')