Untitled

mail@pastecode.io avatar
unknown
python
24 days ago
3.0 kB
53
Indexable
Never
from flask import Flask, jsonify, request
app = Flask(__name__)
numbers = [3, 5, 1, 2, 8, 9, 4, 6, 6, 7]
@app.route('/stats', methods=['GET'])
def get_statistics():
    if not request.args:
        return jsonify({
            'min': min(numbers),
            'max': max(numbers),
            'average': sum(numbers) / len(numbers)
        })
    if 'min' in request.args:
        return jsonify({'min': min(numbers)})
    if 'max' in request.args:
        return jsonify({'max': max(numbers)})
    if 'average' in request.args:
        return jsonify({'average': sum(numbers) / len(numbers)})
    return jsonify({'error': 'Bilinmeyen parametreler'}), 400
if __name__ == '__main__':
    app.run(debug=True)


from flask import Flask, jsonify, request, abort
app = Flask(__name__)
# Öğrenci listesi
students = [
    {'id': 1, 'name': 'John', 'age': 20},
    {'id': 2, 'name': 'Emma', 'age': 22},
    {'id': 3, 'name': 'Michael', 'age': 18},
    {'id': 4, 'name': 'Sophia', 'age': 25},
    {'id': 5, 'name': 'James', 'age': 19}
]
@app.route('/students', methods=['GET'])
def get_students():
    age_param = request.args.get('age', default=18, type=int)
    filtered_students = [student for student in students if student['age'] == age_param]
    if not filtered_students:
        abort(404)
    return jsonify(filtered_students)
if __name__ == '__main__':
    app.run(debug=True)


from bottle import Bottle, run, request, HTTPResponse
import json
app = Bottle()
messages = ['1', '2', '3']
@app.route('/api/messages', method='POST')
def add_message():
    data = request.json
    if 'message' in data:
        messages.append(data['message'])
        return HTTPResponse(status=201)
    else:
        return HTTPResponse(status=400)
@app.route('/api/messages', method='GET')
def get_messages():
    return json.dumps(messages)
@app.route('/api/messages/<message_id>', method='GET')
def get_message_by_id(message_id):
    try:
        message = int(message_id)
        return messages[message]
    except (IndexError, ValueError):
        return HTTPResponse(status=404)
if __name__ == '__main__':
    app.run(debug=True)
    
from flask import Flask, render_template, request, flash
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if len(username) < 4:
            flash('Username must be at least 4 characters long.', 'error')
        elif len(password) < 6:
            flash('Password must be at least 6 characters long.', 'error')
        else:
            flash(f'Account created for {username}!', 'success')
            # Burada veritabanına veya başka bir veri depolama mekanizmasına kaydedin.
    return render_template('register.html')
if __name__ == '__main__':
    app.run(debug=True)
Leave a Comment