CSV Calculator with Flask
May 2, 2025 · AI Conversations
### Example `calculations.csv`
number1,number2,operation
10,5,add
8,2,subtract
4,3,multiply
12,4,divide
### Goal:
Read this CSV and perform the calculations row-by-row, then display results on a simple HTML page using Flask.
—
### Step-by-step solution
#### 1. **CSV File (save as `calculations.csv`)**
number1,number2,operation
10,5,add
8,2,subtract
4,3,multiply
12,4,divide
#### 2. **Python Flask App (`app.py`)**
from flask import Flask, render_template
import csv
app = Flask(__name__)
def calculate(num1, num2, operation):
if operation == 'add':
return num1 + num2
elif operation == 'subtract':
return num1 - num2
elif operation == 'multiply':
return num1 * num2
elif operation == 'divide':
return num1 / num2 if num2 != 0 else 'Error (divide by zero)'
else:
return 'Invalid Operation'
@app.route('/')
def calculator():
results = []
with open('calculations.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
num1 = float(row['number1'])
num2 = float(row['number2'])
operation = row['operation']
result = calculate(num1, num2, operation)
results.append({
'num1': num1,
'num2': num2,
'operation': operation,
'result': result
})
return render_template('calculator.html', results=results)
if __name__ == '__main__':
app.run(debug=True)
#### 3. **HTML Template (`templates/calculator.html`)**
<!DOCTYPE html>
<html>
<head>
<title>CSV Calculator</title>
<style>
body { font-family: Arial; background: #f4f4f4; padding: 20px; }
table { width: 60%; margin: auto; border-collapse: collapse; }
th, td { padding: 10px; text-align: center; border: 1px solid #ccc; }
th { background-color: #333; color: white; }
</style>
</head>
<body>
<h2 style="text-align:center;">CSV Calculator Results</h2>
<table>
<tr>
<th>Number 1</th>
<th>Operation</th>
<th>Number 2</th>
<th>Result</th>
</tr>
{% for item in results %}
<tr>
<td>{{ item.num1 }}</td>
<td>{{ item.operation }}</td>
<td>{{ item.num2 }}</td>
<td>{{ item.result }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
—
Would you like me to package this as a downloadable project folder for you?
—
## **Step 1: Install Python**
If not already installed:
### On Windows:
1. Download: [https://www.python.org/downloads/windows](https://www.python.org/downloads/windows)
2. Run installer:
– **Check** "Add Python to PATH"
– Click **Install Now**
### On macOS/Linux:
Use Terminal:
brew install python # macOS with Homebrew
sudo apt install python3 python3-pip # Ubuntu/Debian
Check:
python --version
pip --version
—
## **Step 2: Install Flask**
pip install flask
—
## **Step 3: Project Structure**
Inside your project or website folder, create the following:
/your-site-folder
├── app.py
├── calculations.csv
└── templates/
└── calculator.html
—
## **Step 4: `calculations.csv` (Sample Input)**
Create this CSV file:
number1,number2,operation
10,5,add
8,2,subtract
4,3,multiply
12,4,divide
—
## **Step 5: `app.py` (Flask Calculator Page)**
Paste this in `app.py`:
from flask import Flask, render_template
import csv
app = Flask(__name__)
def calculate(num1, num2, operation):
if operation == 'add':
return num1 + num2
elif operation == 'subtract':
return num1 - num2
elif operation == 'multiply':
return num1 * num2
elif operation == 'divide':
return num1 / num2 if num2 != 0 else 'Error'
return 'Invalid'
@app.route('/calculator')
def calculator():
results = []
with open('calculations.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
num1 = float(row['number1'])
num2 = float(row['number2'])
operation = row['operation']
result = calculate(num1, num2, operation)
results.append({
'num1': num1,
'num2': num2,
'operation': operation,
'result': result
})
return render_template('calculator.html', results=results)
if __name__ == '__main__':
app.run(debug=True)
—
## **Step 6: `templates/calculator.html`**
<!DOCTYPE html>
<html>
<head>
<title>CSV Calculator</title>
<style>
body { font-family: sans-serif; background: #111; color: #eee; padding: 30px; }
table { width: 80%; margin: auto; border-collapse: collapse; }
th, td { padding: 12px; border: 1px solid #555; }
th { background: #333; }
tr:nth-child(even) { background: #222; }
</style>
</head>
<body>
<h2 style="text-align: center;">CSV Calculator Results</h2>
<table>
<tr>
<th>Number 1</th>
<th>Operation</th>
<th>Number 2</th>
<th>Result</th>
</tr>
{% for row in results %}
<tr>
<td>{{ row.num1 }}</td>
<td>{{ row.operation }}</td>
<td>{{ row.num2 }}</td>
<td>{{ row.result }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
—
## **Step 7: Run Locally**
python app.py
Go to:
**http://127.0.0.1:5000/calculator**
—
## **Step 8: Integrate With Existing Website**
### If your site uses Flask:
1. Place the logic inside your main Flask app.
2. Add this route:
from flask import Blueprint
calculator_bp = Blueprint('calculator', __name__)
@calculator_bp.route('/calculator')
def calculator():
# Copy calculator function code here
...
Then register in your main app:
app.register_blueprint(calculator_bp)
### If your site is static or uses a different backend:
You can:
– Run this Flask app as a **microservice on a different port/subdomain** like `tools.yoursite.com/calculator`
– Embed the `/calculator` page in an iframe
—
Do you want me to zip the full ready-to-run project folder for download?