"Mastering Flask: Python Examples for Web Development"

Getting Started with Flask: A Python Web Framework Example

Flask is a popular, lightweight Python web framework that's perfect for small applications and APIs. It's easy to get started with Flask, and it's a great choice for prototyping and learning. In this article, we'll create a simple Flask application to demonstrate its core features.

Setting Up the Environment

Before we dive into the code, ensure you have Python and pip installed. Then, install Flask using pip:

pip install flask

Once installed, you're ready to create your first Flask application.

Flask Cheatsheet
Flask Cheatsheet

Creating a Basic Flask Application

Create a new file named app.py and add the following code:

from flask import Flask
app = Flask(__name__)

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

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

Here's what's happening:

  • from flask import Flask: Imports the Flask class.
  • app = Flask(__name__): Creates a new Flask web server.
  • @app.route('/'): Decorator that maps the specified route ('/') to the following function.
  • def home():: The function that handles the specified route. In this case, it returns a simple string.
  • if __name__ == '__main__':: Ensures the server only runs if this script is executed directly (not imported).
  • app.run(debug=True): Starts the Flask development server with debug mode enabled.

Running the Application

To run the application, simply execute the script:

🌟 Flask ile Basit Web Sunucusu Kurma 🌟
🌟 Flask ile Basit Web Sunucusu Kurma 🌟

python app.py

Then, open your browser and navigate to http://127.0.0.1:5000/ to see your Flask application in action.

Adding More Routes

Let's add a new route to our application. Update the app.py file as follows:

from flask import Flask, request
app = Flask(__name__)

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

@app.route('/greet/')
def greet(name):
    return f"Hello, {name}!"

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

Here, we've added a new route /greet/{name} that accepts a variable name. The greet function uses this variable to return a personalized greeting.

| Pythonista Planet
| Pythonista Planet

Using HTML Templates

While Flask is great for simple applications, it's often more convenient to use HTML templates for rendering web pages. Let's create a simple template and update our application to use it.

Create a new folder named templates in the same directory as app.py. Inside this folder, create a new file named base.html with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>My Flask App</title>
</head>
<body>
    <h1>Welcome to my Flask app!</h1>
    <p>{{ message }}</p>
</body>
</html>

Now, update the app.py file to use this template:

from flask import Flask, request, render_template
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('base.html', message='Hello, World!')

@app.route('/greet/')
def greet(name):
    return render_template('base.html', message=f"Hello, {name}!")

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

The render_template function renders the specified template with the provided context data.

Conclusion

In this article, we've created a simple Flask application that demonstrates the core features of this popular Python web framework. By following these examples, you should now have a solid understanding of how to get started with Flask and create basic web applications.

Python Tutorials – Real Python
Python Tutorials – Real Python
blog.md
blog.md
🌟 Flask ile Basit Web Sunucusu Kurma 🌟     Python
🌟 Flask ile Basit Web Sunucusu Kurma 🌟 Python
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
What Can You Do With Flask? (Applications & Examples)
What Can You Do With Flask? (Applications & Examples)
Display SQLite Data In HTML Table With Python Flask
Display SQLite Data In HTML Table With Python Flask
a man sitting at a desk in front of a computer and other items on top of it
a man sitting at a desk in front of a computer and other items on top of it
Flask Course - Python Web Application Development
Flask Course - Python Web Application Development
Store & Retrieve Image In Database With Python Flask
Store & Retrieve Image In Database With Python Flask
Seat Reservation System With Python Flask
Seat Reservation System With Python Flask
an image of a banana next to a computer screen with the word flash printed on it
an image of a banana next to a computer screen with the word flash printed on it
🌐✨ PYTHON FOR WEB DEVELOPMENT WITH FLASK ✨🌐
🌐✨ PYTHON FOR WEB DEVELOPMENT WITH FLASK ✨🌐
Python Flask Tutorial 3 - Flask Templates
Python Flask Tutorial 3 - Flask Templates
Flask
Flask
How to build a web app using Python’s Flask and Google App Engine
How to build a web app using Python’s Flask and Google App Engine
Build a JavaScript Front End for a Flask API – Real Python
Build a JavaScript Front End for a Flask API – Real Python
| Pythonista Planet
| Pythonista Planet
how to create a restful api with flask in python
how to create a restful api with flask in python
Python Flask Tutorial 5 - Database with Flask-SQLAlchemy
Python Flask Tutorial 5 - Database with Flask-SQLAlchemy
Flask by Example (Learning Path) – Real Python
Flask by Example (Learning Path) – Real Python
Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started
Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started
Flask Project Structure: Build a Scalable Web App – Real Python
Flask Project Structure: Build a Scalable Web App – Real Python
Tutorial β€” Flask Documentation (3.1.x)
Tutorial β€” Flask Documentation (3.1.x)