← Back to Python Course | Chapter 13: Data Science & Web | Lesson 12 of 14

Python Flask परिचय

Flask Python से websites बनाने का एक छोटा tool है, जैसे किसी दुकान के लिए starter kit। आप बताते हैं कि कौन सा address कौन सा page दिखाएगा, और बाकी काम Flask करता है।

Flask का परिचय

Flask एक minimal, unopinionated web framework है -- यह किसी project structure तय किए बिना या कोई ORM बंडल किए बिना routing और request handling देता है, जो इसे छोटे services और APIs के लिए अच्छा fit बनाता है।

कोई भी Flask code चलाने से पहले इसे pip install flask से install करें।

उदाहरण: Introduction to Flask

python
# pip install flask
from flask import Flask
app = Flask(__name__)
print(type(app))

Flask App Routing

Routing किसी URL path को उस Python function से जोड़ता है जिसे उसके requests संभालने चाहिए, जिसे handler function के ठीक ऊपर @app.route('/path') decorator से declare किया जाता है।

जब कोई browser या client उस path की request करता है, Flask संबंधित function को call करता है और वो जो भी बनाता है उसे HTTP response के रूप में लौटा देता है।

उदाहरण: Flask App Routing

python
from flask import Flask
app = Flask(__name__)

@app.route("/")  # maps the "/" URL path to this function
def home():
    return "Hello, Flask!"

print(app.url_map)

Flask Routes को Test करना

Flask का built-in test client आपको बिना कोई असली listening server शुरू किए या कोई असली network socket खोले, code में ही अपने routes के खिलाफ requests simulate करने देता है।

यह route logic के automated testing को तेज़ बनाता है और किसी special test infrastructure की ज़रूरत नहीं पड़ती।

उदाहरण: Testing Flask Routes

python
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, Flask!"

client = app.test_client()  # simulates requests without a real server
response = client.get("/")
print(response.data)

Dynamic Route Variables इस्तेमाल करना

किसी path segment को angle brackets में लिखना, जैसे /users/<username>, Flask को उस URL हिस्से को एक variable के रूप में capture करने और सीधे handler function में एक argument के रूप में पास करने के लिए कहता है -- जिससे एक ही route definition related URLs के पूरे परिवार को serve कर सकती है।

उदाहरण: Using Dynamic Route Variables

python
from flask import Flask
app = Flask(__name__)

@app.route("/users/<username>")  # <username> is captured from the URL
def show_user(username):
    return f"User: {username}"

client = app.test_client()
print(client.get("/users/alex").data)

JSON Responses लौटाना

Flask route handler से एक plain Python dictionary लौटाना अपने-आप सही Content-Type header सेट के साथ एक JSON HTTP response में बदल जाता है, जो किसी Flask-आधारित API endpoint के लिए सबसे आम response आकार है।

उदाहरण: Returning JSON Responses

python
from flask import Flask
app = Flask(__name__)

@app.route("/data")
def data():
    return {"status": "ok"}  # dict is automatically converted to a JSON response

client = app.test_client()
print(client.get("/data").json)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.