APP.
PY
from flask import Flask, render_template, request, jsonify
from textblob import TextBlob
import pandas as pd
app = Flask(__name__)
# Sample data
data = {
"reviews": [
"I love this product!",
"This is the worst experience I've ever had.",
"It's okay, not great but not terrible.",
"Absolutely fantastic! Highly recommend.",
"I'm very disappointed."
]
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/analyze', methods=['POST'])
def analyze():
reviews = request.json.get('reviews', [])
results = []
for review in reviews:
analysis = TextBlob(review)
sentiment = "Positive" if analysis.sentiment.polarity > 0 else "Negative"
if analysis.sentiment.polarity < 0 else "Neutral"
results.append({"review": review, "sentiment": sentiment, "polarity":
analysis.sentiment.polarity})
return jsonify(results)
if __name__ == '__main__':
app.run(debug=True)
INDEX.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentiment Analysis Dashboard</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<h1>Sentiment Analysis Dashboard</h1>
<textarea id="reviews" rows="10" cols="50" placeholder="Enter reviews
here..."></textarea><br>
<button id="analyzeButton">Analyze Sentiment</button>
<div id="results"></div>
<script>
document.getElementById('analyzeButton').onclick = async function() {
const reviews = document.getElementById('reviews').value.split('\
n').filter(Boolean);
const response = await fetch('/analyze', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reviews: reviews })
});
const results = await response.json();
displayResults(results);
};
function displayResults(results) {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';
results.forEach(result => {
const p = document.createElement('p');
p.textContent = `Review: "${result.review}" | Sentiment: $
{result.sentiment} | Polarity: ${result.polarity}`;
resultsDiv.appendChild(p);
});
}
</script>
</body>
</html>
STYLES.CSS
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
textarea {
width: 100%;
margin-bottom: 10px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#results {
margin-top: 20px;
}
Navigate to your project directory.
Run the Flask app:
bash
Copy
python app.py
Open your web browser and go to http://127.0.0.1:5000/.
How It Works
Input: Users enter reviews into a textarea and click the "Analyze Sentiment"
button.
Analysis: The reviews are sent to the Flask backend, where TextBlob analyzes their
sentiment.
Output: The results are displayed on the dashboard, showing each review along with
its sentiment and polarity score.