My Blog

My WordPress Blog

My Blog

My WordPress Blog

Survey and Polling Application

Project Structure

survey_app/
│
├── app.py               # Main application file
├── database.db          # SQLite database file
├── templates/           # Folder for HTML templates
│   ├── index.html       # Main survey form
│   └── results.html     # Results display page
└── static/              # Folder for static files (CSS, JS)
└── styles.css       # CSS styles

Step 1: Set Up Flask

First, make sure you have Flask installed. You can do this via pip:

pip install Flask

Step 2: Create the Main Application (app.py)

Here’s a basic implementation of the Flask application:

from flask import Flask, render_template, request, redirect
import sqlite3

app = Flask(__name__)

# Database setup
def init_db():
with sqlite3.connect('database.db') as conn:
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS survey (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            question TEXT,
            answer TEXT
        )
    ''')
    conn.commit()
init_db() @app.route('/') def index():
return render_template('index.html')
@app.route('/submit', methods=['POST']) def submit():
question = request.form.get('question')
answer = request.form.get('answer')
with sqlite3.connect('database.db') as conn:
    cursor = conn.cursor()
    cursor.execute('INSERT INTO survey (question, answer) VALUES (?, ?)', (question, answer))
    conn.commit()
return redirect('/results')
@app.route('/results') def results():
with sqlite3.connect('database.db') as conn:
    cursor = conn.cursor()
    cursor.execute('SELECT question, answer FROM survey')
    results = cursor.fetchall()
return render_template('results.html', results=results)
if __name__ == '__main__':
app.run(debug=True)

Step 3: Create HTML Templates

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
&lt;title>Survey Application&lt;/title>
</head> <body>
&lt;h1>Survey&lt;/h1>
&lt;form action="/submit" method="post">
    &lt;label for="question">Question:&lt;/label>&lt;br>
    &lt;input type="text" id="question" name="question" required>&lt;br>&lt;br>
    
    &lt;label for="answer">Answer:&lt;/label>&lt;br>
    &lt;input type="text" id="answer" name="answer" required>&lt;br>&lt;br>
    
    &lt;button type="submit">Submit&lt;/button>
&lt;/form>
</body> </html>

templates/results.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
&lt;title>Survey Results&lt;/title>
</head> <body>
&lt;h1>Survey Results&lt;/h1>
&lt;table>
    &lt;tr>
        &lt;th>Question&lt;/th>
        &lt;th>Answer&lt;/th>
    &lt;/tr>
    {% for question, answer in results %}
    &lt;tr>
        &lt;td>{{ question }}&lt;/td>
        &lt;td>{{ answer }}&lt;/td>
    &lt;/tr>
    {% endfor %}
&lt;/table>
&lt;a href="/">Back to Survey&lt;/a>
</body> </html>

Step 4: Add CSS (static/styles.css)

cssCopy codebody {
font-family: Arial, sans-serif;
margin: 20px;
} h1 {
color: #333;
} form {
margin-bottom: 20px;
} table {
width: 100%;
border-collapse: collapse;
} table, th, td {
border: 1px solid #ddd;
} th, td {
padding: 8px;
text-align: left;
}

Step 5: Run the Application

To run the application, navigate to the project directory in your terminal and execute:

bashCopy codepython app.py

Then open your web browser and go to http://127.0.0.1:5000.

Survey and Polling Application

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top