Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from flask import Flask, render_template, request, jsonify
import os

app = Flask(__name__)

# Sample data for demonstration
sample_posts = [
{
'id': 1,
'title': 'Welcome to Our Web App',
'content': 'This is a modern Python web application built with Flask. It features a beautiful, responsive design and interactive elements.',
'author': 'Admin',
'date': '2024-01-15'
},
{
'id': 2,
'title': 'Getting Started',
'content': 'Learn how to use this web application effectively. Explore the features and discover what you can build.',
'author': 'Developer',
'date': '2024-01-16'
},
{
'id': 3,
'title': 'Python Web Development',
'content': 'Python is an excellent choice for web development. With frameworks like Flask and Django, you can build powerful web applications quickly.',
'author': 'Python Enthusiast',
'date': '2024-01-17'
}
]

@app.route('/')
def index():
return render_template('index.html', posts=sample_posts)

@app.route('/about')
def about():
return render_template('about.html')

@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
name = request.form.get('name')
email = request.form.get('email')
message = request.form.get('message')

# Here you would typically save the message to a database
return jsonify({
'success': True,
'message': f'Thank you {name}! Your message has been received.'
})

return render_template('contact.html')

@app.route('/api/posts')
def api_posts():
return jsonify(sample_posts)

@app.route('/api/posts/<int:post_id>')
def api_post(post_id):
post = next((p for p in sample_posts if p['id'] == post_id), None)
if post:
return jsonify(post)
return jsonify({'error': 'Post not found'}), 404

@app.route('/api/sum')
def api_sum():
try:
# Get query parameters
n1 = request.args.get('n1')
n2 = request.args.get('n2')

# Check if both parameters are provided
if n1 is None or n2 is None:
return jsonify({
'error': 'Missing required parameters',
'message': 'Both n1 and n2 parameters are required'
}), 400

# Convert to float to handle both integers and decimals
try:
num1 = float(n1)
num2 = float(n2)
except ValueError:
return jsonify({
'error': 'Invalid number format',
'message': 'Both n1 and n2 must be valid numbers'
}), 400

# Calculate sum
result = num1 + num2

return jsonify({
'n1': num1,
'n2': num2,
'sum': result,
'message': f'The sum of {num1} and {num2} is {result}'
})

except Exception as e:
return jsonify({
'error': 'Internal server error',
'message': str(e)
}), 500

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=3000)
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flask==2.3.3
Werkzeug==2.3.7
Jinja2==3.1.2
MarkupSafe==2.1.3
itsdangerous==2.1.2
click==8.1.7
blinker==1.6.3
Loading