How to Build and Deploy a Web-Based Calculator using Flask in Docker Environment
- Sharon Rajendra Manmothe

- 1 day ago
- 2 min read
Create a folder named: python-calculator
python-calculator/
│
├─ app.py
├─ requirements.txt
├─ Dockerfile
├─ docker-compose.yml
└─ templates/
└─ index.html
app.py (Flask app)
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
@app.route('/calculate', methods=['POST'])
def calculate():
try:
a = float(request.form.get('a'))
except:
a = None
try:
b = float(request.form.get('b'))
except:
b = None
op = request.form.get('op')
result = None
error = None
try:
if op == "add":
result = a + b
elif op == "sub":
result = a - b
elif op == "mul":
result = a * b
elif op == "div":
if b == 0:
raise ValueError("Division by zero")
result = a / b
elif op == "pow":
result = a ** b
elif op == "sqrt":
if a < 0:
raise ValueError("Square root of negative")
import math
result = math.sqrt(a)
elif op == "percent":
if b is not None:
result = (a / 100) * b
else:
result = a / 100
else:
error = "Invalid Operation"
except Exception as e:
error = str(e)
return render_template("index.html", a=a, b=b, result=result, error=error)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)index.html
<!DOCTYPE html>
<html>
<head>
<title>Python Full Calculator</title>
<style>
body { font-family: Arial; margin: 40px; }
.card { max-width: 450px; padding: 20px; border-radius: 10px; box-shadow: 0 0 8px gray; }
input { width: 100%; padding: 10px; margin: 8px 0; }
button { padding: 10px 12px; margin: 4px; }
.result { font-weight: bold; margin-top: 10px; }
.error { color: red; margin-top: 10px; }
</style>
</head>
<body>
<div class="card">
<h2>Python Calculator (Flask + Docker)</h2>
<form action="/calculate" method="POST">
<label>Number A</label>
<input type="text" name="a" placeholder="Enter number A" value="{{ a }}">
<label>Number B</label>
<input type="text" name="b" placeholder="Enter number B" value="{{ b }}">
<button type="submit" name="op" value="add">A + B</button>
<button type="submit" name="op" value="sub">A - B</button>
<button type="submit" name="op" value="mul">A × B</button>
<button type="submit" name="op" value="div">A ÷ B</button>
<button type="submit" name="op" value="pow">A ^ B</button>
<button type="submit" name="op" value="sqrt">√A</button>
<button type="submit" name="op" value="percent">A % of B</button>
</form>
{% if result %}
<div class="result">Result: {{ result }}</div>
{% endif %}
{% if error %}
<div class="error">Error: {{ error }}</div>
{% endif %}
</div>
</body>
</html>requirements.txt
Flask==3.0.0
Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
docker-compose.yml
version: "3.8"
services:
calculator:
build: .
container_name: python-calculator
ports:
- "5000:5000"
restart: unless-stopped
Run on Windows
Open PowerShell or CMD inside python-calculator folder:
Step 1 — Build Docker image
docker-compose up --build
Step 2 — Open in browser
Visit: http://localhost:5000

Stop container:
docker-compose down
$50
Product Title
Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50
Product Title
Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50
Product Title
Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.


Comments