Predictive Hacks

How to add action buttons in Flask

In the Flask code you can add an action under every if statement of the corresponding button like rendering a template or running a python script. In the HTML code you can add as many buttons as you want just be sure to add the right values and names.

Flask Code

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if request.form.get('action1') == 'VALUE1':
            pass # do something
        elif  request.form.get('action2') == 'VALUE2':
            pass # do something else
            pass # unknown
    elif request.method == 'GET':
        return render_template('index.html', form=form)
    return render_template("index.html")

In the HTML Code (index.html)

<h3>Our Flask Buttons<h3/>
	<form method="post" action="/">
		<input type="submit" value="VALUE1" name="action1"/>
		<input type="submit" value="VALUE2" name="action2" />


Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s