Flask Tutorials: Before First Request 🎯

beginner
22 min

Flask Tutorials: Before First Request 🎯

Welcome to the Flask Tutorials! In this lesson, we'll dive into the world of Flask, a Python web framework that makes it easy to build web applications. Let's get started! 🚀

What is Flask? 📝

Flask is a micro web framework written in Python. It provides an easy-to-use and flexible approach to building web applications. Flask allows you to create web apps from scratch, making it perfect for beginners and intermediates looking to expand their web development skills.

Installing Flask 💡

Before we dive into building our first web application, let's install Flask. Open your terminal and type:

pip install flask

Creating a Flask App ✅

Now that Flask is installed, we can create our first Flask app. Create a new file called app.py and add the following code:

python
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, World!" if __name__ == "__main__": app.run(debug=True)

Let's break this down:

  1. We import Flask from the flask module.
  2. We create an instance of Flask with the __name__ of our current script.
  3. We define a route for the homepage using the @app.route('/') decorator.
  4. We define a function home() that returns our message.
  5. Finally, we check if our script is run directly (not imported) and start the development server with debug mode turned on.

To run our Flask app, save the file and run python app.py in your terminal. Visit http://localhost:5000 in your web browser to see our "Hello, World!" message! 🎉

Understanding the Application Object 💡

The Flask application object (app in our example) is a container for all the components of our web application. It manages routes, templates, and more. It's important to understand that the application object is created only once, and it's shared among all routes in our application.

Creating a Simple Route 💡

Let's create a simple route that displays different messages based on the URL. Edit your app.py file and add the following code:

python
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return "Hello, World!" @app.route('/about') def about(): return "Welcome to our About page!" @app.route('/contact') def contact(): return "Contact us at info@example.com" if __name__ == "__main__": app.run(debug=True)

Here, we've defined three routes: /, /about, and /contact. Each route returns a different message when accessed in the browser.

Quick Quiz
Question 1 of 1

What happens when you access `http://localhost:5000` in your web browser?

Using Templates 💡

Templates allow us to separate our HTML markup from our Python code. In Flask, we use the Jinja2 templating engine. Let's create a simple template for our home page.

Create a new folder called templates in the same directory as your app.py file. Inside the templates folder, create a file called home.html and add the following code:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>{{ message }}</h1> </body> </html>

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

python
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html', message="Hello, World!") if __name__ == "__main__": app.run(debug=True)

Now, when you access http://localhost:5000, Flask will render the home.html template, passing the message variable with the value "Hello, World!".

Quick Quiz
Question 1 of 1

What template engine does Flask use?

That's it for our first lesson! In the next lesson, we'll dive deeper into Flask, covering topics like form handling, static files, and more. Happy coding! 🥳