To create a Flask app with a button in Python, you need to follow these steps:
pip install Flask
in your command prompt or terminal.main.py444 chars15 lines
Note that we have defined the /
route and the home
function, which renders the home.html
file. Inside the function, we have checked whether the request method is POST. If it is POST, this means that the button has been clicked and we can execute the necessary code.
231 chars13 lines
This HTML file contains a form with a button. Notice that the action
attribute of the form is set to '/'
, which is the route that we defined in the Flask app. Also, the method
attribute is set to 'post'
.
234 chars17 lines
This CSS file is just an example. You can customize it to your liking.
http://localhost:5000/
(assuming you have not changed the port number). Click the button and you should see the message "Button clicked."
printed in the console.gistlibby LogSnag