Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is Flask and how do you create a basic Flask application? Flask क्या है और basic Flask application कैसे बनाते हैं?

Answer

Flask is a lightweight, micro web framework for building web applications in Python. It's easy to learn and perfect for small to medium-sized applications.

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

# Basic route
@app.route('/')
def hello():
    return 'Hello, World!'

# Route with parameter
@app.route('/user/<name>')
def greet_user(name):
    return f'Hello, {name}!'

# POST request
@app.route('/submit', methods=['POST'])
def submit():
    data = request.get_json()
    return jsonify({'message': f'Received: {data}'})

# Render HTML template
@app.route('/index')
def index():
    return render_template('index.html', title='Home')

# Running the application
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

# HTTP Methods
@app.route('/api/data', methods=['GET', 'POST', 'PUT', 'DELETE'])
def handle_data():
    if request.method == 'GET':
        return jsonify({'data': 'Sample data'})
    elif request.method == 'POST':
        return jsonify({'status': 'Created'}), 201
    elif request.method == 'PUT':
        return jsonify({'status': 'Updated'}), 200
    elif request.method == 'DELETE':
        return jsonify({'status': 'Deleted'}), 204
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello'

@app.route('/user/<name>')
def greet(name):
    return f'Hello {name}'

@app.route('/post', methods=['POST'])
def handle_post():
    return 'POST received'

if __name__ == '__main__':
    app.run(debug=True)

Was this answer clear?