Beginner Project: Creating a Polling Application with Flask

Posted by

Polling Application with Flask – Beginner Project

Polling Application with Flask – Beginner Project

Are you a beginner looking to build your first web application with Flask? Look no further! In this project, we will guide you through creating a simple polling application using Flask, a popular Python web framework. Let’s get started!

Prerequisites

Before we begin, make sure you have Python and Flask installed on your system. If not, you can easily install them using the following commands:

pip install flask

Setting up the Project

Create a new directory for your project and navigate to it using the terminal. Inside the directory, create a new file named app.py which will serve as the main entry point for your Flask application.

touch app.py

Creating the Polling Form

First, let’s create a simple HTML form to capture the user’s vote. In the app.py file, define a route to render the form:


from flask import Flask, render_template

app = Flask(__name__)

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

Now, create a new directory named templates within your project directory. Inside this directory, create a new file named index.html and add the following code for the polling form:

Polling Application

Cast Your Vote

Option 1
Option 2

Handling the Form Submission

Next, let’s add a route to handle the form submission and process the user’s vote. In the app.py file, define a new route:


@app.route('/submit-vote', methods=['POST'])
def submit_vote():
# Process the user's vote here
return 'Thank you for voting!'

Running the Application

That’s it! You have now created a simple polling application with Flask. To run your application, simply execute the following command in the terminal:

python app.py

Open your web browser and navigate to http://localhost:5000 to see your polling form in action. You can cast your vote and see the “Thank you for voting!” message upon submission.

Conclusion

Congratulations on building your first web application with Flask! This beginner project provides a solid foundation for understanding how to create and deploy web applications using Flask. We hope you found this tutorial helpful and encourage you to continue exploring the capabilities of Flask for building more sophisticated web applications.

0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@PP-tc1zp
11 months ago

Thank you NeuralNine, I have Idea to improve – you don't have to write csv manually: '''if not os.path.exists('polls.csv'):

structure= {

'id':[0],

'poll':[''],

'option1':[''],

'option2':[''],

'option3':[''],

'votes1':[0],

'votes2':[0],

'votes3':[0],

}'''
''' elif request.method == "POST":

poll = request.form['poll']

option1 = request.form['option1']

option2 = request.form['option2']

option3 = request.form['option3']

polls_df.loc[max(polls_df.index.values) + 1] = [poll, option1, option2, option3, 0, 0, 0]

polls_df.dropna(inplace=True) #add this line

polls_df.to_csv('polls.csv')

return redirect(url_for('index'))'''

@amirhashemdoust7325
11 months ago

very good

@paulthomas1052
11 months ago

"….Nobody likes Java…." – what ? 🤣🤣🤣🤣🤣🤣🤣

@Djellowman
11 months ago

What i'm missing from all your tutorial videos is it starting with a demo and an explanation of what it does.

@Zaryad-od2qm
11 months ago

hah, I was advised by chatgpt to look at you to study neural networks)

@philtoa334
11 months ago

Thx_Nice.

@bc4198
11 months ago

Cool!

@hajwowo10
11 months ago

Hello, my wonderful friend, your videos are very beautiful and deserve a lot of support. I have a good suggestion, perhaps for you. Why don't you make your videos contain Arabic translation? With this step, you will get a lot of support from the Middle East and from all countries Those of you guys who agree with me put a like on my comment so that it appears to the owner of this beautiful channel and looks into this matter well. Thank you very much, my friends.

@alexdiaz4371
11 months ago

Clicker python script not working while playing counter strike game, do u know what needed?

@kodeypatterson8973
11 months ago

Nice