Web Poll App in Python
About the project: This is a project for a Web Poll App using Python and the Flask framework. This is a great way to learn about web application development, as it involves handling user input and displaying dynamic content.
This project is designed as a single, self-contained Python file. It will use a simple in-memory dictionary to store the poll's question and vote counts, making it easy to run without a separate database setup.
How to Run the Project
- Install Flask: If you haven't already, open your terminal or command prompt and install Flask using pip:
pip install Flask
- Save the Code: Save the code above into a file named app.py.
- Run the App: Navigate to the directory where you saved the file and run it from your terminal:
python app.py
- Access the App: Open your web browser and go to http://127.0.0.1:5000 to see the poll app in action.
- This project gives a solid foundation for a simple web application. Because the data is stored in memory, all votes will be reset each time you restart the application.
- For a production-ready application, you would replace the poll_data dictionary with a persistent database.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE) and Open your web browser and go to http://127.0.0.1:5000 to see the poll app in action.
# app.py
# Import necessary Flask components
from flask import Flask, render_template_string, request, redirect, url_for
# Create a new Flask application instance
app = Flask(__name__)
# --- In-Memory Data Store ---
poll_data = {
"question": "What is your favorite programming language for data science?",
"options": {
"Python": 0,
"R": 0,
"Julia": 0,
"SQL": 0,
}
}
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Poll App</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #eef1f5;
margin: 0;
color: #333;
}
.container {
background-color: #ffffff;
padding: 40px 50px;
border-radius: 15px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 100%;
text-align: center;
}
h1 {
color: #1a237e;
margin-bottom: 20px;
font-size: 2em;
}
.poll-form {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 15px;
padding: 20px 0;
}
.poll-form label {
font-size: 1.1em;
cursor: pointer;
width: 100%;
text-align: left;
transition: color 0.2s ease-in-out;
}
.poll-form input[type="radio"] {
margin-right: 10px;
transform: scale(1.2);
accent-color: #007bff;
}
.submit-btn {
width: 100%;
padding: 12px;
border: none;
border-radius: 8px;
background-color: #007bff;
color: white;
font-size: 1.2em;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease;
box-shadow: 0 4px 10px rgba(0, 123, 255, 0.2);
}
.submit-btn:hover {
background-color: #0056b3;
transform: translateY(-2px);
}
.results-section {
margin-top: 30px;
}
.result-bar {
background-color: #e0e0e0;
border-radius: 8px;
margin: 10px 0;
overflow: hidden;
position: relative;
height: 40px;
}
.bar-fill {
height: 100%;
background-color: #4CAF50;
transition: width 0.5s ease-out;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 10px;
box-sizing: border-box;
white-space: nowrap;
}
.bar-label {
color: #333;
font-weight: bold;
z-index: 10;
}
.vote-count {
color: white;
font-weight: bold;
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}
.no-votes {
font-style: italic;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<h1>{{ poll_data.question }}</h1>
{% if voted %}
<div class="results-section">
{% set total_votes = poll_data.options.values()|sum %}
{% for option, votes in poll_data.options.items() %}
<div class="result-bar">
{% set percentage = (votes / total_votes * 100) if total_votes > 0 else 0 %}
<div class="bar-fill" style="width: {{ percentage }}%;">
<span class="bar-label" style="color: {{ 'white' if percentage > 50 else '#333' }};">{{ option }}</span>
<span class="vote-count">{{ votes }}</span>
</div>
</div>
{% endfor %}
</div>
<p><a href="/">Vote again?</a></p>
{% else %}
<form action="{{ url_for('vote') }}" method="post" class="poll-form">
{% for option in poll_data.options.keys() %}
<label>
<input type="radio" name="option" value="{{ option }}" required>
{{ option }}
</label>
{% endfor %}
<button type="submit" class="submit-btn">Vote</button>
</form>
{% endif %}
</div>
</body>
</html>
"""
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
selected_option = request.form.get('option')
if selected_option and selected_option in poll_data['options']:
poll_data['options'][selected_option] += 1
return redirect(url_for('home', voted=True))
return redirect(url_for('home'))
voted = request.args.get('voted', False)
return render_template_string(HTML_TEMPLATE, poll_data=poll_data, voted=voted)
if __name__ == '__main__':
app.run(debug=True)
← Back to Projects