You are currently viewing Getting Started with Python and Flask

Getting Started with Python and Flask

  • Post author:
  • Post category:Python
  • Post comments:0 Comments
  • Post last modified:February 23, 2024

Flask is a lightweight and versatile Python web framework that provides tools, libraries, and patterns to build web applications. In this tutorial, we’ll cover the basics of setting up a Flask application, creating routes, handling requests, and using templates.

Prerequisites

  • Basic understanding of Python
  • Python installed on your machine (3.6 or newer)
  • Basic knowledge of web development concepts (HTTP, HTML, etc.)

Installation

First, let’s install Flask. It’s recommended to set up a virtual environment for your project to manage dependencies.

Using pip and Virtual Environment

If you haven’t installed Flask, you can do so using pip:

pip install Flask

To create a virtual environment, navigate to your project directory and run:

python3 -m venv myenv

Activate the virtual environment:

  • On Windows:
myenv\Scripts\activate
  • On macOS/Linux:
source myenv/bin/activate

Now you’re ready to create your Flask application!

Your First Flask App

Let’s create a simple “Hello World” Flask application.

1. Create a Flask App

Create a new Python file, for example, app.py, and open it in your favorite text editor.

2. Import Flask

At the top of your app.py file, import Flask:

from flask import Flask

3. Create an App Instance

Create an instance of the Flask class:

app = Flask(__name__)

4. Define a Route

Define a route for the root URL (“/”) that returns “Hello, World!”:

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

5. Run the App

At the end of your app.py file, add the following code to run the Flask application:

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

6. Run the App

Save the file and run your Flask app:

python app.py

You should see output similar to this:

 * Running on http://localhost:5000/

7. Open in Browser

Open your web browser and go to http://localhost:5000/. You should see “Hello, World!” displayed.

Congratulations! You’ve created your first Flask application.

Adding Dynamic Routes

Flask allows you to create dynamic routes that can accept parameters. Let’s modify our app to greet a specific user.

1. Update the Route

Update your hello_world function to accept a name parameter:

@app.route('/hello/<name>')
def hello_name(name):
    return 'Hello, {}!'.format(name)

2. Run the App

Save your app.py file and run the app again:

python app.py

3. Test in Browser

Open your browser and go to http://localhost:5000/hello/YourName. You should see a personalized greeting.

Using Templates

Flask allows you to render HTML templates to create more complex web pages. Let’s create a simple template for our greetings.

1. Create a Template Directory

Create a directory named templates in your project directory.

2. Create a Template File

Inside the templates directory, create a file named greet.html:

<!DOCTYPE html>
<html>
<head>
    <title>Greeting</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

3. Update Route to Render Template

Update your app.py file to render this template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/hello/<name>')
def hello_name(name):
    return render_template('greet.html', name=name)

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

4. Run the App

Run your Flask app again:

python app.py

5. Test in Browser

Visit http://localhost:5000/hello/YourName in your browser. You should see a nicely formatted greeting.

Conclusion

You’ve now built a simple Flask application that:

  • Responds to different routes
  • Accepts parameters in URLs
  • Renders HTML templates

This is just the beginning of what you can do with Flask. Explore more features like handling forms, interacting with databases, and building more complex web applications! Flask’s official documentation (https://flask.palletsprojects.com/) is an excellent resource for diving deeper into its capabilities.

Leave a Reply